aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorEloy Duran <eloy.de.enige@gmail.com>2009-02-01 14:44:30 +1300
committerMichael Koziarski <michael@koziarski.com>2009-02-01 14:44:30 +1300
commitec8f04584479aff895b0b511a7ba1e9d33f84067 (patch)
tree86d6580345865cd1ce63b2055c0eca091ba8ee52 /activerecord/lib
parenta02d752ae408b34e048b7b040e28c36074a9119f (diff)
downloadrails-ec8f04584479aff895b0b511a7ba1e9d33f84067.tar.gz
rails-ec8f04584479aff895b0b511a7ba1e9d33f84067.tar.bz2
rails-ec8f04584479aff895b0b511a7ba1e9d33f84067.zip
Add support for nested object forms to ActiveRecord and the helpers in ActionPack
Signed-Off-By: Michael Koziarski <michael@koziarski.com> [#1202 state:committed]
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record.rb2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb91
-rw-r--r--activerecord/lib/active_record/autosave_association.rb213
-rwxr-xr-xactiverecord/lib/active_record/base.rb5
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb279
-rw-r--r--activerecord/lib/active_record/reflection.rb5
-rw-r--r--activerecord/lib/active_record/test_case.rb1
7 files changed, 553 insertions, 43 deletions
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb
index e1265b7e1e..fa804f6a73 100644
--- a/activerecord/lib/active_record.rb
+++ b/activerecord/lib/active_record.rb
@@ -46,6 +46,7 @@ module ActiveRecord
autoload :AssociationPreload, 'active_record/association_preload'
autoload :Associations, 'active_record/associations'
autoload :AttributeMethods, 'active_record/attribute_methods'
+ autoload :AutosaveAssociation, 'active_record/autosave_association'
autoload :Base, 'active_record/base'
autoload :Calculations, 'active_record/calculations'
autoload :Callbacks, 'active_record/callbacks'
@@ -55,6 +56,7 @@ module ActiveRecord
autoload :Migration, 'active_record/migration'
autoload :Migrator, 'active_record/migration'
autoload :NamedScope, 'active_record/named_scope'
+ autoload :NestedAttributes, 'active_record/nested_attributes'
autoload :Observing, 'active_record/observer'
autoload :QueryCache, 'active_record/query_cache'
autoload :Reflection, 'active_record/reflection'
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 8b51a38f48..7a88465f8c 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -88,6 +88,18 @@ module ActiveRecord
end unless self.new_record?
end
+ private
+ # Gets the specified association instance if it responds to :loaded?, nil otherwise.
+ def association_instance_get(name)
+ association = instance_variable_get("@#{name}")
+ association if association.respond_to?(:loaded?)
+ end
+
+ # Set the specified association instance.
+ def association_instance_set(name, association)
+ instance_variable_set("@#{name}", association)
+ end
+
# Associations are a set of macro-like class methods for tying objects together through foreign keys. They express relationships like
# "Project has one Project Manager" or "Project belongs to a Portfolio". Each macro adds a number of methods to the class which are
# specialized according to the collection or association symbol and the options hash. It works much the same way as Ruby's own <tt>attr*</tt>
@@ -256,6 +268,10 @@ module ActiveRecord
# You can manipulate objects and associations before they are saved to the database, but there is some special behavior you should be
# aware of, mostly involving the saving of associated objects.
#
+ # Unless you enable the :autosave option on a <tt>has_one</tt>, <tt>belongs_to</tt>,
+ # <tt>has_many</tt>, or <tt>has_and_belongs_to_many</tt> association,
+ # in which case the members are always saved.
+ #
# === One-to-one associations
#
# * Assigning an object to a +has_one+ association automatically saves that object and the object being replaced (if there is one), in
@@ -752,6 +768,9 @@ module ActiveRecord
# If true, all the associated objects are readonly through the association.
# [:validate]
# If false, don't validate the associated objects when saving the parent object. true by default.
+ # [:autosave]
+ # If true, always save any loaded members and destroy members marked for destruction, when saving the parent object. Off by default.
+ #
# Option examples:
# has_many :comments, :order => "posted_on"
# has_many :comments, :include => :author
@@ -865,6 +884,8 @@ module ActiveRecord
# If true, the associated object is readonly through the association.
# [:validate]
# If false, don't validate the associated object when saving the parent object. +false+ by default.
+ # [:autosave]
+ # If true, always save the associated object or destroy it if marked for destruction, when saving the parent object. Off by default.
#
# Option examples:
# has_one :credit_card, :dependent => :destroy # destroys the associated credit card
@@ -882,13 +903,10 @@ module ActiveRecord
else
reflection = create_has_one_reflection(association_id, options)
- ivar = "@#{reflection.name}"
-
method_name = "has_one_after_save_for_#{reflection.name}".to_sym
define_method(method_name) do
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
-
- if !association.nil? && (new_record? || association.new_record? || association[reflection.primary_key_name] != id)
+ association = association_instance_get(reflection.name)
+ if association && (new_record? || association.new_record? || association[reflection.primary_key_name] != id)
association[reflection.primary_key_name] = id
association.save(true)
end
@@ -979,6 +997,8 @@ module ActiveRecord
# If true, the associated object is readonly through the association.
# [:validate]
# If false, don't validate the associated objects when saving the parent object. +false+ by default.
+ # [:autosave]
+ # If true, always save the associated object or destroy it if marked for destruction, when saving the parent object. Off by default.
#
# Option examples:
# belongs_to :firm, :foreign_key => "client_of"
@@ -991,15 +1011,12 @@ module ActiveRecord
def belongs_to(association_id, options = {})
reflection = create_belongs_to_reflection(association_id, options)
- ivar = "@#{reflection.name}"
-
if reflection.options[:polymorphic]
association_accessor_methods(reflection, BelongsToPolymorphicAssociation)
method_name = "polymorphic_belongs_to_before_save_for_#{reflection.name}".to_sym
define_method(method_name) do
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
-
+ association = association_instance_get(reflection.name)
if association && association.target
if association.new_record?
association.save(true)
@@ -1019,9 +1036,7 @@ module ActiveRecord
method_name = "belongs_to_before_save_for_#{reflection.name}".to_sym
define_method(method_name) do
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
-
- if !association.nil?
+ if association = association_instance_get(reflection.name)
if association.new_record?
association.save(true)
end
@@ -1196,6 +1211,8 @@ module ActiveRecord
# If true, all the associated objects are readonly through the association.
# [:validate]
# If false, don't validate the associated objects when saving the parent object. +true+ by default.
+ # [:autosave]
+ # If true, always save any loaded members and destroy members marked for destruction, when saving the parent object. Off by default.
#
# Option examples:
# has_and_belongs_to_many :projects
@@ -1243,33 +1260,30 @@ module ActiveRecord
end
def association_accessor_methods(reflection, association_proxy_class)
- ivar = "@#{reflection.name}"
-
define_method(reflection.name) do |*params|
force_reload = params.first unless params.empty?
-
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ association = association_instance_get(reflection.name)
if association.nil? || force_reload
association = association_proxy_class.new(self, reflection)
retval = association.reload
if retval.nil? and association_proxy_class == BelongsToAssociation
- instance_variable_set(ivar, nil)
+ association_instance_set(reflection.name, nil)
return nil
end
- instance_variable_set(ivar, association)
+ association_instance_set(reflection.name, association)
end
association.target.nil? ? nil : association
end
define_method("loaded_#{reflection.name}?") do
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ association = association_instance_get(reflection.name)
association && association.loaded?
end
define_method("#{reflection.name}=") do |new_value|
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ association = association_instance_get(reflection.name)
if association.nil? || association.target != new_value
association = association_proxy_class.new(self, reflection)
@@ -1280,7 +1294,7 @@ module ActiveRecord
self.send(reflection.name, new_value)
else
association.replace(new_value)
- instance_variable_set(ivar, new_value.nil? ? nil : association)
+ association_instance_set(reflection.name, new_value.nil? ? nil : association)
end
end
@@ -1288,20 +1302,18 @@ module ActiveRecord
return if target.nil? and association_proxy_class == BelongsToAssociation
association = association_proxy_class.new(self, reflection)
association.target = target
- instance_variable_set(ivar, association)
+ association_instance_set(reflection.name, association)
end
end
def collection_reader_method(reflection, association_proxy_class)
define_method(reflection.name) do |*params|
- ivar = "@#{reflection.name}"
-
force_reload = params.first unless params.empty?
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ association = association_instance_get(reflection.name)
- unless association.respond_to?(:loaded?)
+ unless association
association = association_proxy_class.new(self, reflection)
- instance_variable_set(ivar, association)
+ association_instance_set(reflection.name, association)
end
association.reload if force_reload
@@ -1339,8 +1351,7 @@ module ActiveRecord
def add_single_associated_validation_callbacks(association_name)
method_name = "validate_associated_records_for_#{association_name}".to_sym
define_method(method_name) do
- association = instance_variable_get("@#{association_name}")
- if !association.nil?
+ if association = association_instance_get(association_name)
errors.add association_name unless association.target.nil? || association.valid?
end
end
@@ -1350,12 +1361,10 @@ module ActiveRecord
def add_multiple_associated_validation_callbacks(association_name)
method_name = "validate_associated_records_for_#{association_name}".to_sym
- ivar = "@#{association_name}"
-
define_method(method_name) do
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ association = association_instance_get(association_name)
- if association.respond_to?(:loaded?)
+ if association
if new_record?
association
elsif association.loaded?
@@ -1372,8 +1381,6 @@ module ActiveRecord
end
def add_multiple_associated_save_callbacks(association_name)
- ivar = "@#{association_name}"
-
method_name = "before_save_associated_records_for_#{association_name}".to_sym
define_method(method_name) do
@new_record_before_save = new_record?
@@ -1383,13 +1390,13 @@ module ActiveRecord
method_name = "after_create_or_update_associated_records_for_#{association_name}".to_sym
define_method(method_name) do
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ association = association_instance_get(association_name)
records_to_save = if @new_record_before_save
association
- elsif association.respond_to?(:loaded?) && association.loaded?
+ elsif association && association.loaded?
association.select { |record| record.new_record? }
- elsif association.respond_to?(:loaded?) && !association.loaded?
+ elsif association && !association.loaded?
association.target.select { |record| record.new_record? }
else
[]
@@ -1407,15 +1414,13 @@ module ActiveRecord
def association_constructor_method(constructor, reflection, association_proxy_class)
define_method("#{constructor}_#{reflection.name}") do |*params|
- ivar = "@#{reflection.name}"
-
attributees = params.first unless params.empty?
replace_existing = params[1].nil? ? true : params[1]
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ association = association_instance_get(reflection.name)
- if association.nil?
+ unless association
association = association_proxy_class.new(self, reflection)
- instance_variable_set(ivar, association)
+ association_instance_set(reflection.name, association)
end
if association_proxy_class == HasOneAssociation
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
new file mode 100644
index 0000000000..07660ebd03
--- /dev/null
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -0,0 +1,213 @@
+module ActiveRecord
+ # AutosaveAssociation is a module that takes care of automatically saving
+ # your associations when the parent is saved. In addition to saving, it
+ # also destroys any associations that were marked for destruction.
+ # (See mark_for_destruction and marked_for_destruction?)
+ #
+ # Saving of the parent, its associations, and the destruction of marked
+ # associations, all happen inside 1 transaction. This should never leave the
+ # database in an inconsistent state after, for instance, mass assigning
+ # attributes and saving them.
+ #
+ # If validations for any of the associations fail, their error messages will
+ # be applied to the parent.
+ #
+ # Note that it also means that associations marked for destruction won't
+ # be destroyed directly. They will however still be marked for destruction.
+ #
+ # === One-to-one Example
+ #
+ # Consider a Post model with one Author:
+ #
+ # class Post
+ # has_one :author, :autosave => true
+ # end
+ #
+ # Saving changes to the parent and its associated model can now be performed
+ # automatically _and_ atomically:
+ #
+ # post = Post.find(1)
+ # post.title # => "The current global position of migrating ducks"
+ # post.author.name # => "alloy"
+ #
+ # post.title = "On the migration of ducks"
+ # post.author.name = "Eloy Duran"
+ #
+ # post.save
+ # post.reload
+ # post.title # => "On the migration of ducks"
+ # post.author.name # => "Eloy Duran"
+ #
+ # Destroying an associated model, as part of the parent's save action, is as
+ # simple as marking it for destruction:
+ #
+ # post.author.mark_for_destruction
+ # post.author.marked_for_destruction? # => true
+ #
+ # Note that the model is _not_ yet removed from the database:
+ # id = post.author.id
+ # Author.find_by_id(id).nil? # => false
+ #
+ # post.save
+ # post.reload.author # => nil
+ #
+ # Now it _is_ removed from the database:
+ # Author.find_by_id(id).nil? # => true
+ #
+ # === One-to-many Example
+ #
+ # Consider a Post model with many Comments:
+ #
+ # class Post
+ # has_many :comments, :autosave => true
+ # end
+ #
+ # Saving changes to the parent and its associated model can now be performed
+ # automatically _and_ atomically:
+ #
+ # post = Post.find(1)
+ # post.title # => "The current global position of migrating ducks"
+ # post.comments.first.body # => "Wow, awesome info thanks!"
+ # post.comments.last.body # => "Actually, your article should be named differently."
+ #
+ # post.title = "On the migration of ducks"
+ # post.comments.last.body = "Actually, your article should be named differently. [UPDATED]: You are right, thanks."
+ #
+ # post.save
+ # post.reload
+ # post.title # => "On the migration of ducks"
+ # post.comments.last.body # => "Actually, your article should be named differently. [UPDATED]: You are right, thanks."
+ #
+ # Destroying one of the associated models members, as part of the parent's
+ # save action, is as simple as marking it for destruction:
+ #
+ # post.comments.last.mark_for_destruction
+ # post.comments.last.marked_for_destruction? # => true
+ # post.comments.length # => 2
+ #
+ # Note that the model is _not_ yet removed from the database:
+ # id = post.comments.last.id
+ # Comment.find_by_id(id).nil? # => false
+ #
+ # post.save
+ # post.reload.comments.length # => 1
+ #
+ # Now it _is_ removed from the database:
+ # Comment.find_by_id(id).nil? # => true
+ #
+ # === Validation
+ #
+ # Validation is performed on the parent as usual, but also on all autosave
+ # enabled associations. If any of the associations fail validation, its
+ # error messages will be applied on the parents errors object and validation
+ # of the parent will fail.
+ #
+ # Consider a Post model with Author which validates the presence of its name
+ # attribute:
+ #
+ # class Post
+ # has_one :author, :autosave => true
+ # end
+ #
+ # class Author
+ # validates_presence_of :name
+ # end
+ #
+ # post = Post.find(1)
+ # post.author.name = ''
+ # post.save # => false
+ # post.errors # => #<ActiveRecord::Errors:0x174498c @errors={"author_name"=>["can't be blank"]}, @base=#<Post ...>>
+ #
+ # No validations will be performed on the associated models when validations
+ # are skipped for the parent:
+ #
+ # post = Post.find(1)
+ # post.author.name = ''
+ # post.save(false) # => true
+ module AutosaveAssociation
+ def self.included(base)
+ base.class_eval do
+ alias_method_chain :reload, :autosave_associations
+ alias_method_chain :save, :autosave_associations
+ alias_method_chain :valid?, :autosave_associations
+
+ %w{ has_one belongs_to has_many has_and_belongs_to_many }.each do |type|
+ base.send("valid_keys_for_#{type}_association") << :autosave
+ end
+ end
+ end
+
+ # Saves the parent, <tt>self</tt>, and any loaded autosave associations.
+ # In addition, it destroys all children that were marked for destruction
+ # with mark_for_destruction.
+ #
+ # This all happens inside a transaction, _if_ the Transactions module is included into
+ # ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
+ def save_with_autosave_associations(perform_validation = true)
+ returning(save_without_autosave_associations(perform_validation)) do |valid|
+ if valid
+ self.class.reflect_on_all_autosave_associations.each do |reflection|
+ if (association = association_instance_get(reflection.name)) && association.loaded?
+ if association.is_a?(Array)
+ association.proxy_target.each do |child|
+ child.marked_for_destruction? ? child.destroy : child.save(perform_validation)
+ end
+ else
+ association.marked_for_destruction? ? association.destroy : association.save(perform_validation)
+ end
+ end
+ end
+ end
+ end
+ end
+
+ # Returns whether or not the parent, <tt>self</tt>, and any loaded autosave associations are valid.
+ def valid_with_autosave_associations?
+ if valid_without_autosave_associations?
+ self.class.reflect_on_all_autosave_associations.all? do |reflection|
+ if (association = association_instance_get(reflection.name)) && association.loaded?
+ if association.is_a?(Array)
+ association.proxy_target.all? { |child| autosave_association_valid?(reflection, child) }
+ else
+ autosave_association_valid?(reflection, association)
+ end
+ else
+ true # association not loaded yet, so it should be valid
+ end
+ end
+ else
+ false # self was not valid
+ end
+ end
+
+ # Returns whether or not the association is valid and applies any errors to the parent, <tt>self</tt>, if it wasn't.
+ def autosave_association_valid?(reflection, association)
+ returning(association.valid?) do |valid|
+ association.errors.each do |attribute, message|
+ errors.add "#{reflection.name}_#{attribute}", message
+ end unless valid
+ end
+ end
+
+ # Reloads the attributes of the object as usual and removes a mark for destruction.
+ def reload_with_autosave_associations(options = nil)
+ @marked_for_destruction = false
+ reload_without_autosave_associations(options)
+ end
+
+ # Marks this record to be destroyed as part of the parents save transaction.
+ # This does _not_ actually destroy the record yet, rather it will be destroyed when <tt>parent.save</tt> is called.
+ #
+ # Only useful if the <tt>:autosave</tt> option on the parent is enabled for this associated model.
+ def mark_for_destruction
+ @marked_for_destruction = true
+ end
+
+ # Returns whether or not this record will be destroyed as part of the parents save transaction.
+ #
+ # Only useful if the <tt>:autosave</tt> option on the parent is enabled for this associated model.
+ def marked_for_destruction?
+ @marked_for_destruction
+ end
+ end
+end \ No newline at end of file
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 0efccb66ee..f9168c8dc2 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -3136,6 +3136,11 @@ module ActiveRecord #:nodoc:
include Dirty
include Callbacks, Observing, Timestamp
include Associations, AssociationPreload, NamedScope
+
+ # AutosaveAssociation needs to be included before Transactions, because we want
+ # #save_with_autosave_associations to be wrapped inside a transaction.
+ include AutosaveAssociation, NestedAttributes
+
include Aggregations, Transactions, Reflection, Calculations, Serialization
end
end
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
new file mode 100644
index 0000000000..8bfdadd0e3
--- /dev/null
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -0,0 +1,279 @@
+module ActiveRecord
+ module NestedAttributes #:nodoc:
+ def self.included(base)
+ base.extend(ClassMethods)
+ base.class_inheritable_accessor :reject_new_nested_attributes_procs, :instance_writer => false
+ base.reject_new_nested_attributes_procs = {}
+ end
+
+ # == Nested Attributes
+ #
+ # Nested attributes allow you to save attributes on associated records
+ # through the parent. By default nested attribute updating is turned off,
+ # you can enable it using the accepts_nested_attributes_for class method.
+ # When you enable nested attributes an attribute writer is defined on
+ # the model.
+ #
+ # The attribute writer is named after the association, which means that
+ # in the following example, two new methods are added to your model:
+ # <tt>author_attributes=(attributes)</tt> and
+ # <tt>pages_attributes=(attributes)</tt>.
+ #
+ # class Book < ActiveRecord::Base
+ # has_one :author
+ # has_many :pages
+ #
+ # accepts_nested_attributes_for :author, :pages
+ # end
+ #
+ # Note that the <tt>:autosave</tt> option is automatically enabled on every
+ # association that accepts_nested_attributes_for is used for.
+ #
+ # === One-to-one
+ #
+ # Consider a Member model that has one Avatar:
+ #
+ # class Member < ActiveRecord::Base
+ # has_one :avatar
+ # accepts_nested_attributes_for :avatar
+ # end
+ #
+ # Enabling nested attributes on a one-to-one association allows you to
+ # create the member and avatar in one go:
+ #
+ # params = { 'member' => { 'name' => 'Jack', 'avatar_attributes' => { 'icon' => 'smiling' } } }
+ # member = Member.create(params)
+ # member.avatar.icon #=> 'smiling'
+ #
+ # It also allows you to update the avatar through the member:
+ #
+ # params = { 'member' => { 'avatar_attributes' => { 'icon' => 'sad' } } }
+ # member.update_attributes params['member']
+ # member.avatar.icon #=> 'sad'
+ #
+ # By default you will only be able to set and update attributes on the
+ # associated model. If you want to destroy the associated model through the
+ # attributes hash, you have to enable it first using the
+ # <tt>:allow_destroy</tt> option.
+ #
+ # class Member < ActiveRecord::Base
+ # has_one :avatar
+ # accepts_nested_attributes_for :avatar, :allow_destroy => true
+ # end
+ #
+ # Now, when you add the <tt>_delete</tt> key to the attributes hash, with a
+ # value that evaluates to +true+, you will destroy the associated model:
+ #
+ # member.avatar_attributes = { '_delete' => '1' }
+ # member.avatar.marked_for_destruction? # => true
+ # member.save
+ # member.avatar #=> nil
+ #
+ # Note that the model will _not_ be destroyed until the parent is saved.
+ #
+ # === One-to-many
+ #
+ # Consider a member that has a number of posts:
+ #
+ # class Member < ActiveRecord::Base
+ # has_many :posts
+ # accepts_nested_attributes_for :posts, :reject_if => proc { |attributes| attributes['title'].blank? }
+ # end
+ #
+ # You can now set or update attributes on an associated post model through
+ # the attribute hash.
+ #
+ # For each key in the hash that starts with the string 'new' a new model
+ # will be instantiated. When the proc given with the <tt>:reject_if</tt>
+ # option evaluates to +false+ for a certain attribute hash no record will
+ # be built for that hash.
+ #
+ # params = { 'member' => {
+ # 'name' => 'joe', 'posts_attributes' => {
+ # 'new_12345' => { 'title' => 'Kari, the awesome Ruby documentation browser!' },
+ # 'new_54321' => { 'title' => 'The egalitarian assumption of the modern citizen' },
+ # 'new_67890' => { 'title' => '' } # This one matches the :reject_if proc and will not be instantiated.
+ # }
+ # }}
+ #
+ # member = Member.create(params['member'])
+ # member.posts.length #=> 2
+ # member.posts.first.title #=> 'Kari, the awesome Ruby documentation browser!'
+ # member.posts.second.title #=> 'The egalitarian assumption of the modern citizen'
+ #
+ # When the key for post attributes is an integer, the associated post with
+ # that ID will be updated:
+ #
+ # member.attributes = {
+ # 'name' => 'Joe',
+ # 'posts_attributes' => {
+ # '1' => { 'title' => '[UPDATED] An, as of yet, undisclosed awesome Ruby documentation browser!' },
+ # '2' => { 'title' => '[UPDATED] other post' }
+ # }
+ # }
+ #
+ # By default the associated models are protected from being destroyed. If
+ # you want to destroy any of the associated models through the attributes
+ # hash, you have to enable it first using the <tt>:allow_destroy</tt>
+ # option.
+ #
+ # This will allow you to specify which models to destroy in the attributes
+ # hash by setting the '_delete' attribute to a value that evaluates to
+ # +true+:
+ #
+ # class Member < ActiveRecord::Base
+ # has_many :posts
+ # accepts_nested_attributes_for :posts, :allow_destroy => true
+ # end
+ #
+ # params = {'member' => { 'name' => 'joe', 'posts_attributes' => {
+ # '2' => { '_delete' => '1' }
+ # }}}
+ # member.attributes = params['member']
+ # member.posts.detect { |p| p.id == 2 }.marked_for_destruction? # => true
+ # member.posts.length #=> 2
+ # member.save
+ # member.posts.length # => 1
+ #
+ # === Saving
+ #
+ # All changes to models, including the destruction of those marked for
+ # destruction, are saved and destroyed automatically and atomically when
+ # the parent model is saved. This happens inside the transaction initiated
+ # by the parents save method. See ActiveRecord::AutosaveAssociation.
+ module ClassMethods
+ # Defines an attributes writer for the specified association(s).
+ #
+ # Supported options:
+ # [:allow_destroy]
+ # If true, destroys any members from the attributes hash with a
+ # <tt>_delete</tt> key and a value that converts to +true+
+ # (eg. 1, '1', true, or 'true'). This option is off by default.
+ # [:reject_if]
+ # Allows you to specify a Proc that checks whether a record should be
+ # built for a certain attribute hash. The hash is passed to the Proc
+ # and the Proc should return either +true+ or +false+. When no Proc
+ # is specified a record will be built for all attribute hashes.
+ #
+ # Examples:
+ # accepts_nested_attributes_for :avatar
+ # accepts_nested_attributes_for :avatar, :allow_destroy => true
+ # accepts_nested_attributes_for :avatar, :reject_if => proc { ... }
+ # accepts_nested_attributes_for :avatar, :posts, :allow_destroy => true, :reject_if => proc { ... }
+ def accepts_nested_attributes_for(*attr_names)
+ options = { :allow_destroy => false }
+ options.update(attr_names.extract_options!)
+ options.assert_valid_keys(:allow_destroy, :reject_if)
+
+ attr_names.each do |association_name|
+ if reflection = reflect_on_association(association_name)
+ type = case reflection.macro
+ when :has_one, :belongs_to
+ :one_to_one
+ when :has_many, :has_and_belongs_to_many
+ :collection
+ end
+
+ reflection.options[:autosave] = true
+ self.reject_new_nested_attributes_procs[association_name.to_sym] = options[:reject_if]
+
+ # def pirate_attributes=(attributes)
+ # assign_nested_attributes_for_one_to_one_association(:pirate, attributes, false)
+ # end
+ class_eval %{
+ def #{association_name}_attributes=(attributes)
+ assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes, #{options[:allow_destroy]})
+ end
+ }, __FILE__, __LINE__
+ else
+ raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?"
+ end
+ end
+ end
+ end
+
+ # Returns ActiveRecord::AutosaveAssociation::marked_for_destruction?
+ # It's used in conjunction with fields_for to build a form element
+ # for the destruction of this association.
+ #
+ # See ActionView::Helpers::FormHelper::fields_for for more info.
+ def _delete
+ marked_for_destruction?
+ end
+
+ private
+
+ # Assigns the given attributes to the association. An association will be
+ # build if it doesn't exist yet.
+ def assign_nested_attributes_for_one_to_one_association(association_name, attributes, allow_destroy)
+ if should_destroy_nested_attributes_record?(allow_destroy, attributes)
+ send(association_name).mark_for_destruction
+ else
+ (send(association_name) || send("build_#{association_name}")).attributes = attributes
+ end
+ end
+
+ # Assigns the given attributes to the collection association.
+ #
+ # Keys containing an ID for an associated record will update that record.
+ # Keys starting with <tt>new</tt> will instantiate a new record for that
+ # association.
+ #
+ # For example:
+ #
+ # assign_nested_attributes_for_collection_association(:people, {
+ # '1' => { 'name' => 'Peter' },
+ # 'new_43' => { 'name' => 'John' }
+ # })
+ #
+ # Will update the name of the Person with ID 1 and create a new associated
+ # person with the name 'John'.
+ def assign_nested_attributes_for_collection_association(association_name, attributes, allow_destroy)
+ unless attributes.is_a?(Hash)
+ raise ArgumentError, "Hash expected, got #{attributes.class.name} (#{attributes.inspect})"
+ end
+
+ # Make sure any new records sorted by their id before they're build.
+ sorted_by_id = attributes.sort_by { |id, _| id.is_a?(String) ? id.sub(/^new_/, '').to_i : id }
+
+ sorted_by_id.each do |id, record_attributes|
+ if id.acts_like?(:string) && id.starts_with?('new_')
+ build_new_nested_attributes_record(association_name, record_attributes)
+ else
+ assign_to_or_destroy_nested_attributes_record(association_name, id, record_attributes, allow_destroy)
+ end
+ end
+ end
+
+ # Returns +true+ if <tt>allow_destroy</tt> is enabled and the attributes
+ # contains a truthy value for the key <tt>'_delete'</tt>.
+ #
+ # It will _always_ remove the <tt>'_delete'</tt> key, if present.
+ def should_destroy_nested_attributes_record?(allow_destroy, attributes)
+ ConnectionAdapters::Column.value_to_boolean(attributes.delete('_delete')) && allow_destroy
+ end
+
+ # Builds a new record with the given attributes.
+ #
+ # If a <tt>:reject_if</tt> proc exists for this association, it will be
+ # called with the attributes as its argument. If the proc returns a truthy
+ # value, the record is _not_ build.
+ def build_new_nested_attributes_record(association_name, attributes)
+ if reject_proc = self.class.reject_new_nested_attributes_procs[association_name]
+ return if reject_proc.call(attributes)
+ end
+ send(association_name).build(attributes)
+ end
+
+ # Assigns the attributes to the record specified by +id+. Or marks it for
+ # destruction if #should_destroy_nested_attributes_record? returns +true+.
+ def assign_to_or_destroy_nested_attributes_record(association_name, id, attributes, allow_destroy)
+ record = send(association_name).detect { |record| record.id == id.to_i }
+ if should_destroy_nested_attributes_record?(allow_destroy, attributes)
+ record.mark_for_destruction
+ else
+ record.attributes = attributes
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index 1937abdc83..e69bfb1355 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -65,6 +65,11 @@ module ActiveRecord
def reflect_on_association(association)
reflections[association].is_a?(AssociationReflection) ? reflections[association] : nil
end
+
+ # Returns an array of AssociationReflection objects for all associations which have <tt>:autosave</tt> enabled.
+ def reflect_on_all_autosave_associations
+ reflections.values.select { |reflection| reflection.options[:autosave] }
+ end
end
diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb
index 149b93203e..211dd78874 100644
--- a/activerecord/lib/active_record/test_case.rb
+++ b/activerecord/lib/active_record/test_case.rb
@@ -27,6 +27,7 @@ module ActiveRecord
$queries_executed = []
yield
ensure
+ %w{ BEGIN COMMIT }.each { |x| $queries_executed.delete(x) }
assert_equal num, $queries_executed.size, "#{$queries_executed.size} instead of #{num} queries were executed.#{$queries_executed.size == 0 ? '' : "\nQueries:\n#{$queries_executed.join("\n")}"}"
end