Sha256: e3b4b93fa8949001f3c69e50125591a1877b7719af702c03fd34fc1511823a38

Contents?: true

Size: 1.03 KB

Versions: 7

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true
require 'pundit'
require 'proxes/request'

module ProxES
  module Helpers
    module Pundit
      include ::Pundit

      def authorize(record, query = nil)
        if record.is_a?(Request)
          query = record.request_method.downcase
        elsif query.nil?
          raise ArgumentError, 'Pundit cannot determine the query'
        end
        query = :"#{query}?" unless query[-1] == '?'
        super
      end

      def permitted_attributes(record, action)
        param_key = PolicyFinder.new(record).param_key
        policy = policy(record)
        method_name = if policy.respond_to?("permitted_attributes_for_#{action}")
                        "permitted_attributes_for_#{action}"
                      else
                        'permitted_attributes'
                      end

        request.params.fetch(param_key, {}).select do |key, _value|
          policy.public_send(method_name).include? key.to_sym
        end
      end

      def pundit_user
        current_user
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
proxes-0.4.0 lib/proxes/helpers/pundit.rb
proxes-0.3.6 lib/proxes/helpers/pundit.rb
proxes-0.3.5 lib/proxes/helpers/pundit.rb
proxes-0.3.3 lib/proxes/helpers/pundit.rb
proxes-0.3.2 lib/proxes/helpers/pundit.rb
proxes-0.3.1 lib/proxes/helpers/pundit.rb
proxes-0.3.0 lib/proxes/helpers/pundit.rb