Skip to content

Commit

Permalink
Use controller’s action_name attr instead of params[:action]
Browse files Browse the repository at this point in the history
This is the recommended way to access a controller’s current action
  • Loading branch information
Ross-Hunter committed May 17, 2018
1 parent d28170b commit 667b138
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/pundit.rb
Expand Up @@ -175,7 +175,7 @@ def verify_policy_scoped
# @raise [NotAuthorizedError] if the given query method returned false
# @return [Object] Always returns the passed object record
def authorize(record, query = nil)
query ||= params[:action].to_s + "?"
query ||= "#{action_name}?"

@_pundit_policy_authorized = true

Expand Down Expand Up @@ -232,7 +232,7 @@ def policy(record)
# @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 = params[:action])
def permitted_attributes(record, action = action_name)
policy = policy(record)
method_name = if policy.respond_to?("permitted_attributes_for_#{action}")
"permitted_attributes_for_#{action}"
Expand Down
38 changes: 25 additions & 13 deletions spec/pundit_spec.rb
Expand Up @@ -8,7 +8,7 @@
let(:comment) { Comment.new }
let(:comment_four_five_six) { CommentFourFiveSix.new }
let(:article) { Article.new }
let(:controller) { Controller.new(user, action: "update") }
let(:controller) { Controller.new(user, "update", {}) }
let(:artificial_blog) { ArtificialBlog.new }
let(:article_tag) { ArticleTag.new }
let(:comments_relation) { CommentsRelation.new }
Expand Down Expand Up @@ -426,56 +426,68 @@

describe "#permitted_attributes" do
it "checks policy for permitted attributes" do
params = ActionController::Parameters.new(action: "update", post: {
params = ActionController::Parameters.new(post: {
title: "Hello",
votes: 5,
admin: true
})

expect(Controller.new(user, params).permitted_attributes(post).to_h).to eq("title" => "Hello", "votes" => 5)
expect(Controller.new(double, params).permitted_attributes(post).to_h).to eq("votes" => 5)
action = "update"

expect(Controller.new(user, action, params).permitted_attributes(post).to_h).to eq(
"title" => "Hello",
"votes" => 5
)
expect(Controller.new(double, action, params).permitted_attributes(post).to_h).to eq("votes" => 5)
end

it "checks policy for permitted attributes for record of a ActiveModel type" do
params = ActionController::Parameters.new(action: "update", customer_post: {
params = ActionController::Parameters.new(customer_post: {
title: "Hello",
votes: 5,
admin: true
})

expect(Controller.new(user, params).permitted_attributes(customer_post)).to eq("title" => "Hello", "votes" => 5)
expect(Controller.new(double, params).permitted_attributes(customer_post)).to eq("votes" => 5)
expect(Controller.new(user, params).permitted_attributes(customer_post).to_h).to eq(
action = "update"

expect(Controller.new(user, action, params).permitted_attributes(customer_post).to_h).to eq(
"title" => "Hello",
"votes" => 5
)
expect(Controller.new(double, action, params).permitted_attributes(customer_post)).to eq("votes" => 5)
expect(Controller.new(user, action, params).permitted_attributes(customer_post).to_h).to eq(
"title" => "Hello",
"votes" => 5
)
expect(Controller.new(double, params).permitted_attributes(customer_post).to_h).to eq(
expect(Controller.new(double, action, params).permitted_attributes(customer_post).to_h).to eq(
"votes" => 5
)
end
end

describe "#permitted_attributes_for_action" do
it "is checked if it is defined in the policy" do
params = ActionController::Parameters.new(action: "revise", post: {
params = ActionController::Parameters.new(post: {
title: "Hello",
body: "blah",
votes: 5,
admin: true
})

expect(Controller.new(user, params).permitted_attributes(post).to_h).to eq("body" => "blah")
action = "revise"
expect(Controller.new(user, action, params).permitted_attributes(post).to_h).to eq("body" => "blah")
end

it "can be explicitly set" do
params = ActionController::Parameters.new(action: "update", post: {
params = ActionController::Parameters.new(post: {
title: "Hello",
body: "blah",
votes: 5,
admin: true
})

expect(Controller.new(user, params).permitted_attributes(post, :revise).to_h).to eq("body" => "blah")
action = "update"
expect(Controller.new(user, action, params).permitted_attributes(post, :revise).to_h).to eq("body" => "blah")
end
end

Expand Down
5 changes: 3 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -156,10 +156,11 @@ class Controller
# Mark protected methods public so they may be called in test
public(*Pundit.protected_instance_methods)

attr_reader :current_user, :params
attr_reader :current_user, :action_name, :params

def initialize(current_user, params)
def initialize(current_user, action_name, params)
@current_user = current_user
@action_name = action_name
@params = params
end
end
Expand Down

0 comments on commit 667b138

Please sign in to comment.