Sha256: b062f70effcf8a7a8bf6d5b467fdc9eb33fb52c09dcef09698bcc13c5577bf25

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module PagesCore
  module PoliciesHelper
    extend ActiveSupport::Concern

    included do
      helper_method :policy
    end

    module ClassMethods
      def require_authorization(collection, member, options = {})
        options = default_options.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

      def default_options
        {
          collection: [:index, :new, :create],
          member:     [:show, :edit, :update, :destroy]
        }
      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(action_name + "?")
      raise PagesCore::NotAuthorized
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pages_core-3.5.1 app/controllers/concerns/pages_core/policies_helper.rb