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/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 ++----- 14 files changed, 64 insertions(+), 65 deletions(-) (limited to 'activerecord/lib') 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 -- cgit v1.2.3