-
Notifications
You must be signed in to change notification settings - Fork 642
Add support for params.expect #855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@@ -273,6 +273,91 @@ def to_params(*args, **kwargs, &block) | |||
end | |||
end | |||
|
|||
if Gem::Version.new(ActionPack::VERSION::STRING) >= Gem::Version.new("8.0.0") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like JRUBY doesn't support this version? I don't know if this is a good approach though :)
This looks as one could expect (:D). I'm also wondering if this is a good time to adjust what kind of API we will permit (:D), since this is an addition we've got time to adjust the API without breaking any backwards-compatibility. Things I'm thinking of:
We're still rather light on Pundit-usage in our current projects. What about your project, do you use the action-specific |
We do, yeah. |
Well we are not using the expect at all yet as we are waiting for it to be added in Pundit, right? :) |
Correct, I thought the question was if we're using the existing action specific permitted attributes methods. |
Ah yes. Sometimes I write a bit för fort! I haven't forgotten this. Still thinking about if I want to adjust the future API now that there's a chance to do so. |
We use
For us that means that in most cases the methods would look like: def expected_params_for(_action)
%i[foo bar]
end The action-specific cases would take one of two forms: # 1 Switch
def expected_params_for(action)
case action.to_sym
when :create
%i[foo bar]
when :update
[:bar, (:foo if user.admin?)].compact
else
%i[bar]
end
end
# 2 Concat
def expected_params_for(action)
params = %i[foo]
params += %i[bar baz] if action.to_sym == :create
params
end Things I'd note:
All that to say, I think I prefer copying the existing policy API ala |
@matthew-puku Thanks, this is great feedback! |
Fixes: #841
To do
This PR starts adding support for
params.expect
in Rails which is now recommended overparams.require.permit
.See: https://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-expect
I'm a bit unsure what to do with
pundit_params_for
though. It's kind of redundant... but also kind of useful as mentioned in our Readme, if you want to fetch based on another "namespace" than the default. However, for expect it would essentially just turn into a wrapper method forPolicyFinder.new(record).param_key
, so you can just override theparam_key
method on the Policy instead? 🤔