Skip to content

Refactor permitted attributes method#502

Merged
Linuus merged 3 commits intovarvet:masterfrom
pcriv:refactor-permitted-attributes-method
May 17, 2018
Merged

Refactor permitted attributes method#502
Linuus merged 3 commits intovarvet:masterfrom
pcriv:refactor-permitted-attributes-method

Conversation

@pcriv
Copy link
Copy Markdown
Contributor

@pcriv pcriv commented Mar 9, 2018

Closes #469
Closes #396

@pcriv
Copy link
Copy Markdown
Contributor Author

pcriv commented Mar 19, 2018

@Linuus could you also check this PR when you are back?

@Linuus Linuus self-assigned this Mar 19, 2018
@Linuus
Copy link
Copy Markdown
Collaborator

Linuus commented May 15, 2018

@pablocrivella Could you just rebase this on master so we make Travis happy?

@Linuus Linuus added this to the v2.0 milestone May 15, 2018
@pcriv
Copy link
Copy Markdown
Contributor Author

pcriv commented May 15, 2018

@Linuus Done

Comment thread README.md Outdated

If you have defined an action-specific method on your policy for the current action, the `permitted_attributes` helper will call it instead of calling `permitted_attributes` on your controller.

If you have a different way of fetching the parameters other than the standard way:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundancy: "different", "other", "standard".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestions on how to phrase this?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but there's perhaps a more fundamental problem with this PR. See below. :)

Copy link
Copy Markdown

@ce07c3 ce07c3 May 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"If you need to fetch parameters based on namespaces different from the suggested one, override the below method and return an instance of ActionController::Parameters."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks @ce07c3 :)

Comment thread README.md Outdated
end
```

You can override it this method like this:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"For example:"

@pcriv
Copy link
Copy Markdown
Contributor Author

pcriv commented May 15, 2018

@Linuus @ce07c3 Do you want me to fix the README issues on a new commit or amend the last one?

Comment thread lib/pundit.rb
"permitted_attributes"
end
params.require(param_key).permit(*policy.public_send(method_name))
pundit_params_for(record).permit(*policy.public_send(method_name))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pablocrivella we still need to pass the Rails controller specific params implementation in, right? I don't think Hash has a permit instance method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, this would only work with an instance of ActionController::Parameters for the permit method to work

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, should work then. :)

Comment thread README.md
```ruby
# If you don't want to use require
def pundit_params_for(record)
params.fetch(PolicyFinder.new(record).param_key, {})
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does fetch still return the Rails controller specific params implementation? @pablocrivella

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it does:

ams = ActionController::Parameters.new()
=> <ActionController::Parameters {} permitted: false>
ams.fetch(:something, {}).class
=> ActionController::Parameters

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I didn't think this would work actually. According to the docs: http://edgeapi.rubyonrails.org/classes/ActionController/Parameters.html#method-i-fetch it should return the value given to fetch. However, when looking at the code you can clearly see that the value is converted to an instance of ActionController::Parameters (if possible). So I guess it should work :)

@pcriv
Copy link
Copy Markdown
Contributor Author

pcriv commented May 16, 2018

@ce07c3 Updated

Comment thread README.md
params.fetch(PolicyFinder.new(record).param_key, {})
end

# If you are using something like the jsonapi spec
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"JSON API" seems to be the official name @pablocrivella (I'm picky 👼)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ce07c3 I missed this one! I would have changed it since i'm also picky like that 😅

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s fixed in master :)

@Linuus
Copy link
Copy Markdown
Collaborator

Linuus commented May 16, 2018

This looks like a pretty good solution to me. I'll just have to think about it a bit.

Could you squash the two readme updates please?

Comment thread lib/pundit.rb
pundit_params_for(record).permit(*policy.public_send(method_name))
end

def pundit_params_for(record)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add some documentation for this method.

@Linuus Linuus merged commit bceb153 into varvet:master May 17, 2018
@Linuus
Copy link
Copy Markdown
Collaborator

Linuus commented May 17, 2018

Thanks!

@pcriv pcriv deleted the refactor-permitted-attributes-method branch May 18, 2018 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants