Sha256: 151230c402c25b006619bde11eff51e98cb214b6c8fa9ca52f47d1c17852873a

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module ActionPolicy
  module Policy
    # Adds rules aliases support and ability to specify
    # the default rule.
    #
    #   class ApplicationPolicy
    #     include ActionPolicy::Policy::Core
    #     include ActionPolicy::Policy::Aliases
    #
    #     # define which rule to use if `authorize!` called with
    #     # unknown rule
    #     default_rule :manage?
    #
    #     alias_rule :publish?, :unpublish?, to: :update?
    #   end
    #
    # Aliases are used only during `authorize!` call (and do not act like _real_ aliases).
    #
    # Aliases useful when combined with `CachedApply` (since we can cache only the target rule).
    module Aliases
      DEFAULT = :__default__

      class << self
        def included(base)
          base.extend ClassMethods
        end
      end

      def resolve_rule(activity)
        self.class.lookup_alias(activity) ||
          (activity if respond_to?(activity)) ||
          self.class.lookup_default_rule ||
          super
      end

      module ClassMethods # :nodoc:
        def default_rule(val)
          rules_aliases[DEFAULT] = val
        end

        def alias_rule(*rules, to:)
          rules.each do |rule|
            rules_aliases[rule] = to
          end
        end

        def lookup_alias(rule) = rules_aliases[rule]

        def lookup_default_rule() = rules_aliases[DEFAULT]

        def rules_aliases
          return @rules_aliases if instance_variable_defined?(:@rules_aliases)

          if superclass.respond_to?(:rules_aliases)
            superclass.rules_aliases.dup
          else
            {}
          end => @rules_aliases
        end

        def method_added(name)
          rules_aliases.delete(name) if public_method_defined?(name)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
action_policy-0.5.4 lib/action_policy/policy/aliases.rb
action_policy-0.5.3 lib/action_policy/policy/aliases.rb
action_policy-0.5.2 lib/action_policy/policy/aliases.rb
action_policy-0.5.1 lib/action_policy/policy/aliases.rb
action_policy-0.5.0 lib/action_policy/policy/aliases.rb