lib/action_policy/behaviour.rb in action_policy-0.6.1 vs lib/action_policy/behaviour.rb in action_policy-0.6.2

- old
+ new

@@ -56,14 +56,15 @@ policy.apply(authorization_rule_for(policy, rule)) policy.result end def authorization_context - return @__authorization_context if - instance_variable_defined?(:@__authorization_context) + @_authorization_context ||= build_authorization_context + end - @__authorization_context = self.class.authorization_targets + def build_authorization_context + self.class.authorization_targets .each_with_object({}) do |(key, meth), obj| obj[key] = send(meth) end end @@ -71,14 +72,22 @@ # otherwise fallback to :manage? rule. def authorization_rule_for(policy, rule) policy.resolve_rule(rule) end - def lookup_authorization_policy(record, **options) # :nodoc: - record = implicit_authorization_target! if :__undef__ == record # rubocop:disable Style/YodaCondition See https://github.com/palkan/action_policy/pull/180 - Kernel.raise ArgumentError, "Record must be specified" if record.nil? + def lookup_authorization_policy(record, with: nil, **options) # :nodoc: + if record == :__undef__ + record = + if with + implicit_authorization_target + else + implicit_authorization_target! + end + end - policy_for(record: record, **options) + Kernel.raise ArgumentError, "Record or policy must be specified" if record.nil? && with.nil? + + policy_for(record: record, with: with, **options) end module ClassMethods # :nodoc: # Configure authorization context. #