Module: Pressroom::Scoped

Extended by:
ActiveSupport::Concern
Included in:
Category, Post, Tag
Defined in:
app/models/concerns/pressroom/scoped.rb

Overview

Belonging to one publication among several.

An application that hosts a single blog never touches any of this: the empty scope is the default, and every query without an explicit scope behaves exactly as it did before publications existed.

Constant Summary collapse

NO_SCOPE_TYPE =
""
NO_SCOPE_ID =
0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.id_for(owner) ⇒ Object



30
# File 'app/models/concerns/pressroom/scoped.rb', line 30

def self.id_for(owner) = owner ? owner.id : NO_SCOPE_ID

.type_for(owner) ⇒ Object



28
# File 'app/models/concerns/pressroom/scoped.rb', line 28

def self.type_for(owner) = owner ? owner.class.polymorphic_name : NO_SCOPE_TYPE

Instance Method Details

#scopeObject



32
33
34
35
36
# File 'app/models/concerns/pressroom/scoped.rb', line 32

def scope
  return nil unless scoped?

  scope_type.constantize.find_by(id: scope_id)
end

#scope=(owner) ⇒ Object



38
39
40
41
# File 'app/models/concerns/pressroom/scoped.rb', line 38

def scope=(owner)
  self.scope_type = Scoped.type_for(owner)
  self.scope_id = Scoped.id_for(owner)
end

#scoped?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/concerns/pressroom/scoped.rb', line 43

def scoped?
  scope_type.present? && scope_id.to_i.positive?
end