aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2014-12-01 18:44:51 -0500
committereileencodes <eileencodes@gmail.com>2015-01-02 17:15:31 -0500
commite9684d6c8849767ad38cb3cc76cb628252a67ed6 (patch)
treeb2e6b59dfea301da7247887c86150c09c0edf753 /activerecord/lib
parent4d27d56c3547e57bc509980eb6a9b14c39488c46 (diff)
downloadrails-e9684d6c8849767ad38cb3cc76cb628252a67ed6.tar.gz
rails-e9684d6c8849767ad38cb3cc76cb628252a67ed6.tar.bz2
rails-e9684d6c8849767ad38cb3cc76cb628252a67ed6.zip
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.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/association_scope.rb51
-rw-r--r--activerecord/lib/active_record/reflection.rb117
2 files changed, 76 insertions, 92 deletions
diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb
index cc6d23e80a..949ba75b1b 100644
--- a/activerecord/lib/active_record/associations/association_scope.rb
+++ b/activerecord/lib/active_record/associations/association_scope.rb
@@ -113,55 +113,6 @@ module ActiveRecord
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 @@ module ActiveRecord
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|
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index 1bc92b1587..ea469f881c 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -163,7 +163,7 @@ module ActiveRecord
end
def constraints
- scope ? [scope] : []
+ scope_chain.flatten
end
end
@@ -714,47 +714,6 @@ module ActiveRecord
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 @@ module ActiveRecord
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