aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/scoping
diff options
context:
space:
mode:
authorChen Kinnrot <kinnrot@gmail.com>2017-11-18 21:22:00 +0200
committerChen Kinnrot <kinnrot@gmail.com>2017-11-28 15:50:18 +0200
commit6552ce0361f30a46a58a8bcb659866f06c8b7430 (patch)
tree9dafcb449cade201f9979ba94d935d93501a37e2 /activerecord/test/cases/scoping
parentf2b6406066888aaa10aba6c3cb95c2eb6dda760e (diff)
downloadrails-6552ce0361f30a46a58a8bcb659866f06c8b7430.tar.gz
rails-6552ce0361f30a46a58a8bcb659866f06c8b7430.tar.bz2
rails-6552ce0361f30a46a58a8bcb659866f06c8b7430.zip
Prevent scope named same as a ActiveRecord::Relation instance method.
Due to inconsistent behavior when chaining scopes and one scope named after a Relation method Validation code added in 2 places: - scope, to prevent problematic scope names. - enum, cause it tries to auto define scope.
Diffstat (limited to 'activerecord/test/cases/scoping')
-rw-r--r--activerecord/test/cases/scoping/named_scoping_test.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb
index b0431a4e34..17d3f27bb1 100644
--- a/activerecord/test/cases/scoping/named_scoping_test.rb
+++ b/activerecord/test/cases/scoping/named_scoping_test.rb
@@ -151,6 +151,22 @@ class NamedScopingTest < ActiveRecord::TestCase
assert_equal "The scope body needs to be callable.", e.message
end
+ def test_scopes_name_is_relation_method
+ conflicts = [
+ :records,
+ :to_ary,
+ :to_sql,
+ :explain
+ ]
+
+ conflicts.each do |name|
+ e = assert_raises ArgumentError do
+ Class.new(Post).class_eval { scope name, -> { where(approved: true) } }
+ end
+ assert_match(/You tried to define a scope named \"#{name}\" on the model/, e.message)
+ end
+ end
+
def test_active_records_have_scope_named__all__
assert !Topic.all.empty?