Navigation Menu

Skip to content

Commit

Permalink
Add an :update_only option to accepts_nested_attributes_for for to-on…
Browse files Browse the repository at this point in the history
…e associations. [#2563 state:resolved]

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
  • Loading branch information
siebertm authored and alloy committed Dec 28, 2009
1 parent 9c771a9 commit 07b615f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
26 changes: 23 additions & 3 deletions activerecord/lib/active_record/nested_attributes.rb
Expand Up @@ -212,6 +212,11 @@ module ClassMethods
# nested attributes array exceeds the specified limit, NestedAttributes::TooManyRecords
# exception is raised. If omitted, any number associations can be processed.
# Note that the :limit option is only applicable to one-to-many associations.
# [:update_only]
# Allows to specify that the an existing record can only be updated.
# A new record in only created when there is no existing record. This
# option only works for on-to-one associations and is ignored for
# collection associations. This option is off by default.
#
# Examples:
# # creates avatar_attributes=
Expand All @@ -221,9 +226,9 @@ module ClassMethods
# # creates avatar_attributes= and posts_attributes=
# accepts_nested_attributes_for :avatar, :posts, :allow_destroy => true
def accepts_nested_attributes_for(*attr_names)
options = { :allow_destroy => false }
options = { :allow_destroy => false, :update_only => false }
options.update(attr_names.extract_options!)
options.assert_valid_keys(:allow_destroy, :reject_if, :limit)
options.assert_valid_keys(:allow_destroy, :reject_if, :limit, :update_only)

attr_names.each do |association_name|
if reflection = reflect_on_association(association_name)
Expand Down Expand Up @@ -288,14 +293,29 @@ def _delete #:nodoc:
# record’s id, then the existing record will be modified. Otherwise a new
# record will be built.
#
# If update_only is true, a new record is only created when no object exists,
# otherwise it will be updated
#
# If update_only is false and the given attributes include an <tt>:id</tt>
# that matches the existing record’s id, then the existing record will be
# modified. Otherwise a new record will be built.
#
# If the given attributes include a matching <tt>:id</tt> attribute _and_ a
# <tt>:_destroy</tt> key set to a truthy value, then the existing record
# will be marked for destruction.
def assign_nested_attributes_for_one_to_one_association(association_name, attributes)
options = self.nested_attributes_options[association_name]
attributes = attributes.with_indifferent_access

if attributes['id'].blank?
if options[:update_only]
if existing_record = send(association_name)
assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
else
unless reject_new_record?(association_name, attributes)
send("build_#{association_name}", attributes.except(*UNASSIGNABLE_KEYS))
end
end
elsif attributes['id'].blank?
unless reject_new_record?(association_name, attributes)
method = "build_#{association_name}"
if respond_to?(method)
Expand Down
31 changes: 31 additions & 0 deletions activerecord/test/cases/nested_attributes_test.rb
Expand Up @@ -245,6 +245,37 @@ def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
def test_should_automatically_enable_autosave_on_the_association
assert Pirate.reflect_on_association(:ship).options[:autosave]
end

def test_should_accept_update_only_option
Pirate.accepts_nested_attributes_for :ship, :update_only => true
@pirate.update_attribute(:ship_attributes, { :id => @pirate.ship.id, :name => 'Mayflower' })

Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
end

def test_should_create_new_model_when_nothing_is_there_and_update_only_is_true
Pirate.accepts_nested_attributes_for :ship, :update_only => true
@ship.delete

assert_difference('Ship.count', 1) do
@pirate.reload.update_attribute(:ship_attributes, { :name => 'Mayflower' })
end

Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
end


def test_should_update_existing_when_update_only_is_true_and_no_id_is_given
Pirate.accepts_nested_attributes_for :ship, :update_only => true

assert_no_difference('Ship.count') do
@pirate.reload.update_attributes(:ship_attributes => { :name => 'Mayflower' })
end

assert_equal 'Mayflower', @ship.reload.name

Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
end
end

class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
Expand Down

0 comments on commit 07b615f

Please sign in to comment.