aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2016-02-02 05:00:15 -0200
committerRafael França <rafaelmfranca@gmail.com>2016-02-02 05:00:15 -0200
commit7710d7f432ca776885a346346ccf0971e80b7a34 (patch)
treee248680e2fcaf9aa5d9458da36be36edbbc57704 /activerecord
parent2c7bc9251c41db15514fba6b9c03e41cd6232433 (diff)
parent2812af720e4869f03e836d1b527b0a50c2a52b22 (diff)
downloadrails-7710d7f432ca776885a346346ccf0971e80b7a34.tar.gz
rails-7710d7f432ca776885a346346ccf0971e80b7a34.tar.bz2
rails-7710d7f432ca776885a346346ccf0971e80b7a34.zip
Merge pull request #23331 from amatsuda/valid_scope_name
Warn if a named scope is overwriting an existing scope or method
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/scoping/named.rb10
-rw-r--r--activerecord/test/cases/scoping/named_scoping_test.rb19
2 files changed, 29 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb
index 103569c84d..5395bd6076 100644
--- a/activerecord/lib/active_record/scoping/named.rb
+++ b/activerecord/lib/active_record/scoping/named.rb
@@ -151,6 +151,7 @@ module ActiveRecord
"a class method with the same name."
end
+ valid_scope_name?(name)
extension = Module.new(&block) if block
if body.respond_to?(:to_proc)
@@ -169,6 +170,15 @@ module ActiveRecord
end
end
end
+
+ protected
+
+ def valid_scope_name?(name)
+ if respond_to?(name, true)
+ logger.warn "Creating scope :#{name}. " \
+ "Overwriting existing method #{self.name}.#{name}."
+ end
+ end
end
end
end
diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb
index 7a8eaeccb7..acba97bbb8 100644
--- a/activerecord/test/cases/scoping/named_scoping_test.rb
+++ b/activerecord/test/cases/scoping/named_scoping_test.rb
@@ -440,6 +440,25 @@ class NamedScopingTest < ActiveRecord::TestCase
end
end
+ def test_scopes_with_reserved_names
+ class << Topic
+ def public_method; end
+ public :public_method
+
+ def protected_method; end
+ protected :protected_method
+
+ def private_method; end
+ private :private_method
+ end
+
+ [:public_method, :protected_method, :private_method].each do |reserved_method|
+ assert Topic.respond_to?(reserved_method, true)
+ ActiveRecord::Base.logger.expects(:warn)
+ silence_warnings { Topic.scope(reserved_method, -> { }) }
+ end
+ end
+
def test_scopes_on_relations
# Topic.replied
approved_topics = Topic.all.approved.order('id DESC')