Skip to content

Commit

Permalink
refactor: extract permitted_attribute_names from permitted_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
milgner committed Jul 18, 2022
1 parent d7c2e6e commit 518c00e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
29 changes: 22 additions & 7 deletions lib/pundit/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,40 @@ def policy(record)
policies[record] ||= Pundit.policy!(pundit_user, record)
end

# Retrieves a set of permitted attributes from the policy by instantiating

# Retrieves a list of permitted attribute names from the policy by instantiating
# the policy class for the given record and calling `permitted_attributes` on
# it, or `permitted_attributes_for_{action}` if `action` is defined. It then infers
# what key the record should have in the params hash and retrieves the
# permitted attributes from the params hash under that key.
# it, or `permitted_attributes_for_{action}` if `action` is defined.
#
# @see https://github.com/varvet/pundit#strong-parameters
# @param record [Object] the object we're retrieving permitted attributes for
# @param action [Symbol, String] the name of the action being performed on the record (e.g. `:update`).
# If omitted then this defaults to the Rails controller action name.
# @return [Hash{String => Object}] the permitted attributes
def permitted_attributes(record, action = action_name)
# @return [Array<Symbol>] the names of the permitted attributes
def permitted_attribute_names(record, action = action_name)
policy = policy(record)
method_name = if policy.respond_to?("permitted_attributes_for_#{action}")
"permitted_attributes_for_#{action}"
else
"permitted_attributes"
end
pundit_params_for(record).permit(*policy.public_send(method_name))
policy.public_send(method_name)
end

# Retrieves a set of permitted attributes from the policy by instantiating
# the policy class for the given record and calling `permitted_attributes` on
# it, or `permitted_attributes_for_{action}` if `action` is defined. It then infers
# what key the record should have in the params hash and retrieves the
# permitted attributes from the params hash under that key.
#
# @see https://github.com/varvet/pundit#strong-parameters
# @param record [Object] the object we're retrieving permitted attributes for
# @param action [Symbol, String] the name of the action being performed on the record (e.g. `:update`).
# If omitted then this defaults to the Rails controller action name.
# @return [Hash{String => Object}] the permitted attributes
def permitted_attributes(record, action = action_name)
attribute_names = permitted_attribute_names(record, action)
pundit_params_for(record).permit(*attribute_names)
end

# Retrieves the params for the given record.
Expand Down
16 changes: 16 additions & 0 deletions spec/authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,22 @@
end
end

describe "#permitted_attribute_names" do
it "checks policy" do
action = 'update'
expect(Controller.new(user, action, {}).permitted_attribute_names(post)).to eq(
%i[title votes]
)
end
end

describe "#permitted_attribute_names_for_action" do
it "is checked if it is defined in the policy" do
action = "revise"
expect(Controller.new(user, action, {}).permitted_attribute_names(post)).to eq(%i[body])
end
end

describe "#permitted_attributes" do
it "checks policy for permitted attributes" do
params = ActionController::Parameters.new(
Expand Down

0 comments on commit 518c00e

Please sign in to comment.