Last active
December 20, 2015 16:39
-
-
Save xaethos/6163374 to your computer and use it in GitHub Desktop.
Convert as_json hash to controller params, for testing ease
This file contains 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
def to_form_param json | |
case json | |
when Array | |
return json if json.empty? || !json[0].is_a?(Hash) | |
i = 0 | |
json.reduce({}.with_indifferent_access) do |memo, item| | |
memo["#{i}"] = to_form_param item | |
i += 1 | |
memo | |
end | |
when Hash | |
out = json.dup.with_indifferent_access | |
out.each do |key, value| | |
out[key] = to_form_param value | |
end | |
out | |
else | |
json | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment