aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/aggregations.rb2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb38
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb8
-rw-r--r--activerecord/lib/active_record/associations/association_proxy.rb6
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_association.rb6
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb2
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb8
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb4
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb8
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb10
-rwxr-xr-xactiverecord/lib/active_record/base.rb20
-rwxr-xr-xactiverecord/lib/active_record/callbacks.rb4
-rw-r--r--activerecord/lib/active_record/locking/pessimistic.rb2
-rwxr-xr-xactiverecord/lib/active_record/validations.rb11
-rwxr-xr-xactiverecord/test/associations_test.rb107
-rwxr-xr-xactiverecord/test/base_test.rb14
-rw-r--r--activerecord/test/finder_test.rb8
18 files changed, 136 insertions, 124 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 745e06dac9..2aaa62a7b5 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,7 +1,5 @@
*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 f7a7b2c98f..a137a11cd4 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?
+ end unless self.new_record?
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 4032ddcf6c..e2dca89d49 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?
+ end unless self.new_record?
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? == true).
+ # order to update their primary keys - except if the parent object is unsaved (new_record? == 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? == true) members of the collection are automatically saved when the parent is saved.
+ # * All unsaved (new_record? == 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? || association.new? || association["#{reflection.primary_key_name}"] != id)
+ if !association.nil? && (new_record? || association.new_record? || association["#{reflection.primary_key_name}"] != id)
association["#{reflection.primary_key_name}"] = id
association.save(true)
end
@@ -655,6 +655,12 @@ 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]
@@ -664,7 +670,7 @@ module ActiveRecord
before_save <<-EOF
association = instance_variable_get("@#{reflection.name}")
if association && association.target
- if association.new?
+ if association.new_record?
association.save(true)
end
@@ -684,7 +690,7 @@ module ActiveRecord
before_save <<-EOF
association = instance_variable_get("@#{reflection.name}")
if !association.nil?
- if association.new?
+ if association.new_record?
association.save(true)
end
@@ -924,10 +930,10 @@ module ActiveRecord
define_method(method_name) do
association = instance_variable_get("@#{association_name}")
if association.respond_to?(:loaded?)
- if new?
+ if new_record?
association
else
- association.select { |record| record.new? }
+ association.select { |record| record.new_record? }
end.each do |record|
errors.add "#{association_name}" unless record.valid?
end
@@ -935,7 +941,7 @@ module ActiveRecord
end
validate method_name
- before_save("@new_record_before_save = new?; true")
+ before_save("@new_record_before_save = new_record?; true")
after_callback = <<-end_eval
association = instance_variable_get("@#{association_name}")
@@ -944,7 +950,7 @@ module ActiveRecord
if @new_record_before_save
records_to_save = association
else
- records_to_save = association.select { |record| record.new? }
+ records_to_save = association.select { |record| record.new_record? }
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
@@ -991,7 +997,7 @@ module ActiveRecord
if reflection.options[:exclusively_dependent]
reflection.options[:dependent] = :delete_all
- #warn "The :exclusively_dependent option is deprecated. Please use :dependent => :delete_all instead.")
+ ::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)
end
# See HasManyAssociation#delete_records. Dependent associations
@@ -1451,7 +1457,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(parent.active_record.base_class.name)] +
+ aliased_join_table_name, polymorphic_foreign_type, klass.quote_value(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
]
@@ -1466,7 +1472,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(source_reflection.active_record.base_class.name)
+ klass.quote_value(source_reflection.active_record.base_class.name)
]
else
case source_reflection.macro
@@ -1495,7 +1501,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(parent.active_record.base_class.name)
+ klass.quote_value(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 " % [
@@ -1503,7 +1509,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(reflection.active_record.base_class.name)
+ klass.quote_value(reflection.active_record.base_class.name)
]
else
foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
@@ -1524,7 +1530,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(klass.name.demodulize)] unless klass.descends_from_active_record?
+ klass.quote_value(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 97d0ec8ce8..552c1ed06e 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?
+ result &&= insert_record(record) unless @owner.new_record?
@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? }
+ records.reject! { |record| @target.delete(record) if record.new_record? }
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.save unless @owner.new_record?
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? })
+ unsaved_records = Array(@target.detect { |r| r.new_record? })
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 5d36c22f66..ac7188d712 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["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record?
record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s
else
- record[@reflection.primary_key_name] = @owner.id unless @owner.new?
+ record[@reflection.primary_key_name] = @owner.id unless @owner.new_record?
end
end
@@ -125,7 +125,7 @@ module ActiveRecord
end
def load_target
- if !loaded? and (!@owner.new? || foreign_key_present)
+ if !loaded? and (!@owner.new_record? || 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 52dd61a47e..1752678cbd 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?
+ if counter_cache_name && @owner[counter_cache_name] && !@owner.new_record?
@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?
+ if counter_cache_name && !@owner.new_record?
@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?
+ @owner[@reflection.primary_key_name] = record.id unless record.new_record?
@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 0a71fa89c9..9549b959fc 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?
+ unless record.new_record?
@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 f10c5957f8..8c0e50e38e 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?
+ insert_record(record) unless @owner.new_record?
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?
+ insert_record(record) unless @owner.new_record?
@target << record
callback(:after_add, record)
@@ -101,7 +101,7 @@ module ActiveRecord
end
def insert_record(record)
- if record.new?
+ if record.new_record?
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, record[column.name], column)
+ value = @owner.send(:quote_value, 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 d4c6bdcada..d3fdcbe02f 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? || foreign_key_present
+ if !@owner.new_record? || 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 @owner.class.base_class.name.to_s}"
+ "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@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 e232c650fa..d7ca7b5834 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?
+ raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) if @owner.new_record?
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?) && !associate.new?
+ raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) unless associate.respond_to?(:new_record?) && !associate.new_record?
@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(
+ "#{as}_type" => reflection.klass.quote_value(
@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(@reflection.through_reflection.klass.name)
+ @owner.class.quote_value(@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 9e7f281744..4ccd725ebd 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[@reflection.primary_key_name] = @owner.id unless @owner.new_record?
self.target = record
end
@@ -30,11 +30,11 @@ module ActiveRecord
unless @target.nil?
if dependent? && !dont_save && @target != obj
- @target.destroy unless @target.new?
+ @target.destroy unless @target.new_record?
@owner.clear_association_cache
else
@target[@reflection.primary_key_name] = nil
- @target.save unless @owner.new? || @target.new?
+ @target.save unless @owner.new_record? || @target.new_record?
end
end
@@ -48,7 +48,7 @@ module ActiveRecord
@loaded = true
- unless @owner.new? or obj.nil? or dont_save
+ unless @owner.new_record? 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 @owner.class.base_class.name.to_s}"
+ "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@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 72155546bd..e9c9212560 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? # true
+ # winter.new_record? # true
#
# == Saving arrays, hashes, and other non-mappable objects in text columns
#
@@ -1518,18 +1518,8 @@ 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?
- @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?
+ @new_record
end
# * No record exists: Creates a new record with values matching those of the object attributes.
@@ -1548,7 +1538,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?
+ unless new_record?
connection.delete <<-end_sql, "#{self.class.name} Destroy"
DELETE FROM #{self.class.table_name}
WHERE #{self.class.primary_key} = #{quoted_id}
@@ -1717,7 +1707,7 @@ module ActiveRecord #:nodoc:
comparison_object.equal?(self) ||
(comparison_object.instance_of?(self.class) &&
comparison_object.id == id &&
- !comparison_object.new?)
+ !comparison_object.new_record?)
end
# Delegates to ==
@@ -1773,7 +1763,7 @@ module ActiveRecord #:nodoc:
private
def create_or_update
- if new? then create else update end
+ if new_record? then create else update end
true
end
diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb
index dd92a03111..bfe96fd2ed 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? then result = callback(:before_validation_on_create) else result = callback(:before_validation_on_update) end
+ if new_record? 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? then callback(:after_validation_on_create) else callback(:after_validation_on_update) end
+ if new_record? 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 81146e0688..caad760742 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?
+ reload(:lock => lock) unless new_record?
self
end
end
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index cc372b5042..c9d4740670 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -333,7 +333,8 @@ module ActiveRecord
attr_accessor *(attr_names.map { |n| "#{n}_confirmation" })
validates_each(attr_names, configuration) do |record, attr_name, value|
- record.errors.add(attr_name, configuration[:message]) unless record.send("#{attr_name}_confirmation").nil? or value == record.send("#{attr_name}_confirmation")
+ confirm = record.send("#{attr_name}_confirmation")
+ record.errors.add(attr_name, configuration[:message]) unless value.nil? || value == confirm
end
end
@@ -374,6 +375,10 @@ 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:
# * <tt>message</tt> - A custom error message (default is: "can't be blank")
# * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
@@ -538,7 +543,7 @@ module ActiveRecord
condition_params << scope_value
end
end
- unless record.new?
+ unless record.new_record?
condition_sql << " AND #{record.class.table_name}.#{record.class.primary_key} <> ?"
condition_params << record.send(:id)
end
@@ -772,7 +777,7 @@ module ActiveRecord
run_validations(:validate)
validate
- if new?
+ if new_record?
run_validations(:validate_on_create)
validate_on_create
else
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 5c6090833e..d7997beaa3 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?
+ assert account.new_record?
assert firm.save
assert_equal account, firm.account
- assert !account.new?
+ assert !account.new_record?
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?
+ assert account.new_record?
assert firm.save
assert_equal account, firm.account
- assert !account.new?
+ assert !account.new_record?
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?
+ assert firm.new_record?
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?
+ assert !a.new_record?
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?
- assert a.new?
+ assert firm.new_record?
+ assert a.new_record?
assert_equal a, firm.account
assert firm.save
- assert !firm.new?
- assert !a.new?
+ assert !firm.new_record?
+ assert !a.new_record?
assert_equal a, firm.account
assert_equal a, firm.account(true)
end
@@ -350,6 +350,13 @@ 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
@@ -551,14 +558,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?
- assert c.new?
+ assert new_firm.new_record?
+ assert c.new_record?
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?
- assert !c.new?
+ assert !new_firm.new_record?
+ assert !c.new_record?
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.
@@ -569,10 +576,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?
+ assert c.new_record?
assert !firm.valid?
assert !firm.save
- assert c.new?
+ assert c.new_record?
end
def test_invalid_adding_before_save
@@ -580,21 +587,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?
+ assert c.new_record?
assert !c.valid?
assert !new_firm.valid?
assert !new_firm.save
- assert c.new?
- assert new_firm.new?
+ assert c.new_record?
+ assert new_firm.new_record?
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?
+ assert new_client.new_record?
assert_equal new_client, companies(:first_firm).clients_of_firm.last
assert companies(:first_firm).save
- assert !new_client.new?
+ assert !new_client.new_record?
assert_equal 2, companies(:first_firm).clients_of_firm(true).size
end
@@ -637,18 +644,18 @@ class HasManyAssociationsTest < Test::Unit::TestCase
def test_invalid_build
new_client = companies(:first_firm).clients_of_firm.build
- assert new_client.new?
+ assert new_client.new_record?
assert !new_client.valid?
assert_equal new_client, companies(:first_firm).clients_of_firm.last
assert !companies(:first_firm).save
- assert new_client.new?
+ assert new_client.new_record?
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?
+ assert !new_client.new_record?
assert_equal new_client, companies(:first_firm).clients_of_firm.last
assert_equal new_client, companies(:first_firm).clients_of_firm(true).last
end
@@ -1025,10 +1032,10 @@ class BelongsToAssociationsTest < Test::Unit::TestCase
apple = Firm.new("name" => "Apple")
client.firm = apple
assert_equal apple, client.firm
- assert apple.new?
+ assert apple.new_record?
assert client.save
assert apple.save
- assert !apple.new?
+ assert !apple.new_record?
assert_equal apple, client.firm
assert_equal apple, client.firm(true)
end
@@ -1037,10 +1044,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?
+ assert final_cut.new_record?
assert final_cut.save
- assert !final_cut.new?
- assert !firm.new?
+ assert !final_cut.new_record?
+ assert !firm.new_record?
assert_equal firm, final_cut.firm
assert_equal firm, final_cut.firm(true)
end
@@ -1049,11 +1056,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?
- assert apple.new?
+ assert final_cut.new_record?
+ assert apple.new_record?
assert final_cut.save
- assert !final_cut.new?
- assert !apple.new?
+ assert !final_cut.new_record?
+ assert !apple.new_record?
assert_equal apple, final_cut.firm
assert_equal apple, final_cut.firm(true)
end
@@ -1367,10 +1374,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?
- assert p.new?
+ assert aredridel.new_record?
+ assert p.new_record?
assert aredridel.save
- assert !aredridel.new?
+ assert !aredridel.new_record?
assert_equal no_of_devels+1, Developer.count
assert_equal no_of_projects+1, Project.count
assert_equal 2, aredridel.projects.size
@@ -1385,10 +1392,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?
- assert p.new?
+ assert ken.new_record?
+ assert p.new_record?
assert ken.save
- assert !ken.new?
+ assert !ken.new_record?
assert_equal no_of_devels+1, Developer.count
assert_equal no_of_projects+1, Project.count
assert_equal 2, ken.projects.size
@@ -1421,9 +1428,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?
+ assert proj.new_record?
devel.save
- assert !proj.new?
+ assert !proj.new_record?
assert_equal devel.projects.last, proj
assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated
end
@@ -1433,10 +1440,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?
+ assert proj2.new_record?
devel.save
- assert !devel.new?
- assert !proj2.new?
+ assert !devel.new_record?
+ assert !proj2.new_record?
assert_equal devel.projects.last, proj2
assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated
end
@@ -1445,7 +1452,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?
+ assert !proj.new_record?
assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated
end
@@ -1454,10 +1461,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?
+ assert proj2.new_record?
devel.save
- assert !devel.new?
- assert !proj2.new?
+ assert !devel.new_record?
+ assert !proj2.new_record?
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 33ea40e91a..18c5b50006 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?
+ assert cloned_topic.new_record?
# 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?
+ assert !cloned_topic.new_record?
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?
+ assert clone.new_record?
# 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?
+ assert !clone.new_record?
assert clone.id != dev.id
end
@@ -1340,6 +1340,12 @@ class BasicsTest < Test::Unit::TestCase
xml = [ topics(:first), topics(:second) ].to_xml(:indent => 0, :skip_instruct => true, :include => :replies)
assert xml.include?(%(<replies><reply>))
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?(%(<topic-id type="integer">#{topics(:first).topic_id}</topic-id>))
+ assert xml.include?(%(<topic-id type="integer">#{topics(:second).topic_id}</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 ee68b9664d..bd72348916 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?
+ assert !sig38.new_record?
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?
+ assert !another.new_record?
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?
+ assert sig38.new_record?
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?
+ assert another.new_record?
end
def test_find_with_bad_sql