From dc925119a3912ecfe0df400007163f33b99d6385 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 28 Jan 2016 17:13:47 +0900 Subject: Revert "Remove valid_scope_name? check - use ruby" This reverts commit f6db31ec16e42ee7713029f7120f0b011d1ddc6c. Reason: Scope names can very easily conflict, particularly when sharing Concerns within the team, or using multiple gems that extend AR models. It is true that Ruby has the ability to detect this with the -w option, but the reality is that we are depending on too many gems that do not care about Ruby warnings, therefore it might not be a realistic solution to turn this switch on in our real-world apps. --- activerecord/lib/active_record/scoping/named.rb | 10 ++++++++++ activerecord/test/cases/scoping/named_scoping_test.rb | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) (limited to 'activerecord') 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..db1e42fcc2 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) + Topic.scope(reserved_method) + end + end + def test_scopes_on_relations # Topic.replied approved_topics = Topic.all.approved.order('id DESC') -- cgit v1.2.3 From 60b8ab710d6afd5810f1a4625b6551190e12d81f Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 28 Jan 2016 22:29:55 +0900 Subject: scope needs the second argument --- activerecord/test/cases/scoping/named_scoping_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb index db1e42fcc2..722153bb33 100644 --- a/activerecord/test/cases/scoping/named_scoping_test.rb +++ b/activerecord/test/cases/scoping/named_scoping_test.rb @@ -455,7 +455,7 @@ class NamedScopingTest < ActiveRecord::TestCase [:public_method, :protected_method, :private_method].each do |reserved_method| assert Topic.respond_to?(reserved_method, true) ActiveRecord::Base.logger.expects(:warn) - Topic.scope(reserved_method) + Topic.scope(reserved_method, -> { }) end end -- cgit v1.2.3 From 2812af720e4869f03e836d1b527b0a50c2a52b22 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 28 Jan 2016 22:30:52 +0900 Subject: Suppress :warning:s --- activerecord/test/cases/scoping/named_scoping_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb index 722153bb33..acba97bbb8 100644 --- a/activerecord/test/cases/scoping/named_scoping_test.rb +++ b/activerecord/test/cases/scoping/named_scoping_test.rb @@ -455,7 +455,7 @@ class NamedScopingTest < ActiveRecord::TestCase [:public_method, :protected_method, :private_method].each do |reserved_method| assert Topic.respond_to?(reserved_method, true) ActiveRecord::Base.logger.expects(:warn) - Topic.scope(reserved_method, -> { }) + silence_warnings { Topic.scope(reserved_method, -> { }) } end end -- cgit v1.2.3