Sha256: a633b42445a9010ac84d5685e6849ae368db2c37f2f966c142ba83404ed3634d
Contents?: true
Size: 1.52 KB
Versions: 2
Compression:
Stored size: 1.52 KB
Contents
module Para module AttributeField class NestedOneField < AttributeField::BelongsToField include Para::Helpers::AttributesMappings include Para::AttributeField::NestedField register :nested_one, self def parse_input(params, resource) if (nested_attributes = params[nested_attributes_key]) mappings = nested_model_mappings(nested_attributes) nested_resource = fetch_or_build_nested_resource_for(resource, nested_attributes) mappings.fields.each do |field| field.parse_input(nested_attributes, nested_resource) end params[nested_attributes_key] = nested_attributes else super end end private # Force loading association and look for a resource matching the provided # attributes. If no resource is found, one is created and a fake `id` is # assigned to it to hack Rails' nested resources lookup from new # attributes params. # # This is necessary to be able to provide a resource to the #parse_input # method when called on the nested resources hash # def fetch_or_build_nested_resource_for(parent, attributes) if (nested_resource = parent.association(name).load_target) return nested_resource end parent.association(name).build(attributes.slice('type')).tap do |resource| attributes['id'] = "__#{ object_id }" temporarily_extend_new_resource(resource, attributes) end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
para-0.7.1 | lib/para/attribute_field/nested_one.rb |
para-0.7.0 | lib/para/attribute_field/nested_one.rb |