Skip to content

Instantly share code, notes, and snippets.

@rwrrll
Created February 18, 2014 16:36
Show Gist options
  • Save rwrrll/9074470 to your computer and use it in GitHub Desktop.
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`.
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