From 0b6358beb417f69c47edd93d099906cc818855ee Mon Sep 17 00:00:00 2001
From: eileencodes <eileencodes@gmail.com>
Date: Tue, 2 Sep 2014 14:13:32 -0400
Subject: Always add lambda to scope chain to eliminate branch in eval_scope

We convert all other scopes to lambda's so it makes sense that we should
always returns a lambda on a ThroughReflection as well. This eliminates
the need to check if the scope is a Relation.
---
 activerecord/lib/active_record/associations/association_scope.rb | 6 +-----
 activerecord/lib/active_record/reflection.rb                     | 7 +++++--
 2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb
index b58c902bfc..43e1e79fcf 100644
--- a/activerecord/lib/active_record/associations/association_scope.rb
+++ b/activerecord/lib/active_record/associations/association_scope.rb
@@ -188,11 +188,7 @@ module ActiveRecord
       end
 
       def eval_scope(klass, scope, owner)
-        if scope.is_a?(Relation)
-          scope
-        else
-          klass.unscoped.instance_exec(owner, &scope)
-        end
+        klass.unscoped.instance_exec(owner, &scope)
       end
     end
   end
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index c0deb76a33..6b5a592ee5 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -728,8 +728,11 @@ module ActiveRecord
           through_scope_chain = through_reflection.scope_chain.map(&:dup)
 
           if options[:source_type]
-            through_scope_chain.first <<
-              through_reflection.klass.where(foreign_type => options[:source_type])
+            type = foreign_type
+            source_type = options[:source_type]
+            through_scope_chain.first << lambda { |object|
+              where(type => source_type)
+            }
           end
 
           # Recursively fill out the rest of the array from the through reflection
-- 
cgit v1.2.3