Sha256: 73be348ff9db53698cfb77d1882dc601add0492c12b56385bad615fbb8c1f163

Contents?: true

Size: 1.03 KB

Versions: 24

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module PagesCore
  module PoliciesHelper
    extend ActiveSupport::Concern

    included do
      helper_method :policy
    end

    module ClassMethods
      def require_authorization(object: nil, instance: nil)
        object ||= inferred_policy_class

        before_action do |controller|
          instance_name = "@#{object.name.underscore}"
          record = instance || controller.instance_variable_get(instance_name)

          verify_policy_with_proc(controller, record || object)
        end
      end

      def inferred_policy_class
        const_get(name.demodulize.gsub(/Controller$/, "").singularize)
      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

24 entries across 24 versions & 1 rubygems

Version Path
pages_core-3.8.2 app/controllers/concerns/pages_core/policies_helper.rb
pages_core-3.8.1 app/controllers/concerns/pages_core/policies_helper.rb
pages_core-3.8.0 app/controllers/concerns/pages_core/policies_helper.rb
pages_core-3.7.0 app/controllers/concerns/pages_core/policies_helper.rb