Sha256: 4b9b681f0d766011d02d815ff3638b57a4eeab4eb8368e1b356ef6ddbbf92585

Contents?: true

Size: 1.04 KB

Versions: 47

Compression:

Stored size: 1.04 KB

Contents

module Avo
  module Concerns
    module PolicyHelpers
      extend ActiveSupport::Concern

      class_methods do
        def inherit_association_from_policy(association_name, policy_class)
          [:create, :edit, :update, :destroy, :show, :reorder, :act_on].each do |method_action|
            define_policy_method(method_action, association_name, policy_class)
          end

          define_policy_method("view", association_name, policy_class, "index")
        end

        private

        # Define a method for the given action and association name.
        # Call the policy class with the given action passing the user and record.
        # Example:
        #  def create_team_members?
        #   TeamMemberPolicy.new(user, record).create?
        #  end
        def define_policy_method(method_action, association_name, policy_class, policy_action = method_action)
          define_method "#{method_action}_#{association_name}?" do
            policy_class.new(user, record).send "#{policy_action}?"
          end
        end
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
avo-2.53.0 lib/avo/concerns/policy_helpers.rb
avo-2.52.0 lib/avo/concerns/policy_helpers.rb
avo-2.49.0 lib/avo/concerns/policy_helpers.rb
avo-2.48.0 lib/avo/concerns/policy_helpers.rb
avo-2.47.0 lib/avo/concerns/policy_helpers.rb
avo-2.46.0 lib/avo/concerns/policy_helpers.rb
avo-2.45.0 lib/avo/concerns/policy_helpers.rb
avo-2.44.0 lib/avo/concerns/policy_helpers.rb
avo-2.43.0 lib/avo/concerns/policy_helpers.rb
avo-2.42.2 lib/avo/concerns/policy_helpers.rb
avo-2.42.1 lib/avo/concerns/policy_helpers.rb
avo-2.42.0 lib/avo/concerns/policy_helpers.rb
avo-2.41.0 lib/avo/concerns/policy_helpers.rb
avo-2.40.0 lib/avo/concerns/policy_helpers.rb
avo-2.39.0 lib/avo/concerns/policy_helpers.rb
avo-2.38.0 lib/avo/concerns/policy_helpers.rb
avo-2.37.2 lib/avo/concerns/policy_helpers.rb
avo-2.37.1 lib/avo/concerns/policy_helpers.rb
avo-2.37.0 lib/avo/concerns/policy_helpers.rb
avo-2.36.3 lib/avo/concerns/policy_helpers.rb