Skip to content

Instantly share code, notes, and snippets.

@romanlehnert
Forked from johncant/attributify.rb
Last active August 29, 2015 14:16
Show Gist options
  • Save romanlehnert/debda25c4d1d5416cc0c to your computer and use it in GitHub Desktop.
Save romanlehnert/debda25c4d1d5416cc0c to your computer and use it in GitHub Desktop.
module Attributify
def attributify(key, unprocessed=params[key])
if unprocessed.is_a? Hash
specific_params = {}
unprocessed.each do |k,v|
if (v.is_a?(Hash) && !k.to_s.match(/_attributes$/)) or (v.is_a?(Array) && !k.to_s.match(/_ids$/))
# Nested, so suffix with '_attributes'
specific_params["#{k}_attributes".to_sym] = attributify(key, v)
else
specific_params[k] = v
end
end
return specific_params
elsif unprocessed.is_a? Array
unprocessed.map do |p| attributify(key, p) end
else
unprocessed
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment