X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=vendor%2Fplugins%2Fhas_many_polymorphs%2Flib%2Fhas_many_polymorphs%2Freflection.rb;fp=vendor%2Fplugins%2Fhas_many_polymorphs%2Flib%2Fhas_many_polymorphs%2Freflection.rb;h=3a8d5373e09cab57e572f9f3874bb9c2f28c32c2;hb=97369db0c55dbf9d52059116497995481d9d163d;hp=0000000000000000000000000000000000000000;hpb=9937b32d7394e77fa89915b66f9645024ccb2009;p=cs356-p2-videostore.git
diff --git a/vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs/reflection.rb b/vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs/reflection.rb
new file mode 100644
index 0000000..3a8d537
--- /dev/null
+++ b/vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs/reflection.rb
@@ -0,0 +1,57 @@
+module ActiveRecord #:nodoc:
+ module Reflection #:nodoc:
+
+ module ClassMethods #:nodoc:
+
+ # Update the default reflection switch so that :has_many_polymorphs types get instantiated. It's not a composed method so we have to override the whole thing.
+ def create_reflection(macro, name, options, active_record)
+ case macro
+ when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many
+ reflection = AssociationReflection.new(macro, name, options, active_record)
+ when :composed_of
+ reflection = AggregateReflection.new(macro, name, options, active_record)
+ # added by has_many_polymorphs #
+ when :has_many_polymorphs
+ reflection = PolymorphicReflection.new(macro, name, options, active_record)
+ # end added #
+ end
+ write_inheritable_hash :reflections, name => reflection
+ reflection
+ end
+
+ end
+
+ class PolymorphicError < ActiveRecordError #:nodoc:
+ end
+
+=begin rdoc
+
+The reflection built by the has_many_polymorphs method.
+
+Inherits from ActiveRecord::Reflection::AssociationReflection.
+
+=end
+
+ class PolymorphicReflection < AssociationReflection
+ # Stub out the validity check. Has_many_polymorphs checks validity on macro creation, not on reflection.
+ def check_validity!
+ # nothing
+ end
+
+ # Return the source reflection.
+ def source_reflection
+ # normally is the has_many to the through model, but we return ourselves,
+ # since there isn't a real source class for a polymorphic target
+ self
+ end
+
+ # Set the classname of the target. Uses the join class name.
+ def class_name
+ # normally is the classname of the association target
+ @class_name ||= options[:join_class_name]
+ end
+
+ end
+
+ end
+end