From 5eaedd36fe4ed404049ca02b39a48acb84fbed4f Mon Sep 17 00:00:00 2001 From: Nicolae Claudius Date: Sat, 16 Nov 2013 17:58:03 +0200 Subject: [PATCH 1/2] Always create subrecords via build_associated --- CHANGELOG | 1 + lib/active_scaffold/attribute_params.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 99cea8d1f3..3804540399 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -19,6 +19,7 @@ - Display count items when pagination is disabled - Check security in column groups in forms - Add refresh_link option to :select form_ui +- Always create subrecords via build_associated = 3.3.3 - Allow to override select options in active_scaffold_search_select diff --git a/lib/active_scaffold/attribute_params.rb b/lib/active_scaffold/attribute_params.rb index 82e8bd607e..17e4e06e50 100644 --- a/lib/active_scaffold/attribute_params.rb +++ b/lib/active_scaffold/attribute_params.rb @@ -173,7 +173,7 @@ def find_or_create_for_params(params, parent_column, parent_record) if params.has_key? klass.primary_key record_from_current_or_find(klass, params[klass.primary_key], current) elsif klass.authorized_for?(:crud_type => :create) - parent_column.association.klass.new + build_associated(parent_column.association, parent_record) end end From b82fdc8f7252ca88b1f44b72a0ff75e511fa967d Mon Sep 17 00:00:00 2001 From: Nicolae Claudius Date: Sun, 17 Nov 2013 13:23:43 +0200 Subject: [PATCH 2/2] fix tests --- lib/active_scaffold/helpers/controller_helpers.rb | 5 +++++ test/misc/attribute_params_test.rb | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/active_scaffold/helpers/controller_helpers.rb b/lib/active_scaffold/helpers/controller_helpers.rb index 1f96445959..0f0e0b8206 100644 --- a/lib/active_scaffold/helpers/controller_helpers.rb +++ b/lib/active_scaffold/helpers/controller_helpers.rb @@ -100,6 +100,10 @@ def build_associated(association, parent_record) # avoid use build_association in has_one when record is saved and had associated record # because associated record would be changed in DB parent_record.send("build_#{association.name}") + elsif association.macro == :has_one && parent_record.persisted? && parent_record.send(association.name).present? + association.klass.new.tap do |record| + parent_record.send("#{association.name}=", record) + end else association.klass.new.tap do |record| save_record_to_association(record, association.reverse, parent_record) # set inverse @@ -109,3 +113,4 @@ def build_associated(association, parent_record) end end end + diff --git a/test/misc/attribute_params_test.rb b/test/misc/attribute_params_test.rb index 4d61c5a11b..015a045ba6 100644 --- a/test/misc/attribute_params_test.rb +++ b/test/misc/attribute_params_test.rb @@ -168,8 +168,8 @@ def test_saving_has_many_crud_and_belongs_to_select model = update_record_from_params(Building.new, :create, :name, :floors, :name => 'First', :floors => floors) assert_equal 'First', model.name assert_equal 3, model.floors.size - assert_equal floor.id, model.floors.first.id - assert_equal [nil, *people.map(&:id)], model.floors.map(&:tenant_id) + assert_equal floor.id, model.floors.last.id + assert_equal [*people.map(&:id), nil], model.floors.map(&:tenant_id) assert model.save model = update_record_from_params(model, :update, :name, :floors, :name => 'Tower', :floors => {'0' => ''}) @@ -301,3 +301,4 @@ def logger @logger ||= Logger.new(STDOUT) end end +