From 334c6e626438d59c725631115715b8fae49c0ae2 Mon Sep 17 00:00:00 2001 From: Brent Odell Date: Mon, 4 Mar 2019 14:28:11 -0600 Subject: [PATCH 1/3] Only count something as nested attributes if Hash - If the value is not a hash, we'll let ActiveAttr::Model#initialize take care of it through super() in Forminate#initialize --- lib/forminate/association_builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/forminate/association_builder.rb b/lib/forminate/association_builder.rb index 120a693..00bdad0 100644 --- a/lib/forminate/association_builder.rb +++ b/lib/forminate/association_builder.rb @@ -62,7 +62,7 @@ def unprefixed_attributes end def nested_attributes - attrs[name] + attrs[name] if attrs[name].is_a?(Hash) end end end From 72a6e8e90e7043174204b0b178994fa687c7153a Mon Sep 17 00:00:00 2001 From: Brent Odell Date: Mon, 4 Mar 2019 14:29:57 -0600 Subject: [PATCH 2/3] Don't cleanup attributes if no nested_attributes - The whole reason for cleaning up attributes was that if there were both nested_attributes & prefixed_attributes for the same value, we'd set the nested_attributes, but then they'd get overwritten by the prefixed attributes when super() was called in Forminate#initialize --- lib/forminate/association_builder.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/forminate/association_builder.rb b/lib/forminate/association_builder.rb index 00bdad0..98ed942 100644 --- a/lib/forminate/association_builder.rb +++ b/lib/forminate/association_builder.rb @@ -16,6 +16,8 @@ def build end def attribute_keys_for_cleanup + return [] unless nested_attributes.present? + prefixed_attributes.keys.push(name) end From e5c30f1d0590645bfe751bd115c4e91c63d5c8e0 Mon Sep 17 00:00:00 2001 From: Brent Odell Date: Mon, 4 Mar 2019 14:32:29 -0600 Subject: [PATCH 3/3] Dup keys for cleanup from AssociationBuilder - In Forminate - AssociationBuilder#attribute_keys_for_cleanup was getting modified with each iteration, reducing the number of keys that were supposed to be cleaned up, resulting in some never getting cleaned up. This gives us a copy that won't be mutated, fixing the issue. --- lib/forminate.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/forminate.rb b/lib/forminate.rb index 5bdaa1b..2e1cac1 100644 --- a/lib/forminate.rb +++ b/lib/forminate.rb @@ -122,8 +122,9 @@ def build_associations(attributes) association_names.each do |association_name| association_builder = AssociationBuilder.new(association_name, attributes) association = association_builder.build + keys_for_cleanup = association_builder.attribute_keys_for_cleanup.dup attributes.delete_if do |key, _| - association_builder.attribute_keys_for_cleanup.include?(key.to_sym) + keys_for_cleanup.include?(key.to_sym) end instance_variable_set("@#{association_name}".to_sym, association) end