Permalink
Browse files

Backed out of new_record? to new? transformation as it would screw up…

… existing models that did boolean calls on "new" attributes [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5018 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 9d7e643 commit 85fbb22f071b96d5a20ac71dc16d4432c08cdcf3 @dhh dhh committed Sep 5, 2006
View
@@ -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]
@@ -7,7 +7,7 @@ def self.included(base)
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
@@ -65,7 +65,7 @@ def self.included(base)
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 @@ def clear_association_cache #:nodoc:
# === 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 @@ def clear_association_cache #:nodoc:
# (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 @@ def has_one(association_id, options = {})
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 @@ def has_one(association_id, options = {})
# :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 @@ def belongs_to(association_id, options = {})
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 @@ def belongs_to(association_id, options = {})
before_save <<-EOF
association = instance_variable_get("@#{reflection.name}")
if !association.nil?
- if association.new?
+ if association.new_record?
association.save(true)
end
@@ -924,18 +930,18 @@ def add_multiple_associated_save_callbacks(association_name)
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
end
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 @@ def add_multiple_associated_save_callbacks(association_name)
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 @@ def configure_dependency_for_has_many(reflection)
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 @@ def association_join
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 @@ def association_join
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,15 +1501,15 @@ def association_join
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 " % [
table_name_and_alias,
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 @@ def association_join
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
@@ -23,7 +23,7 @@ def <<(*records)
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 @@ def sum(*args, &block)
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 @@ def create(attributes = {})
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 @@ def size
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
@@ -99,10 +99,10 @@ def extract_options_from_args!(args)
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 @@ def method_missing(method, *args, &block)
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
@@ -13,21 +13,21 @@ def replace(record)
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
@target = @owner[@reflection.primary_key_name] = nil
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
@@ -7,7 +7,7 @@ def replace(record)
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
@@ -19,7 +19,7 @@ def create(attributes = {})
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 @@ def push_with_attributes(record, join_attributes = {})
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 @@ def count_records
end
def insert_record(record)
- if record.new?
+ if record.new_record?
return false unless record.save
end
@@ -118,7 +118,7 @@ def insert_record(record)
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
@@ -111,7 +111,7 @@ def method_missing(method, *args, &block)
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 @@ def construct_sql
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
@@ -49,15 +49,15 @@ def reset
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
klass = through.klass
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 @@ def construct_join_attributes(associate)
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 @@ def construct_joins(custom_joins = nil)
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
@@ -18,7 +18,7 @@ def build(attributes = {}, replace_existing = true)
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 @@ def replace(obj, dont_save = false)
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 @@ def replace(obj, dont_save = false)
@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 @@ def construct_sql
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
Oops, something went wrong.

0 comments on commit 85fbb22

Please sign in to comment.