Add the has_many_polymorphs plugin
[cs356-p2-videostore.git] / vendor / plugins / has_many_polymorphs / lib / has_many_polymorphs / reflection.rb
1 module ActiveRecord #:nodoc:
2   module Reflection #:nodoc:
3     
4     module ClassMethods #:nodoc:
5   
6       # Update the default reflection switch so that <tt>:has_many_polymorphs</tt> types get instantiated. It's not a composed method so we have to override the whole thing.
7       def create_reflection(macro, name, options, active_record)
8         case macro
9           when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many
10             reflection = AssociationReflection.new(macro, name, options, active_record)
11           when :composed_of
12             reflection = AggregateReflection.new(macro, name, options, active_record)
13         # added by has_many_polymorphs #
14           when :has_many_polymorphs
15             reflection = PolymorphicReflection.new(macro, name, options, active_record)
16         # end added #
17         end
18         write_inheritable_hash :reflections, name => reflection
19         reflection
20       end
21       
22     end
23
24     class PolymorphicError < ActiveRecordError #:nodoc:
25     end
26     
27 =begin rdoc
28
29 The reflection built by the <tt>has_many_polymorphs</tt> method. 
30
31 Inherits from ActiveRecord::Reflection::AssociationReflection.
32
33 =end
34
35     class PolymorphicReflection < AssociationReflection
36       # Stub out the validity check. Has_many_polymorphs checks validity on macro creation, not on reflection.
37       def check_validity! 
38         # nothing
39       end                 
40
41       # Return the source reflection.
42       def source_reflection
43         # normally is the has_many to the through model, but we return ourselves, 
44         # since there isn't a real source class for a polymorphic target
45         self
46       end      
47       
48       # Set the classname of the target. Uses the join class name.
49       def class_name
50         # normally is the classname of the association target
51         @class_name ||= options[:join_class_name]
52       end
53                      
54     end
55  
56   end
57 end