Permalink
Browse files

AR::Base does not need to know how to create reflection objects

  • Loading branch information...
1 parent e20dd73 commit d531ac0b9afcce02d5cd81006c45dad2fb813e22 @tenderlove tenderlove committed Jul 23, 2013
@@ -223,7 +223,7 @@ def composed_of(part_id, options = {})
reader_method(name, class_name, mapping, allow_nil, constructor)
writer_method(name, class_name, mapping, allow_nil, converter)
- create_reflection(:composed_of, part_id, nil, options, self)
+ ActiveRecord::Reflection.create(:composed_of, part_id, nil, options, self)
end
private
@@ -54,7 +54,7 @@ def build
validate_options
define_accessors
configure_dependency if options[:dependent]
- reflection = model.create_reflection(macro, name, scope, options, model)
+ reflection = ActiveRecord::Reflection.create(macro, name, scope, options, model)
Association.extensions.each do |extension|
extension.build @model, reflection
end
@@ -10,6 +10,25 @@ module Reflection # :nodoc:
self.aggregate_reflections = {}
end
+ def self.create(macro, name, scope, options, ar)
+ case macro
+ when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many
+ klass = options[:through] ? ThroughReflection : AssociationReflection
+ when :composed_of
+ klass = AggregateReflection
+ end
+
+ reflection = klass.new(macro, name, scope, options, ar)
+
+ if klass == AggregateReflection
+ ar.aggregate_reflections = ar.aggregate_reflections.merge(name => reflection)
+ else
+ ar.reflections = ar.reflections.merge(name => reflection)
+ end
+
+ reflection
+ end
+
# \Reflection enables to interrogate Active Record classes and objects
# about their associations and aggregations. This information can,
# for example, be used in a form builder that takes an Active Record object
@@ -19,25 +38,6 @@ module Reflection # :nodoc:
# MacroReflection class has info for AggregateReflection and AssociationReflection
# classes.
module ClassMethods
- def create_reflection(macro, name, scope, options, active_record)
- case macro
- when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many
- klass = options[:through] ? ThroughReflection : AssociationReflection
- when :composed_of
- klass = AggregateReflection
- end
-
- reflection = klass.new(macro, name, scope, options, active_record)
-
- if klass == AggregateReflection
- self.aggregate_reflections = self.aggregate_reflections.merge(name => reflection)
- else
- self.reflections = self.reflections.merge(name => reflection)
- end
-
- reflection
- end
-
# Returns an array of AggregateReflection objects for all the aggregations in the class.
def reflect_on_all_aggregations
aggregate_reflections.values

0 comments on commit d531ac0

Please sign in to comment.