aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
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/lib
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/lib')
-rw-r--r--activerecord/lib/active_record/enum.rb2
-rw-r--r--activerecord/lib/active_record/scoping/named.rb6
2 files changed, 8 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb
index f373b98035..1a3e6e4d09 100644
--- a/activerecord/lib/active_record/enum.rb
+++ b/activerecord/lib/active_record/enum.rb
@@ -221,6 +221,8 @@ module ActiveRecord
def detect_enum_conflict!(enum_name, method_name, klass_method = false)
if klass_method && dangerous_class_method?(method_name)
raise_conflict_error(enum_name, method_name, type: "class")
+ elsif klass_method && method_defined_within?(method_name, Relation)
+ raise_conflict_error(enum_name, method_name, type: "class", source: Relation.name)
elsif !klass_method && dangerous_attribute_method?(method_name)
raise_conflict_error(enum_name, method_name)
elsif !klass_method && method_defined_within?(method_name, _enum_methods_module, Module)
diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb
index 310af72c41..752655aa05 100644
--- a/activerecord/lib/active_record/scoping/named.rb
+++ b/activerecord/lib/active_record/scoping/named.rb
@@ -171,6 +171,12 @@ module ActiveRecord
"a class method with the same name."
end
+ if method_defined_within?(name, Relation)
+ raise ArgumentError, "You tried to define a scope named \"#{name}\" " \
+ "on the model \"#{self.name}\", but ActiveRecord::Relation already defined " \
+ "an instance method with the same name."
+ end
+
valid_scope_name?(name)
extension = Module.new(&block) if block