さて、私は単純なクラスをコーディングしました。世界で最高のコードではありませんが、機能します
# nested attributes module
module Api
module NestedAttributes
# check for nested attributes
# transform parameters like this:
# company: { name: 'test1', address: [{name: 'name1'}]}
# To
# company: { name: 'test1', address_attributes: [{name: 'name1'}]}
def transform params, attributes = []
apply params, attributes
return params
end
private
def generate_key key
"#{key.to_s}_attributes".to_sym
end
def apply params, attributes
case attributes
when Array
apply_array params, attributes
when Symbol
apply_symbol params, attributes
when Hash
apply_hash params, attributes
end
end
def apply_array params, array
# return the params if the attributes are empty
return params if array.empty?
# for each array value
array.each do |value|
apply params, value
end
end
def apply_hash params, hash
# return the params if the attributes are empty
return params if hash.empty?
# for each key
hash.each_key do |key|
# the new key name
new_key = generate_key key
if params.has_key? key
# delete the key and assigned the value to the new key
params[new_key] = params.delete(key)
apply params[new_key], hash[key]
end
end
end
def apply_symbol params, symbol
if params.is_a? Array
params.each { |p| apply_symbol p, symbol}
else
if params.has_key? symbol
new_key = generate_key symbol
# delete the key and assigned the value to the new key
params[new_key] = params.delete(symbol)
end
end
end
end
end
利用方法:
class Test
include Api:NestedAttributes
def test
params = { company: { options: {}}}
transform params, company: options
# result => { company_attributes: { options_attributes: {}}}
end
end