Module: Recurso::Resource
- Included in:
- Global
- Defined in:
- lib/recurso/concerns/resource.rb
Class Method Summary collapse
Instance Method Summary collapse
- #policy_class ⇒ Object
- #policy_type ⇒ Object
- #policy_type=(value) ⇒ Object
- #relevant_levels_for(action) ⇒ Object
- #relevant_policy_type ⇒ Object
- #relevant_resources ⇒ Object
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. 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_class ⇒ Object
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_type ⇒ Object
18 19 20 |
# File 'lib/recurso/concerns/resource.rb', line 18 def policy_type &.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 .policy_type = value else (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_type ⇒ Object
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_resources ⇒ Object
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 |