aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-12-03 04:29:55 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-12-03 04:29:55 +0000
commit6abda696b5df14a9ab132c34311daaabe12030e6 (patch)
treeeb0cfad7daa4d46848e6ea10e1abd90ed93a3368 /activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
parent57b7532b910f9258cad4111db79349d2d63be6d4 (diff)
downloadrails-6abda696b5df14a9ab132c34311daaabe12030e6.tar.gz
rails-6abda696b5df14a9ab132c34311daaabe12030e6.tar.bz2
rails-6abda696b5df14a9ab132c34311daaabe12030e6.zip
Added preliminary support for join models [DHH] Added preliminary support for polymorphic associations [DHH] Refactored associations to use reflections to get DRYer, beware, major refactoring -- double check before deploying anything with this (all tests pass, but..)
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3213 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb62
1 files changed, 21 insertions, 41 deletions
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 e2a7f1a58e..6c2b9b89fd 100644
--- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
+++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
@@ -1,69 +1,49 @@
module ActiveRecord
module Associations
- class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc:
- def initialize(owner, association_name, association_class_name, association_class_primary_key_name, options)
- @owner = owner
- @options = options
- @association_name = association_name
- @association_class_primary_key_name = association_class_primary_key_name
-
- proxy_extend(options[:extend]) if options[:extend]
-
- reset
- end
-
- def create(attributes = {})
- raise ActiveRecord::ActiveRecordError, "Can't create an abstract polymorphic object"
- end
-
- def build(attributes = {})
- raise ActiveRecord::ActiveRecordError, "Can't build an abstract polymorphic object"
- end
-
- def replace(obj, dont_save = false)
- if obj.nil?
- @target = @owner[@association_class_primary_key_name] = @owner[@options[:foreign_type]] = nil
+ class BelongsToPolymorphicAssociation < AssociationProxy #:nodoc:
+ def replace(record)
+ if record.nil?
+ @target = @owner[@reflection.primary_key_name] = @owner[@reflection.options[:foreign_type]] = nil
else
- @target = (AssociationProxy === obj ? obj.target : obj)
+ @target = (AssociationProxy === record ? record.target : record)
- unless obj.new_record?
- @owner[@association_class_primary_key_name] = obj.id
- @owner[@options[:foreign_type]] = ActiveRecord::Base.send(:class_name_of_active_record_descendant, obj.class).to_s
+ unless record.new_record?
+ @owner[@reflection.primary_key_name] = record.id
+ @owner[@reflection.options[:foreign_type]] = ActiveRecord::Base.send(:class_name_of_active_record_descendant, record.class).to_s
end
@updated = true
end
- @loaded = true
+ loaded
+ record
+ end
- return (@target.nil? ? nil : self)
+ def updated?
+ @updated
end
-
+
private
def find_target
return nil if association_class.nil?
- if @options[:conditions]
+ if @reflection.options[:conditions]
association_class.find(
- @owner[@association_class_primary_key_name],
- :conditions => interpolate_sql(@options[:conditions]),
- :include => @options[:include]
+ @owner[@reflection.primary_key_name],
+ :conditions => interpolate_sql(@reflection.options[:conditions]),
+ :include => @reflection.options[:include]
)
else
- association_class.find(@owner[@association_class_primary_key_name], :include => @options[:include])
+ association_class.find(@owner[@reflection.primary_key_name], :include => @reflection.options[:include])
end
end
def foreign_key_present
- !@owner[@association_class_primary_key_name].nil?
+ !@owner[@reflection.primary_key_name].nil?
end
- def target_obsolete?
- @owner[@association_class_primary_key_name] != @target.id
- end
-
def association_class
- @owner[@options[:foreign_type]] ? @owner[@options[:foreign_type]].constantize : nil
+ @owner[@reflection.options[:foreign_type]] ? @owner[@reflection.options[:foreign_type]].constantize : nil
end
end
end