aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-05-20 21:35:58 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-05-20 21:35:58 -0300
commit9a1abedcdeecd9464668695d4f9c1d55a2fd9332 (patch)
tree287c625ff80dc7feeddea2dcb5a59ce6783a9cd3
parentc72d6c91a7c0c2dc81cc857a1d6db496e84e0065 (diff)
parent9c3afdc327132c7f1f4d05eebc0c05b715442e7d (diff)
downloadrails-9a1abedcdeecd9464668695d4f9c1d55a2fd9332.tar.gz
rails-9a1abedcdeecd9464668695d4f9c1d55a2fd9332.tar.bz2
rails-9a1abedcdeecd9464668695d4f9c1d55a2fd9332.zip
Merge pull request #14544 from jefflai2/named_scope_sti
Fixes Issue #13466. Conflicts: activerecord/CHANGELOG.md
-rw-r--r--activerecord/CHANGELOG.md11
-rw-r--r--activerecord/lib/active_record/scoping.rb4
-rw-r--r--activerecord/lib/active_record/scoping/named.rb8
-rw-r--r--activerecord/test/cases/scoping/named_scoping_test.rb7
-rw-r--r--activerecord/test/fixtures/ratings.yml10
-rw-r--r--activerecord/test/models/comment.rb1
-rw-r--r--activerecord/test/models/rating.rb4
-rw-r--r--activerecord/test/schema/schema.rb2
8 files changed, 42 insertions, 5 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 283ca33913..3c44660b3a 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,14 @@
+* Changed scoped blocks to be executed with `instance_eval`
+
+ Named scopes (i.e. using STI) were previously cached according to
+ base class so scoped queries made by classes with a common ancestor would
+ leak. Changed the way scope blocks were called so inheritance rules are
+ followed during the call and scopes are cached correctly.
+
+ Fixes #13466.
+
+ *Jefferson Lai*
+
* Change belongs_to touch to be consistent with timestamp updates
If a model is set up with a belongs_to: touch relatinoship the parent
diff --git a/activerecord/lib/active_record/scoping.rb b/activerecord/lib/active_record/scoping.rb
index 3e43591672..fca4f1c9d3 100644
--- a/activerecord/lib/active_record/scoping.rb
+++ b/activerecord/lib/active_record/scoping.rb
@@ -11,11 +11,11 @@ module ActiveRecord
module ClassMethods
def current_scope #:nodoc:
- ScopeRegistry.value_for(:current_scope, base_class.to_s)
+ ScopeRegistry.value_for(:current_scope, self.to_s)
end
def current_scope=(scope) #:nodoc:
- ScopeRegistry.set_value_for(:current_scope, base_class.to_s, scope)
+ ScopeRegistry.set_value_for(:current_scope, self.to_s, scope)
end
end
diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb
index 49cadb66d0..826b710e92 100644
--- a/activerecord/lib/active_record/scoping/named.rb
+++ b/activerecord/lib/active_record/scoping/named.rb
@@ -148,9 +148,13 @@ module ActiveRecord
extension = Module.new(&block) if block
singleton_class.send(:define_method, name) do |*args|
- scope = all.scoping { body.call(*args) }
+ if body.respond_to?(:to_proc)
+ scope = all.scoping { instance_exec(*args, &body) }
+ else
+ # Body is given as an object instead of a block, so invoke call()
+ scope = all.scoping { body.call(*args) }
+ end
scope = scope.extending(extension) if extension
-
scope || all
end
end
diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb
index 59ec2dd6a4..bfde9448f3 100644
--- a/activerecord/test/cases/scoping/named_scoping_test.rb
+++ b/activerecord/test/cases/scoping/named_scoping_test.rb
@@ -2,12 +2,13 @@ require "cases/helper"
require 'models/post'
require 'models/topic'
require 'models/comment'
+require 'models/rating'
require 'models/reply'
require 'models/author'
require 'models/developer'
class NamedScopingTest < ActiveRecord::TestCase
- fixtures :posts, :authors, :topics, :comments, :author_addresses
+ fixtures :posts, :authors, :topics, :comments, :author_addresses, :ratings
def test_implements_enumerable
assert !Topic.all.empty?
@@ -115,6 +116,10 @@ class NamedScopingTest < ActiveRecord::TestCase
assert_equal 1,SpecialPost.containing_the_letter_a.count
end
+ def test_scope_subquery_with_STI
+ assert_nothing_raised { VerySpecialComment.special_parent(SpecialRating.first).count }
+ end
+
def test_has_many_through_associations_have_access_to_scopes
assert_not_equal Comment.containing_the_letter_e, authors(:david).comments
assert !Comment.containing_the_letter_e.empty?
diff --git a/activerecord/test/fixtures/ratings.yml b/activerecord/test/fixtures/ratings.yml
index 34e208efa3..2b45c5080e 100644
--- a/activerecord/test/fixtures/ratings.yml
+++ b/activerecord/test/fixtures/ratings.yml
@@ -2,13 +2,23 @@ normal_comment_rating:
id: 1
comment_id: 8
value: 1
+ type: Rating
special_comment_rating:
id: 2
comment_id: 6
value: 1
+ type: Rating
sub_special_comment_rating:
id: 3
comment_id: 12
value: 1
+ type: Rating
+
+special_rating:
+ id: 4
+ comment_id: 10
+ value: 1
+ type: SpecialRating
+ special_comment_id: 3
diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb
index bf0162d09b..ea261ac9ec 100644
--- a/activerecord/test/models/comment.rb
+++ b/activerecord/test/models/comment.rb
@@ -39,6 +39,7 @@ class SubSpecialComment < SpecialComment
end
class VerySpecialComment < Comment
+ scope :special_parent, -> (special_rating) { where parent_id: special_rating.special_comment.id }
end
class CommentThatAutomaticallyAltersPostBody < Comment
diff --git a/activerecord/test/models/rating.rb b/activerecord/test/models/rating.rb
index 25a52c4ad7..5409230c2e 100644
--- a/activerecord/test/models/rating.rb
+++ b/activerecord/test/models/rating.rb
@@ -2,3 +2,7 @@ class Rating < ActiveRecord::Base
belongs_to :comment
has_many :taggings, :as => :taggable
end
+
+class SpecialRating < Rating
+ belongs_to :special_comment
+end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index d448fccc5b..9e511af3bd 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -595,7 +595,9 @@ ActiveRecord::Schema.define do
create_table :ratings, force: true do |t|
t.integer :comment_id
+ t.integer :special_comment_id
t.integer :value
+ t.string :type
end
create_table :readers, force: true do |t|