module Recurso module Resource 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 def policy_type permission_policy&.policy_type end def policy_type=(value) if permission_policy permission_policy.policy_type = value else build_permission_policy(policy_type: value) end end def policy_class "#{self.class}Policy".constantize rescue NameError Recurso::ResourcePolicy end def relevant_policy_type (relevant_resources.map(&:policy_type).detect(&:present?) || :open).to_sym end def relevant_resources @relevant_resources ||= self.class.relevant_association_names.map(&method(:public_send)).compact.uniq end def relevant_levels_for(action) [ Recurso::Config.instance.levels_for_action[action], (Recurso::Config.instance.default_level if default_can?(action)) ].flatten.compact end private def default_can?(action) Recurso::Config.instance.actions_for_default[relevant_policy_type].include?(action) end end end