From 3b9e90a4dab94d25bfa7ed5e47ca0641857ef992 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 12 Apr 2005 05:34:10 +0000 Subject: Moved build_association and create_association for has_one and belongs_to out of deprecation as they work when the association is nil unlike association.build and association.create, which require the association to be already in place #864 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1146 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 43 ++++++++++++++++------ .../associations/belongs_to_association.rb | 1 - .../lib/active_record/deprecated_associations.rb | 20 +--------- 3 files changed, 32 insertions(+), 32 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 9253abd14d..1b78b02e84 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -163,9 +163,11 @@ module ActiveRecord # * collection.find_all(conditions = nil, orderings = nil, limit = nil, joins = nil) - finds all associated objects responding # criteria mentioned (like in the standard find_all) and that meets the condition that it has to be associated with this object. # * collection.build(attributes = {}) - returns a new object of the collection type that has been instantiated - # with +attributes+ and linked to this object through a foreign key but has not yet been saved. + # with +attributes+ and linked to this object through a foreign key but has not yet been saved. *Note:* This only works if an + # associated object already exists, not if its nil! # * collection.create(attributes = {}) - returns a new object of the collection type that has been instantiated # with +attributes+ and linked to this object through a foreign key and that has already been saved (if it passed the validation). + # *Note:* This only works if an associated object already exists, not if its nil! # # Example: A Firm class declares has_many :clients, which will add: # * Firm#clients (similar to Clients.find_all "firm_id = #{id}") @@ -250,19 +252,19 @@ module ActiveRecord # * association=(associate) - assigns the associate object, extracts the primary key, sets it as the foreign key, # and saves the associate object. # * association.nil? - returns true if there is no associated object. - # * association.build(attributes = {}) - returns a new object of the associated type that has been instantiated + # * build_association(attributes = {}) - returns a new object of the associated type that has been instantiated # with +attributes+ and linked to this object through a foreign key but has not yet been saved. Note: This ONLY works if # an association already exists. It will NOT work if the association is nil. - # * association.create(attributes = {}) - returns a new object of the associated type that has been instantiated + # * create_association(attributes = {}) - returns a new object of the associated type that has been instantiated # with +attributes+ and linked to this object through a foreign key and that has already been saved (if it passed the validation). - # Note: This ONLY works if an association already exists. It will NOT work if the association is nil. # # Example: An Account class declares has_one :beneficiary, which will add: # * Account#beneficiary (similar to Beneficiary.find_first "account_id = #{id}") # * Account#beneficiary=(beneficiary) (similar to beneficiary.account_id = account.id; beneficiary.save) # * Account#beneficiary.nil? - # * Account#beneficiary.build (similar to Beneficiary.new("account_id" => id)) - # * Account#beneficiary.create (similar to b = Beneficiary.new("account_id" => id); b.save; b) + # * Account#build_beneficiary (similar to Beneficiary.new("account_id" => id)) + # * Account#create_beneficiary (similar to b = Beneficiary.new("account_id" => id); b.save; b) + # # The declaration can also include an options hash to specialize the behavior of the association. # # Options are: @@ -303,13 +305,13 @@ module ActiveRecord end association_accessor_methods(association_name, association_class_name, association_class_primary_key_name, options, HasOneAssociation) + association_constructor_method(:build, association_name, association_class_name, association_class_primary_key_name, options, HasOneAssociation) + association_constructor_method(:create, association_name, association_class_name, association_class_primary_key_name, options, HasOneAssociation) module_eval "before_destroy '#{association_name}.destroy unless #{association_name}.nil?'" if options[:dependent] # deprecated api deprecated_has_association_method(association_name) - deprecated_build_method("build_", association_name, association_class_name, association_class_primary_key_name) - deprecated_create_method("create_", association_name, association_class_name, association_class_primary_key_name) deprecated_association_comparison_method(association_name, association_class_name) end @@ -319,9 +321,9 @@ module ActiveRecord # * association(force_reload = false) - returns the associated object. Nil is returned if none is found. # * association=(associate) - assigns the associate object, extracts the primary key, and sets it as the foreign key. # * association.nil? - returns true if there is no associated object. - # * association.build(attributes = {}) - returns a new object of the associated type that has been instantiated + # * build_association(attributes = {}) - returns a new object of the associated type that has been instantiated # with +attributes+ and linked to this object through a foreign key but has not yet been saved. - # * association.create(attributes = {}) - returns a new object of the associated type that has been instantiated + # * create_association(attributes = {}) - returns a new object of the associated type that has been instantiated # with +attributes+ and linked to this object through a foreign key and that has already been saved (if it passed the validation). # # Example: A Post class declares belongs_to :author, which will add: @@ -329,8 +331,8 @@ module ActiveRecord # * Post#author=(author) (similar to post.author_id = author.id) # * Post#author? (similar to post.author == some_author) # * Post#author.nil? - # * Post#author.build (similar to Author.new("post_id" => id)) - # * Post#author.create (similar to b = Author.new("post_id" => id); b.save; b) + # * Post#build_author (similar to Author.new("post_id" => id)) + # * Post#create_author (similar to b = Author.new("post_id" => id); b.save; b) # The declaration can also include an options hash to specialize the behavior of the association. # # Options are: @@ -365,6 +367,8 @@ module ActiveRecord association_class_primary_key_name = options[:foreign_key] || Inflector.underscore(Inflector.demodulize(association_class_name)) + "_id" association_accessor_methods(association_name, association_class_name, association_class_primary_key_name, options, BelongsToAssociation) + association_constructor_method(:build, association_name, association_class_name, association_class_primary_key_name, options, BelongsToAssociation) + association_constructor_method(:create, association_name, association_class_name, association_class_primary_key_name, options, BelongsToAssociation) module_eval do before_save <<-EOF @@ -626,6 +630,21 @@ module ActiveRecord end end + def association_constructor_method(constructor, association_name, association_class_name, association_class_primary_key_name, options, association_proxy_class) + define_method("#{constructor}_#{association_name}") do |*params| + attributees = params.first unless params.empty? + association = instance_variable_get("@#{association_name}") + + if association.nil? + association = association_proxy_class.new(self, + association_name, association_class_name, + association_class_primary_key_name, options) + instance_variable_set("@#{association_name}", association) + end + + association.send(constructor, attributees) + end + end def find_with_associations(options = {}) reflections = reflect_on_included_associations(options[:include]) diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 521aff4058..b142a13153 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -1,7 +1,6 @@ module ActiveRecord module Associations class BelongsToAssociation < AssociationProxy #:nodoc: - def reset @target = nil @loaded = false diff --git a/activerecord/lib/active_record/deprecated_associations.rb b/activerecord/lib/active_record/deprecated_associations.rb index a3d0f1d947..077ac1de3b 100644 --- a/activerecord/lib/active_record/deprecated_associations.rb +++ b/activerecord/lib/active_record/deprecated_associations.rb @@ -84,25 +84,7 @@ module ActiveRecord !#{association_name}(force_reload).nil? end end_eval - end - - def deprecated_build_method(method_prefix, collection_name, collection_class_name, class_primary_key_name)# :nodoc: - module_eval <<-"end_eval", __FILE__, __LINE__ - def #{method_prefix + collection_name}(attributes = {}) - association = #{collection_class_name}.new - association.attributes = attributes.merge({ "#{class_primary_key_name}" => id}) - association - end - end_eval - end - - def deprecated_create_method(method_prefix, collection_name, collection_class_name, class_primary_key_name)# :nodoc: - module_eval <<-"end_eval", __FILE__, __LINE__ - def #{method_prefix + collection_name}(attributes = nil) - #{collection_class_name}.create((attributes || {}).merge({ "#{class_primary_key_name}" => id})) - end - end_eval - end + end end end end -- cgit v1.2.3