Module: Recurso::Resource

Included in:
Global
Defined in:
lib/recurso/concerns/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/recurso/concerns/resource.rb', line 4

def self.included(base)
  base.has_many :permissions, as: :resource, dependent: :destroy, class_name: Recurso::Config.instance.default_permission_class_name
  base.has_one  :permission_policy, as: :resource, autosave: true
  base.belongs_to :itself, class_name: base.to_s, foreign_key: :id, required: false

  def base.relevant_associations
    relevant_association_names.map(&method(:reflect_on_association))
  end

  def base.relevant_association_names
    [:itself]
  end
end

Instance Method Details

#policy_classObject



30
31
32
33
34
# File 'lib/recurso/concerns/resource.rb', line 30

def policy_class
  "#{self.class}Policy".constantize
rescue NameError
  Recurso::ResourcePolicy
end

#policy_typeObject



18
19
20
# File 'lib/recurso/concerns/resource.rb', line 18

def policy_type
  permission_policy&.policy_type
end

#policy_type=(value) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/recurso/concerns/resource.rb', line 22

def policy_type=(value)
  if permission_policy
    permission_policy.policy_type = value
  else
    build_permission_policy(policy_type: value)
  end
end

#relevant_levels_for(action) ⇒ Object



44
45
46
47
48
49
# File 'lib/recurso/concerns/resource.rb', line 44

def relevant_levels_for(action)
  [
    Recurso::Config.instance.levels_for_action[action],
    (Recurso::Config.instance.default_level if default_can?(action))
  ].flatten.compact
end

#relevant_policy_typeObject



36
37
38
# File 'lib/recurso/concerns/resource.rb', line 36

def relevant_policy_type
  (relevant_resources.map(&:policy_type).detect(&:present?) || :open).to_sym
end

#relevant_resourcesObject



40
41
42
# File 'lib/recurso/concerns/resource.rb', line 40

def relevant_resources
  @relevant_resources ||= self.class.relevant_association_names.map(&method(:public_send)).compact.uniq
end