Sha256: 2a4bf293afe3d3ba6bf833c3c040fb3309c7ac2957c4f95a86aaf99641917f84

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

module PagesCore
  module PoliciesHelper
    extend ActiveSupport::Concern

    included do
      helper_method :policy
    end

    module ClassMethods
      def require_authorization(collection, member, options = {})
        options = {
          collection: [:index, :new, :create],
          member:     [:show, :edit, :update, :destroy]
        }.merge(options)
        before_action do |controller|
          action = params[:action].to_sym
          if options[:collection].include?(action)
            verify_policy_with_proc(controller, collection)
          elsif options[:member].include?(action)
            verify_policy_with_proc(controller, member)
          end
        end
      end
    end

    def policy(object)
      Policy.for(current_user, object)
    end

    def verify_policy_with_proc(controller, record)
      record = controller.instance_eval(&record) if record.is_a?(Proc)
      verify_policy(record)
    end

    def verify_policy(record)
      return true if policy(record).public_send(params[:action] + "?")
      fail PagesCore::NotAuthorized
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pages_core-3.4.3 app/controllers/concerns/pages_core/policies_helper.rb
pages_core-3.4.2 app/controllers/concerns/pages_core/policies_helper.rb