Clean up / refactor new reflection classes

Move `RuntimeReflection` and `PolymorphicReflect` into Reflection. This
allows the methods to inherit from `ThroughReflection` and DRY up the
methods by removing duplicates.
This commit is contained in:
eileencodes 2014-12-01 18:44:51 -05:00
parent 4d27d56c35
commit e9684d6c88
2 changed files with 76 additions and 92 deletions

@ -113,55 +113,6 @@ def next_chain_scope(scope, table, reflection, connection, assoc_klass, foreign_
scope = scope.joins(join(foreign_table, constraint))
end
class RuntimeReflection
attr_accessor :next
def initialize(reflection, association)
@reflection = reflection
@association = association
end
def klass
@association.klass
end
def scope
@reflection.scope
end
def table_name
klass.table_name
end
def plural_name
@reflection.plural_name
end
def join_keys(assoc_klass)
@reflection.join_keys(assoc_klass)
end
def type
@reflection.type
end
def constraints
@reflection.constraints
end
def source_type_info
@reflection.source_type_info
end
def alias_name(name, alias_tracker)
@alias ||= begin
alias_name = "#{plural_name}_#{name}_join"
table_name = klass.table_name
alias_tracker.aliased_table_for(table_name, alias_name)
end
end
end
class ReflectionProxy < SimpleDelegator
attr_accessor :next
@ -175,7 +126,7 @@ def alias_name(name, alias_tracker)
def get_chain(refl, association, tracker)
name = refl.name
runtime_reflection = RuntimeReflection.new(refl, association)
runtime_reflection = ActiveRecord::Reflection::RuntimeReflection.new(refl, association)
runtime_reflection.alias_name(name, tracker)
prev = runtime_reflection
refl.chain.drop(1).each { |reflection|

@ -163,7 +163,7 @@ def source_macro
end
def constraints
scope ? [scope] : []
scope_chain.flatten
end
end
@ -714,47 +714,6 @@ def chain
end
end
class PolymorphicReflection
def initialize(reflection, prev_reflection)
@reflection = reflection
@prev_reflection = prev_reflection
end
def klass
@reflection.klass
end
def scope
@reflection.scope
end
def table_name
@reflection.table_name
end
def plural_name
@reflection.plural_name
end
def join_keys(assoc_klass)
@reflection.join_keys(assoc_klass)
end
def type
@reflection.type
end
def constraints
[source_type_info]
end
def source_type_info
type = @prev_reflection.foreign_type
source_type = @prev_reflection.options[:source_type]
lambda { |object| where(type => source_type) }
end
end
# Consider the following example:
#
# class Person
@ -934,5 +893,79 @@ def derive_class_name
delegate(*delegate_methods, to: :delegate_reflection)
end
class PolymorphicReflection < ThroughReflection
def initialize(reflection, prev_reflection)
@reflection = reflection
@prev_reflection = prev_reflection
end
def klass
@reflection.klass
end
def scope
@reflection.scope
end
def table_name
@reflection.table_name
end
def plural_name
@reflection.plural_name
end
def join_keys(assoc_klass)
@reflection.join_keys(assoc_klass)
end
def type
@reflection.type
end
def constraints
[source_type_info]
end
def source_type_info
type = @prev_reflection.foreign_type
source_type = @prev_reflection.options[:source_type]
lambda { |object| where(type => source_type) }
end
end
class RuntimeReflection < PolymorphicReflection
attr_accessor :next
def initialize(reflection, association)
@reflection = reflection
@association = association
end
def klass
@association.klass
end
def table_name
klass.table_name
end
def constraints
@reflection.constraints
end
def source_type_info
@reflection.source_type_info
end
def alias_name(name, alias_tracker)
@alias ||= begin
alias_name = "#{plural_name}_#{name}_join"
table_name = klass.table_name
alias_tracker.aliased_table_for(table_name, alias_name)
end
end
end
end
end