From 0e8432e97404edf5a259374e6aa0cf47ce009273 Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Fri, 10 Sep 2021 12:19:46 -0700 Subject: [PATCH] Remove warning when overwriting existing scopes --- activerecord/CHANGELOG.md | 10 ++++++++++ .../lib/active_record/scoping/named.rb | 8 -------- activerecord/test/cases/enum_test.rb | 8 -------- .../test/cases/scoping/named_scoping_test.rb | 20 ------------------- 4 files changed, 10 insertions(+), 36 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 77a4abcfec..d5b5118fa5 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,13 @@ +* Remove warning when overwriting existing scopes + + Removes the following unnecessary warning message that appeared when overwriting existing scopes + + ``` + Creating scope :my_scope_name. Overwriting existing method "MyClass.my_scope_name" when overwriting existing scopes + ``` + + *Weston Ganger* + * Fix `ActiveRecord::InternalMetadata` to not be broken by `config.active_record.record_timestamps = false` Since the model always create the timestamp columns, it has to set them, otherwise it breaks diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index 621da4a113..92c907355b 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -168,7 +168,6 @@ module ActiveRecord "an instance method with the same name." end - valid_scope_name?(name) extension = Module.new(&block) if block if body.respond_to?(:to_proc) @@ -193,13 +192,6 @@ module ActiveRecord def singleton_method_added(name) generate_relation_method(name) if Kernel.respond_to?(name) && !ActiveRecord::Relation.method_defined?(name) end - - def valid_scope_name?(name) - if respond_to?(name, true) && logger - logger.warn "Creating scope :#{name}. " \ - "Overwriting existing method #{self.name}.#{name}." - end - end end end end diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index 3ddb349e91..4737588347 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -879,9 +879,6 @@ class EnumTest < ActiveRecord::TestCase " This has caused a conflict with auto generated negative scopes."\ " Avoid using enum elements starting with 'not' where the positive form is also an element." - # this message comes from ActiveRecord::Scoping::Named, but it's worth noting that both occur in this case - expected_message_2 = "Creating scope :not_sent. Overwriting existing method Book.not_sent." - Class.new(ActiveRecord::Base) do def self.name "Book" @@ -892,7 +889,6 @@ class EnumTest < ActiveRecord::TestCase end assert_includes(logger.logged(:warn), expected_message_1) - assert_includes(logger.logged(:warn), expected_message_2) ensure ActiveRecord::Base.logger = old_logger end @@ -907,9 +903,6 @@ class EnumTest < ActiveRecord::TestCase " This has caused a conflict with auto generated negative scopes."\ " Avoid using enum elements starting with 'not' where the positive form is also an element." - # this message comes from ActiveRecord::Scoping::Named, but it's worth noting that both occur in this case - expected_message_2 = "Creating scope :not_sent. Overwriting existing method Book.not_sent." - Class.new(ActiveRecord::Base) do def self.name "Book" @@ -920,7 +913,6 @@ class EnumTest < ActiveRecord::TestCase end assert_includes(logger.logged(:warn), expected_message_1) - assert_includes(logger.logged(:warn), expected_message_2) ensure ActiveRecord::Base.logger = old_logger end diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb index a31f0d2627..997e7a4b65 100644 --- a/activerecord/test/cases/scoping/named_scoping_test.rb +++ b/activerecord/test/cases/scoping/named_scoping_test.rb @@ -512,26 +512,6 @@ 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) - assert_called(ActiveRecord::Base.logger, :warn) do - silence_warnings { Topic.scope(reserved_method, -> { }) } - end - end - end - def test_scopes_on_relations # Topic.replied approved_topics = Topic.all.approved.order("id DESC")