From 9d7e6432b22a11e96ad7a3cab7510fac7b3927d6 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 5 Sep 2006 18:48:10 +0000 Subject: Deprecated ActiveRecord::Base.new_record? in favor of ActiveRecord::Base.new? (old version still works until Rails 2.0) [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5017 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 + activerecord/lib/active_record/aggregations.rb | 2 +- activerecord/lib/active_record/associations.rb | 38 +++----- .../associations/association_collection.rb | 8 +- .../associations/association_proxy.rb | 6 +- .../associations/belongs_to_association.rb | 6 +- .../belongs_to_polymorphic_association.rb | 2 +- .../has_and_belongs_to_many_association.rb | 8 +- .../associations/has_many_association.rb | 4 +- .../associations/has_many_through_association.rb | 8 +- .../associations/has_one_association.rb | 10 +- activerecord/lib/active_record/base.rb | 20 +++- activerecord/lib/active_record/callbacks.rb | 4 +- .../lib/active_record/locking/pessimistic.rb | 2 +- activerecord/lib/active_record/validations.rb | 11 +-- activerecord/test/associations_test.rb | 107 ++++++++++----------- activerecord/test/base_test.rb | 14 +-- activerecord/test/finder_test.rb | 8 +- 18 files changed, 124 insertions(+), 136 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 2aaa62a7b5..745e06dac9 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Deprecated ActiveRecord::Base.new_record? in favor of ActiveRecord::Base.new? (old version still works until Rails 2.0) [DHH] + * Rename AR::Base#quote so people can use that name in their models. #3628 [Koz] * Add deprecation warning for inferred foreign key. #6029 [Josh Susser] diff --git a/activerecord/lib/active_record/aggregations.rb b/activerecord/lib/active_record/aggregations.rb index a137a11cd4..f7a7b2c98f 100644 --- a/activerecord/lib/active_record/aggregations.rb +++ b/activerecord/lib/active_record/aggregations.rb @@ -7,7 +7,7 @@ module ActiveRecord def clear_aggregation_cache #:nodoc: self.class.reflect_on_all_aggregations.to_a.each do |assoc| instance_variable_set "@#{assoc.name}", nil - end unless self.new_record? + end unless self.new? end # Active Record implements aggregation through a macro-like class method called +composed_of+ for representing attributes diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index e2dca89d49..4032ddcf6c 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -65,7 +65,7 @@ module ActiveRecord def clear_association_cache #:nodoc: self.class.reflect_on_all_associations.to_a.each do |assoc| instance_variable_set "@#{assoc.name}", nil - end unless self.new_record? + end unless self.new? end # Associations are a set of macro-like class methods for tying objects together through foreign keys. They express relationships like @@ -131,7 +131,7 @@ module ActiveRecord # === 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 - # order to update their primary keys - except if the parent object is unsaved (new_record? == true). + # order to update their primary keys - except if the parent object is unsaved (new? == true). # * If either of these saves fail (due to one of the objects being invalid) the assignment statement returns false and the assignment # is cancelled. # * If you wish to assign an object to a has_one association without saving it, use the #association.build method (documented below). @@ -144,7 +144,7 @@ module ActiveRecord # (the owner of the collection) is not yet stored in the database. # * If saving any of the objects being added to a collection (via #push or similar) fails, then #push returns false. # * You can add an object to a collection without automatically saving it by using the #collection.build method (documented below). - # * All unsaved (new_record? == true) members of the collection are automatically saved when the parent is saved. + # * All unsaved (new? == true) members of the collection are automatically saved when the parent is saved. # # === Association callbacks # @@ -591,7 +591,7 @@ module ActiveRecord module_eval do after_save <<-EOF association = instance_variable_get("@#{reflection.name}") - if !association.nil? && (new_record? || association.new_record? || association["#{reflection.primary_key_name}"] != id) + if !association.nil? && (new? || association.new? || association["#{reflection.primary_key_name}"] != id) association["#{reflection.primary_key_name}"] = id association.save(true) end @@ -655,12 +655,6 @@ module ActiveRecord # :conditions => 'discounts > #{payments_count}' # belongs_to :attachable, :polymorphic => true def belongs_to(association_id, options = {}) - if options.include?(:class_name) && !options.include?(:foreign_key) - ::ActiveSupport::Deprecation.warn( - "The inferred foreign_key name will change in Rails 2.0 to use the association name instead of its class name when they differ. When using :class_name in belongs_to, use the :foreign_key option to explicitly set the key name to avoid problems in the transition.", - caller) - end - reflection = create_belongs_to_reflection(association_id, options) if reflection.options[:polymorphic] @@ -670,7 +664,7 @@ module ActiveRecord before_save <<-EOF association = instance_variable_get("@#{reflection.name}") if association && association.target - if association.new_record? + if association.new? association.save(true) end @@ -690,7 +684,7 @@ module ActiveRecord before_save <<-EOF association = instance_variable_get("@#{reflection.name}") if !association.nil? - if association.new_record? + if association.new? association.save(true) end @@ -930,10 +924,10 @@ module ActiveRecord define_method(method_name) do association = instance_variable_get("@#{association_name}") if association.respond_to?(:loaded?) - if new_record? + if new? association else - association.select { |record| record.new_record? } + association.select { |record| record.new? } end.each do |record| errors.add "#{association_name}" unless record.valid? end @@ -941,7 +935,7 @@ module ActiveRecord end validate method_name - before_save("@new_record_before_save = new_record?; true") + before_save("@new_record_before_save = new?; true") after_callback = <<-end_eval association = instance_variable_get("@#{association_name}") @@ -950,7 +944,7 @@ module ActiveRecord if @new_record_before_save records_to_save = association else - records_to_save = association.select { |record| record.new_record? } + records_to_save = association.select { |record| record.new? } end records_to_save.each { |record| association.send(:insert_record, record) } association.send(:construct_sql) # reconstruct the SQL queries now that we know the owner's id @@ -997,7 +991,7 @@ module ActiveRecord if reflection.options[:exclusively_dependent] reflection.options[:dependent] = :delete_all - ::ActiveSupport::Deprecation.warn("The :exclusively_dependent option is deprecated and will be removed from Rails 2.0. Please use :dependent => :delete_all instead. See http://www.rubyonrails.org/deprecation for details.", caller) + #warn "The :exclusively_dependent option is deprecated. Please use :dependent => :delete_all instead.") end # See HasManyAssociation#delete_records. Dependent associations @@ -1457,7 +1451,7 @@ module ActiveRecord table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, polymorphic_foreign_key, parent.aliased_table_name, parent.primary_key, - aliased_join_table_name, polymorphic_foreign_type, klass.quote_value(parent.active_record.base_class.name)] + + aliased_join_table_name, polymorphic_foreign_type, klass.quote(parent.active_record.base_class.name)] + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [table_name_and_alias, aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key ] @@ -1472,7 +1466,7 @@ module ActiveRecord aliased_table_name, "#{source_reflection.options[:as]}_id", aliased_join_table_name, options[:foreign_key] || primary_key, aliased_table_name, "#{source_reflection.options[:as]}_type", - klass.quote_value(source_reflection.active_record.base_class.name) + klass.quote(source_reflection.active_record.base_class.name) ] else case source_reflection.macro @@ -1501,7 +1495,7 @@ module ActiveRecord aliased_table_name, "#{reflection.options[:as]}_id", parent.aliased_table_name, parent.primary_key, aliased_table_name, "#{reflection.options[:as]}_type", - klass.quote_value(parent.active_record.base_class.name) + klass.quote(parent.active_record.base_class.name) ] when reflection.macro == :has_one && reflection.options[:as] " LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s " % [ @@ -1509,7 +1503,7 @@ module ActiveRecord aliased_table_name, "#{reflection.options[:as]}_id", parent.aliased_table_name, parent.primary_key, aliased_table_name, "#{reflection.options[:as]}_type", - klass.quote_value(reflection.active_record.base_class.name) + klass.quote(reflection.active_record.base_class.name) ] else foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key @@ -1530,7 +1524,7 @@ module ActiveRecord join << %(AND %s.%s = %s ) % [ aliased_table_name, reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column), - klass.quote_value(klass.name.demodulize)] unless klass.descends_from_active_record? + klass.quote(klass.name.demodulize)] unless klass.descends_from_active_record? join << "AND #{interpolate_sql(sanitize_sql(reflection.options[:conditions]))} " if reflection.options[:conditions] join end diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 552c1ed06e..97d0ec8ce8 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -23,7 +23,7 @@ module ActiveRecord flatten_deeper(records).each do |record| raise_on_type_mismatch(record) callback(:before_add, record) - result &&= insert_record(record) unless @owner.new_record? + result &&= insert_record(record) unless @owner.new? @target << record callback(:after_add, record) end @@ -51,7 +51,7 @@ module ActiveRecord def delete(*records) records = flatten_deeper(records) records.each { |record| raise_on_type_mismatch(record) } - records.reject! { |record| @target.delete(record) if record.new_record? } + records.reject! { |record| @target.delete(record) if record.new? } return if records.empty? @owner.transaction do @@ -91,7 +91,7 @@ module ActiveRecord attributes.collect { |attr| create(attr) } else record = build(attributes) - record.save unless @owner.new_record? + record.save unless @owner.new? record end end @@ -103,7 +103,7 @@ module ActiveRecord if loaded? && !@reflection.options[:uniq] @target.size elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array) - unsaved_records = Array(@target.detect { |r| r.new_record? }) + unsaved_records = Array(@target.detect { |r| r.new? }) unsaved_records.size + count_records else count_records diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index ac7188d712..5d36c22f66 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -99,10 +99,10 @@ module ActiveRecord def set_belongs_to_association_for(record) if @reflection.options[:as] - record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record? + record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new? record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s else - record[@reflection.primary_key_name] = @owner.id unless @owner.new_record? + record[@reflection.primary_key_name] = @owner.id unless @owner.new? end end @@ -125,7 +125,7 @@ module ActiveRecord end def load_target - if !loaded? and (!@owner.new_record? || foreign_key_present) + if !loaded? and (!@owner.new? || foreign_key_present) @target = find_target end diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 1752678cbd..52dd61a47e 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -13,7 +13,7 @@ module ActiveRecord counter_cache_name = @reflection.counter_cache_column if record.nil? - if counter_cache_name && @owner[counter_cache_name] && !@owner.new_record? + if counter_cache_name && @owner[counter_cache_name] && !@owner.new? @reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name] end @@ -21,13 +21,13 @@ module ActiveRecord else raise_on_type_mismatch(record) - if counter_cache_name && !@owner.new_record? + if counter_cache_name && !@owner.new? @reflection.klass.increment_counter(counter_cache_name, record.id) @reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name] end @target = (AssociationProxy === record ? record.target : record) - @owner[@reflection.primary_key_name] = record.id unless record.new_record? + @owner[@reflection.primary_key_name] = record.id unless record.new? @updated = true end diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb index 9549b959fc..0a71fa89c9 100644 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -7,7 +7,7 @@ module ActiveRecord else @target = (AssociationProxy === record ? record.target : record) - unless record.new_record? + unless record.new? @owner[@reflection.primary_key_name] = record.id @owner[@reflection.options[:foreign_type]] = record.class.base_class.name.to_s end diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index 8c0e50e38e..f10c5957f8 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -19,7 +19,7 @@ module ActiveRecord attributes.collect { |attr| create(attr) } else record = build(attributes) - insert_record(record) unless @owner.new_record? + insert_record(record) unless @owner.new? record end end @@ -75,7 +75,7 @@ module ActiveRecord join_attributes.each { |key, value| record[key.to_s] = value } callback(:before_add, record) - insert_record(record) unless @owner.new_record? + insert_record(record) unless @owner.new? @target << record callback(:after_add, record) @@ -101,7 +101,7 @@ module ActiveRecord end def insert_record(record) - if record.new_record? + if record.new? return false unless record.save end @@ -118,7 +118,7 @@ module ActiveRecord attributes[column.name] = record.quoted_id else if record.attributes.has_key?(column.name) - value = @owner.send(:quote_value, record[column.name], column) + value = @owner.send(:quote, record[column.name], column) attributes[column.name] = value unless value.nil? end end diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index d3fdcbe02f..d4c6bdcada 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -111,7 +111,7 @@ module ActiveRecord end def load_target - if !@owner.new_record? || foreign_key_present + if !@owner.new? || foreign_key_present begin if !loaded? if @target.is_a?(Array) && @target.any? @@ -184,7 +184,7 @@ module ActiveRecord when @reflection.options[:as] @finder_sql = "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " + - "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}" + "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}" @finder_sql << " AND (#{conditions})" if conditions else diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index d7ca7b5834..e232c650fa 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -49,7 +49,7 @@ module ActiveRecord def <<(*records) return if records.empty? through = @reflection.through_reflection - raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) if @owner.new_record? + raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) if @owner.new? load_target @@ -57,7 +57,7 @@ module ActiveRecord klass.transaction do flatten_deeper(records).each do |associate| raise_on_type_mismatch(associate) - raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) unless associate.respond_to?(:new_record?) && !associate.new_record? + raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) unless associate.respond_to?(:new?) && !associate.new? @owner.send(@reflection.through_reflection.name).proxy_target << klass.with_scope(:create => construct_join_attributes(associate)) { klass.create! } @target << associate @@ -127,7 +127,7 @@ module ActiveRecord def construct_quoted_owner_attributes(reflection) if as = reflection.options[:as] { "#{as}_id" => @owner.quoted_id, - "#{as}_type" => reflection.klass.quote_value( + "#{as}_type" => reflection.klass.quote( @owner.class.base_class.name.to_s, reflection.klass.columns_hash["#{as}_type"]) } else @@ -164,7 +164,7 @@ module ActiveRecord if @reflection.source_reflection.options[:as] polymorphic_join = "AND %s.%s = %s" % [ @reflection.table_name, "#{@reflection.source_reflection.options[:as]}_type", - @owner.class.quote_value(@reflection.through_reflection.klass.name) + @owner.class.quote(@reflection.through_reflection.klass.name) ] end end diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 4ccd725ebd..9e7f281744 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -18,7 +18,7 @@ module ActiveRecord if replace_existing replace(record, true) else - record[@reflection.primary_key_name] = @owner.id unless @owner.new_record? + record[@reflection.primary_key_name] = @owner.id unless @owner.new? self.target = record end @@ -30,11 +30,11 @@ module ActiveRecord unless @target.nil? if dependent? && !dont_save && @target != obj - @target.destroy unless @target.new_record? + @target.destroy unless @target.new? @owner.clear_association_cache else @target[@reflection.primary_key_name] = nil - @target.save unless @owner.new_record? || @target.new_record? + @target.save unless @owner.new? || @target.new? end end @@ -48,7 +48,7 @@ module ActiveRecord @loaded = true - unless @owner.new_record? or obj.nil? or dont_save + unless @owner.new? or obj.nil? or dont_save return (obj.save ? self : false) else return (obj.nil? ? nil : self) @@ -69,7 +69,7 @@ module ActiveRecord when @reflection.options[:as] @finder_sql = "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " + - "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}" + "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}" else @finder_sql = "#{@reflection.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index e9c9212560..72155546bd 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -183,7 +183,7 @@ module ActiveRecord #:nodoc: # # # No 'Winter' tag exists # winter = Tag.find_or_initialize_by_name("Winter") - # winter.new_record? # true + # winter.new? # true # # == Saving arrays, hashes, and other non-mappable objects in text columns # @@ -1518,9 +1518,19 @@ module ActiveRecord #:nodoc: end # Returns true if this object hasn't been saved yet -- that is, a record for the object doesn't exist yet. - def new_record? + def new? @new_record end + + # Deprecated alias for new? + def new_record? + ActiveSupport::Deprecation.warn( + "ActiveRecord::Base.new_record? has been deprecated and will be removed with Rails 2.0." + + "Please use ActiveRecord::Base.new? instead.", caller + ) + + new? + end # * No record exists: Creates a new record with values matching those of the object attributes. # * A record does exist: Updates the record with values matching those of the object attributes. @@ -1538,7 +1548,7 @@ module ActiveRecord #:nodoc: # Deletes the record in the database and freezes this instance to reflect that no changes should # be made (since they can't be persisted). def destroy - unless new_record? + unless new? connection.delete <<-end_sql, "#{self.class.name} Destroy" DELETE FROM #{self.class.table_name} WHERE #{self.class.primary_key} = #{quoted_id} @@ -1707,7 +1717,7 @@ module ActiveRecord #:nodoc: comparison_object.equal?(self) || (comparison_object.instance_of?(self.class) && comparison_object.id == id && - !comparison_object.new_record?) + !comparison_object.new?) end # Delegates to == @@ -1763,7 +1773,7 @@ module ActiveRecord #:nodoc: private def create_or_update - if new_record? then create else update end + if new? then create else update end true end diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb index bfe96fd2ed..dd92a03111 100755 --- a/activerecord/lib/active_record/callbacks.rb +++ b/activerecord/lib/active_record/callbacks.rb @@ -293,13 +293,13 @@ module ActiveRecord def valid_with_callbacks? #:nodoc: return false if callback(:before_validation) == false - if new_record? then result = callback(:before_validation_on_create) else result = callback(:before_validation_on_update) end + if new? then result = callback(:before_validation_on_create) else result = callback(:before_validation_on_update) end return false if result == false result = valid_without_callbacks? callback(:after_validation) - if new_record? then callback(:after_validation_on_create) else callback(:after_validation_on_update) end + if new? then callback(:after_validation_on_create) else callback(:after_validation_on_update) end return result end diff --git a/activerecord/lib/active_record/locking/pessimistic.rb b/activerecord/lib/active_record/locking/pessimistic.rb index caad760742..81146e0688 100644 --- a/activerecord/lib/active_record/locking/pessimistic.rb +++ b/activerecord/lib/active_record/locking/pessimistic.rb @@ -69,7 +69,7 @@ module ActiveRecord # or pass true for "FOR UPDATE" (the default, an exclusive row lock). Returns # the locked record. def lock!(lock = true) - reload(:lock => lock) unless new_record? + reload(:lock => lock) unless new? self end end diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index c9d4740670..cc372b5042 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -333,8 +333,7 @@ module ActiveRecord attr_accessor *(attr_names.map { |n| "#{n}_confirmation" }) validates_each(attr_names, configuration) do |record, attr_name, value| - confirm = record.send("#{attr_name}_confirmation") - record.errors.add(attr_name, configuration[:message]) unless value.nil? || value == confirm + record.errors.add(attr_name, configuration[:message]) unless record.send("#{attr_name}_confirmation").nil? or value == record.send("#{attr_name}_confirmation") end end @@ -375,10 +374,6 @@ module ActiveRecord # # The first_name attribute must be in the object and it cannot be blank. # - # If you want to validate the presence of a boolean field (where the real values are true and false), - # you will want to use validates_inclusion_of :field_name, :in => [true, false] - # This is due to the way Object#blank? handles boolean values. false.blank? # => true - # # Configuration options: # * message - A custom error message (default is: "can't be blank") # * on - Specifies when this validation is active (default is :save, other options :create, :update) @@ -543,7 +538,7 @@ module ActiveRecord condition_params << scope_value end end - unless record.new_record? + unless record.new? condition_sql << " AND #{record.class.table_name}.#{record.class.primary_key} <> ?" condition_params << record.send(:id) end @@ -777,7 +772,7 @@ module ActiveRecord run_validations(:validate) validate - if new_record? + if new? run_validations(:validate_on_create) validate_on_create else diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index d7997beaa3..5c6090833e 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -178,13 +178,13 @@ class HasOneAssociationsTest < Test::Unit::TestCase assert_equal num_accounts - 1, Account.count assert_equal [account_id], Account.destroyed_account_ids[firm.id] end - + def test_deprecated_exclusive_dependence assert_deprecated(/:exclusively_dependent.*:dependent => :delete_all/) do Firm.has_many :deprecated_exclusively_dependent_clients, :class_name => 'Client', :exclusively_dependent => true end end - + def test_exclusive_dependence num_accounts = Account.count firm = ExclusivelyDependentFirm.find(9) @@ -244,10 +244,10 @@ class HasOneAssociationsTest < Test::Unit::TestCase account = firm.account.build("credit_limit" => 1000) assert_equal account, firm.account - assert account.new_record? + assert account.new? assert firm.save assert_equal account, firm.account - assert !account.new_record? + assert !account.new? end def test_build_before_either_saved @@ -255,10 +255,10 @@ class HasOneAssociationsTest < Test::Unit::TestCase firm.account = account = Account.new("credit_limit" => 1000) assert_equal account, firm.account - assert account.new_record? + assert account.new? assert firm.save assert_equal account, firm.account - assert !account.new_record? + assert !account.new? end def test_failing_build_association @@ -295,7 +295,7 @@ class HasOneAssociationsTest < Test::Unit::TestCase def test_assignment_before_parent_saved firm = Firm.new("name" => "GlobalMegaCorp") firm.account = a = Account.find(1) - assert firm.new_record? + assert firm.new? assert_equal a, firm.account assert firm.save assert_equal a, firm.account @@ -305,7 +305,7 @@ class HasOneAssociationsTest < Test::Unit::TestCase def test_assignment_before_child_saved firm = Firm.find(1) firm.account = a = Account.new("credit_limit" => 1000) - assert !a.new_record? + assert !a.new? assert_equal a, firm.account assert_equal a, firm.account assert_equal a, firm.account(true) @@ -314,12 +314,12 @@ class HasOneAssociationsTest < Test::Unit::TestCase def test_assignment_before_either_saved firm = Firm.new("name" => "GlobalMegaCorp") firm.account = a = Account.new("credit_limit" => 1000) - assert firm.new_record? - assert a.new_record? + assert firm.new? + assert a.new? assert_equal a, firm.account assert firm.save - assert !firm.new_record? - assert !a.new_record? + assert !firm.new? + assert !a.new? assert_equal a, firm.account assert_equal a, firm.account(true) end @@ -350,13 +350,6 @@ class HasOneAssociationsTest < Test::Unit::TestCase end end - def test_deprecated_inferred_foreign_key - assert_not_deprecated { Company.belongs_to :firm } - assert_not_deprecated { Company.belongs_to :client, :foreign_key => "firm_id" } - assert_not_deprecated { Company.belongs_to :firm, :class_name => "Firm", :foreign_key => "client_of" } - assert_deprecated("inferred foreign_key name") { Company.belongs_to :client, :class_name => "Firm" } - end - end @@ -558,14 +551,14 @@ class HasManyAssociationsTest < Test::Unit::TestCase new_firm = Firm.new("name" => "A New Firm, Inc") new_firm.clients_of_firm.push Client.new("name" => "Natural Company") new_firm.clients_of_firm << (c = Client.new("name" => "Apple")) - assert new_firm.new_record? - assert c.new_record? + assert new_firm.new? + assert c.new? assert_equal 2, new_firm.clients_of_firm.size assert_equal no_of_firms, Firm.count # Firm was not saved to database. assert_equal no_of_clients, Client.count # Clients were not saved to database. assert new_firm.save - assert !new_firm.new_record? - assert !c.new_record? + assert !new_firm.new? + assert !c.new? assert_equal new_firm, c.firm assert_equal no_of_firms+1, Firm.count # Firm was saved to database. assert_equal no_of_clients+2, Client.count # Clients were saved to database. @@ -576,10 +569,10 @@ class HasManyAssociationsTest < Test::Unit::TestCase def test_invalid_adding firm = Firm.find(1) assert !(firm.clients_of_firm << c = Client.new) - assert c.new_record? + assert c.new? assert !firm.valid? assert !firm.save - assert c.new_record? + assert c.new? end def test_invalid_adding_before_save @@ -587,21 +580,21 @@ class HasManyAssociationsTest < Test::Unit::TestCase no_of_clients = Client.count new_firm = Firm.new("name" => "A New Firm, Inc") new_firm.clients_of_firm.concat([c = Client.new, Client.new("name" => "Apple")]) - assert c.new_record? + assert c.new? assert !c.valid? assert !new_firm.valid? assert !new_firm.save - assert c.new_record? - assert new_firm.new_record? + assert c.new? + assert new_firm.new? end def test_build new_client = companies(:first_firm).clients_of_firm.build("name" => "Another Client") assert_equal "Another Client", new_client.name - assert new_client.new_record? + assert new_client.new? assert_equal new_client, companies(:first_firm).clients_of_firm.last assert companies(:first_firm).save - assert !new_client.new_record? + assert !new_client.new? assert_equal 2, companies(:first_firm).clients_of_firm(true).size end @@ -644,18 +637,18 @@ class HasManyAssociationsTest < Test::Unit::TestCase def test_invalid_build new_client = companies(:first_firm).clients_of_firm.build - assert new_client.new_record? + assert new_client.new? assert !new_client.valid? assert_equal new_client, companies(:first_firm).clients_of_firm.last assert !companies(:first_firm).save - assert new_client.new_record? + assert new_client.new? assert_equal 1, companies(:first_firm).clients_of_firm(true).size end def test_create force_signal37_to_load_all_clients_of_firm new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client") - assert !new_client.new_record? + assert !new_client.new? assert_equal new_client, companies(:first_firm).clients_of_firm.last assert_equal new_client, companies(:first_firm).clients_of_firm(true).last end @@ -1032,10 +1025,10 @@ class BelongsToAssociationsTest < Test::Unit::TestCase apple = Firm.new("name" => "Apple") client.firm = apple assert_equal apple, client.firm - assert apple.new_record? + assert apple.new? assert client.save assert apple.save - assert !apple.new_record? + assert !apple.new? assert_equal apple, client.firm assert_equal apple, client.firm(true) end @@ -1044,10 +1037,10 @@ class BelongsToAssociationsTest < Test::Unit::TestCase final_cut = Client.new("name" => "Final Cut") firm = Firm.find(1) final_cut.firm = firm - assert final_cut.new_record? + assert final_cut.new? assert final_cut.save - assert !final_cut.new_record? - assert !firm.new_record? + assert !final_cut.new? + assert !firm.new? assert_equal firm, final_cut.firm assert_equal firm, final_cut.firm(true) end @@ -1056,11 +1049,11 @@ class BelongsToAssociationsTest < Test::Unit::TestCase final_cut = Client.new("name" => "Final Cut") apple = Firm.new("name" => "Apple") final_cut.firm = apple - assert final_cut.new_record? - assert apple.new_record? + assert final_cut.new? + assert apple.new? assert final_cut.save - assert !final_cut.new_record? - assert !apple.new_record? + assert !final_cut.new? + assert !apple.new? assert_equal apple, final_cut.firm assert_equal apple, final_cut.firm(true) end @@ -1374,10 +1367,10 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase no_of_projects = Project.count aredridel = Developer.new("name" => "Aredridel") aredridel.projects.concat([Project.find(1), p = Project.new("name" => "Projekt")]) - assert aredridel.new_record? - assert p.new_record? + assert aredridel.new? + assert p.new? assert aredridel.save - assert !aredridel.new_record? + assert !aredridel.new? assert_equal no_of_devels+1, Developer.count assert_equal no_of_projects+1, Project.count assert_equal 2, aredridel.projects.size @@ -1392,10 +1385,10 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase ken.projects.push_with_attributes( Project.find(1), :joined_on => now ) p = Project.new("name" => "Foomatic") ken.projects.push_with_attributes( p, :joined_on => now ) - assert ken.new_record? - assert p.new_record? + assert ken.new? + assert p.new? assert ken.save - assert !ken.new_record? + assert !ken.new? assert_equal no_of_devels+1, Developer.count assert_equal no_of_projects+1, Project.count assert_equal 2, ken.projects.size @@ -1428,9 +1421,9 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase devel = Developer.find(1) proj = devel.projects.build("name" => "Projekt") assert_equal devel.projects.last, proj - assert proj.new_record? + assert proj.new? devel.save - assert !proj.new_record? + assert !proj.new? assert_equal devel.projects.last, proj assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated end @@ -1440,10 +1433,10 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase proj1 = devel.projects.build(:name => "Make bed") proj2 = devel.projects.build(:name => "Lie in it") assert_equal devel.projects.last, proj2 - assert proj2.new_record? + assert proj2.new? devel.save - assert !devel.new_record? - assert !proj2.new_record? + assert !devel.new? + assert !proj2.new? assert_equal devel.projects.last, proj2 assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated end @@ -1452,7 +1445,7 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase devel = Developer.find(1) proj = devel.projects.create("name" => "Projekt") assert_equal devel.projects.last, proj - assert !proj.new_record? + assert !proj.new? assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated end @@ -1461,10 +1454,10 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase proj1 = devel.projects.create(:name => "Make bed") proj2 = devel.projects.create(:name => "Lie in it") assert_equal devel.projects.last, proj2 - assert proj2.new_record? + assert proj2.new? devel.save - assert !devel.new_record? - assert !proj2.new_record? + assert !devel.new? + assert !proj2.new? assert_equal devel.projects.last, proj2 assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated end diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 18c5b50006..33ea40e91a 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -807,7 +807,7 @@ class BasicsTest < Test::Unit::TestCase cloned_topic = nil assert_nothing_raised { cloned_topic = topic.clone } assert_equal topic.title, cloned_topic.title - assert cloned_topic.new_record? + assert cloned_topic.new? # test if the attributes have been cloned topic.title = "a" @@ -822,7 +822,7 @@ class BasicsTest < Test::Unit::TestCase assert_equal "b", topic.title["a"] cloned_topic.save - assert !cloned_topic.new_record? + assert !cloned_topic.new? assert cloned_topic.id != topic.id end @@ -834,7 +834,7 @@ class BasicsTest < Test::Unit::TestCase assert_nothing_raised { clone = dev.clone } assert_kind_of DeveloperSalary, clone.salary assert_equal dev.salary.amount, clone.salary.amount - assert clone.new_record? + assert clone.new? # test if the attributes have been cloned original_amount = clone.salary.amount @@ -842,7 +842,7 @@ class BasicsTest < Test::Unit::TestCase assert_equal original_amount, clone.salary.amount assert clone.save - assert !clone.new_record? + assert !clone.new? assert clone.id != dev.id end @@ -1340,12 +1340,6 @@ class BasicsTest < Test::Unit::TestCase xml = [ topics(:first), topics(:second) ].to_xml(:indent => 0, :skip_instruct => true, :include => :replies) assert xml.include?(%()) end - - def test_array_to_xml_including_methods - xml = [ topics(:first), topics(:second) ].to_xml(:indent => 0, :skip_instruct => true, :methods => [ :topic_id ]) - assert xml.include?(%(#{topics(:first).topic_id})) - assert xml.include?(%(#{topics(:second).topic_id})) - end def test_array_to_xml_including_has_one_association xml = [ companies(:first_firm), companies(:rails_core) ].to_xml(:indent => 0, :skip_instruct => true, :include => :account) diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb index bd72348916..ee68b9664d 100644 --- a/activerecord/test/finder_test.rb +++ b/activerecord/test/finder_test.rb @@ -345,7 +345,7 @@ class FinderTest < Test::Unit::TestCase sig38 = Company.find_or_create_by_name("38signals") assert_equal number_of_companies + 1, Company.count assert_equal sig38, Company.find_or_create_by_name("38signals") - assert !sig38.new_record? + assert !sig38.new? end def test_find_or_create_from_two_attributes @@ -353,20 +353,20 @@ class FinderTest < Test::Unit::TestCase another = Topic.find_or_create_by_title_and_author_name("Another topic","John") assert_equal number_of_topics + 1, Topic.count assert_equal another, Topic.find_or_create_by_title_and_author_name("Another topic", "John") - assert !another.new_record? + assert !another.new? end def test_find_or_initialize_from_one_attribute sig38 = Company.find_or_initialize_by_name("38signals") assert_equal "38signals", sig38.name - assert sig38.new_record? + assert sig38.new? end def test_find_or_initialize_from_two_attributes another = Topic.find_or_initialize_by_title_and_author_name("Another topic","John") assert_equal "Another topic", another.title assert_equal "John", another.author_name - assert another.new_record? + assert another.new? end def test_find_with_bad_sql -- cgit v1.2.3