Created
February 18, 2014 16:36
-
-
Save rwrrll/9074470 to your computer and use it in GitHub Desktop.
Kinda hacky way of specifying that permitted parameters must be present in Rails' strong_parameters - use `insist` in place of `permit`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActionController | |
class Parameters | |
def insist(*filters) | |
filters.each { |f| ensure_presence(f) } | |
permit(filters) | |
end | |
private | |
def ensure_presence(filter, params = self) | |
case filter | |
when Symbol, String | |
raise ParameterMissing.new(filter) unless params.include? filter.to_s | |
when Hash | |
key = filter.keys.first | |
ensure_presence(key, params) | |
ensure_presence(filter.values, params[key]) | |
when Array | |
filter.each { |f| ensure_presence(f, params) } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment