Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix always use build associated #312

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/active_scaffold/attribute_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions lib/active_scaffold/helpers/controller_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -109,3 +113,4 @@ def build_associated(association, parent_record)
end
end
end

5 changes: 3 additions & 2 deletions test/misc/attribute_params_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' => ''})
Expand Down Expand Up @@ -301,3 +301,4 @@ def logger
@logger ||= Logger.new(STDOUT)
end
end