Module: RailsConditionalParams::Patch
- Defined in:
- lib/rails_conditional_params/patch.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#permit_with_conditional_params(*filters) ⇒ Object
Adds support for conditional params:.
Class Method Details
.included(other) ⇒ Object
34 35 36 |
# File 'lib/rails_conditional_params/patch.rb', line 34 def Patch.included(other) other.alias_method_chain :permit, :conditional_params end |
Instance Method Details
#permit_with_conditional_params(*filters) ⇒ Object
Adds support for conditional params:
params.permit(yes: true, no: false)
# => { "yes" => "..." }
This is useful for parameters that should be permitted in some cases, but not others:
params.permit(:title, :body, published: admin?)
# Result only includes published if admin? is true.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rails_conditional_params/patch.rb', line 13 def permit_with_conditional_params(*filters) filters.each do |filter| if filter.is_a? Hash filter.delete_if do |key, value| case value when TrueClass filters << key true when FalseClass, NilClass # do nothing true else false end end end end permit_without_conditional_params(*filters) end |