From 0b740c4a741698d6870848fb4d99831c30fd9a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciek=20Dubi=C5=84ski?= Date: Mon, 15 Jan 2024 16:52:27 +0100 Subject: [PATCH] Use a dedicated ActiveSupport::Deprecation instance (#2479) Rails 7.1 started deprecating the direct usage of ActiveSupport::Deprecate methods. Instead, the preferred way is to instantiate the class and use the custom instance. Compatible with Rails 4.0+ Co-authored-by: Nick Charlton --- app/controllers/concerns/administrate/punditize.rb | 2 +- lib/administrate.rb | 12 ++++++++---- lib/administrate/field/deferred.rb | 2 +- .../controllers/admin/application_controller_spec.rb | 8 ++++---- spec/controllers/admin/orders_controller_spec.rb | 12 ++++++------ spec/example_app/config/environments/test.rb | 7 ++++++- spec/generators/dashboard_generator_spec.rb | 4 ++-- spec/generators/install_generator_spec.rb | 4 ++-- spec/lib/administrate/search_spec.rb | 8 ++++---- spec/lib/fields/belongs_to_spec.rb | 12 ++++++------ spec/lib/fields/deferred_spec.rb | 4 ++-- spec/lib/fields/has_many_spec.rb | 8 ++++---- spec/lib/fields/has_one_spec.rb | 4 ++-- 13 files changed, 48 insertions(+), 39 deletions(-) diff --git a/app/controllers/concerns/administrate/punditize.rb b/app/controllers/concerns/administrate/punditize.rb index 9a3841985f..33dacddff4 100644 --- a/app/controllers/concerns/administrate/punditize.rb +++ b/app/controllers/concerns/administrate/punditize.rb @@ -42,7 +42,7 @@ def policy_scope!(user, scope) end if policy_scope.respond_to? :resolve_admin - ActiveSupport::Deprecation.warn( + Administrate.deprecator.warn( "Pundit policy scope `resolve_admin` method is deprecated. " + "Please use a namespaced pundit policy instead.", ) diff --git a/lib/administrate.rb b/lib/administrate.rb index 0a3c94ebb2..afc52637eb 100644 --- a/lib/administrate.rb +++ b/lib/administrate.rb @@ -2,7 +2,7 @@ module Administrate def self.warn_of_missing_resource_class - ActiveSupport::Deprecation.warn( + deprecator.warn( "Calling Field::Base.permitted_attribute without the option " + ":resource_class is deprecated. If you are seeing this " + "message, you are probably using a custom field type that" + @@ -12,7 +12,7 @@ def self.warn_of_missing_resource_class end def self.warn_of_deprecated_option(name) - ActiveSupport::Deprecation.warn( + deprecator.warn( "The option :#{name} is deprecated. " + "Administrate should detect it automatically. " + "Please file an issue at " + @@ -22,7 +22,7 @@ def self.warn_of_deprecated_option(name) end def self.warn_of_deprecated_method(klass, method) - ActiveSupport::Deprecation.warn( + deprecator.warn( "The method #{klass}##{method} is deprecated. " + "If you are seeing this message you are probably " + "using a dashboard that depends explicitly on it. " + @@ -32,10 +32,14 @@ def self.warn_of_deprecated_method(klass, method) end def self.warn_of_deprecated_authorization_method(method) - ActiveSupport::Deprecation.warn( + deprecator.warn( "The method `#{method}` is deprecated. " + "Please use `accessible_action?` instead, " + "or see the documentation for other options.", ) end + + def self.deprecator + @deprecator ||= ActiveSupport::Deprecation.new(VERSION, "Administrate") + end end diff --git a/lib/administrate/field/deferred.rb b/lib/administrate/field/deferred.rb index b599bf4202..88f7f23998 100644 --- a/lib/administrate/field/deferred.rb +++ b/lib/administrate/field/deferred.rb @@ -34,7 +34,7 @@ def searchable? end def searchable_field - ActiveSupport::Deprecation.warn( + Administrate.deprecator.warn( "searchable_field is deprecated, use searchable_fields instead", ) options.fetch(:searchable_field) diff --git a/spec/controllers/admin/application_controller_spec.rb b/spec/controllers/admin/application_controller_spec.rb index 4e87b88df9..06b99543fd 100644 --- a/spec/controllers/admin/application_controller_spec.rb +++ b/spec/controllers/admin/application_controller_spec.rb @@ -82,9 +82,9 @@ def index end it "triggers a deprecation warning" do - allow(ActiveSupport::Deprecation).to receive(:warn) + allow(Administrate.deprecator).to receive(:warn) get :index - expect(ActiveSupport::Deprecation).to( + expect(Administrate.deprecator).to( have_received(:warn). with(/`show_action\?` is deprecated/), ) @@ -99,9 +99,9 @@ def index end it "triggers a deprecation warning" do - allow(ActiveSupport::Deprecation).to receive(:warn) + allow(Administrate.deprecator).to receive(:warn) get :index - expect(ActiveSupport::Deprecation).to( + expect(Administrate.deprecator).to( have_received(:warn). with(/`valid_action\?` is deprecated/), ) diff --git a/spec/controllers/admin/orders_controller_spec.rb b/spec/controllers/admin/orders_controller_spec.rb index 423b5d6872..da0a0f4728 100644 --- a/spec/controllers/admin/orders_controller_spec.rb +++ b/spec/controllers/admin/orders_controller_spec.rb @@ -108,7 +108,7 @@ def send_request(order:) context "with deprecated Punditize concern" do before do - allow(ActiveSupport::Deprecation).to receive(:warn) + allow(Administrate.deprecator).to receive(:warn) class OrderPolicy class Scope @@ -147,7 +147,7 @@ def pundit_user locals = capture_view_locals { get :index } expect(locals[:resources]).to contain_exactly(order1, order3) - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/`resolve_admin` method is deprecated/) end end @@ -162,7 +162,7 @@ def pundit_user it "allows me to edit my records" do order = create :order, customer: user expect { get :edit, params: { id: order.id } }.not_to raise_error - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/`resolve_admin` method is deprecated/) end @@ -171,7 +171,7 @@ def pundit_user order = create(:order, customer: other_user) expect { get :show, params: { id: order.id } }. to raise_error(ActiveRecord::RecordNotFound) - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/`resolve_admin` method is deprecated/) end end @@ -192,7 +192,7 @@ def send_request(order:) send_request(order: order) expect(response).to redirect_to([:admin, order]) expect(order.reload.address_line_one).to eq("22 Acacia Avenue") - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/`resolve_admin` method is deprecated/) end @@ -202,7 +202,7 @@ def send_request(order:) expect do send_request(order: order) end.to raise_error(ActiveRecord::RecordNotFound) - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/`resolve_admin` method is deprecated/) end end diff --git a/spec/example_app/config/environments/test.rb b/spec/example_app/config/environments/test.rb index 7932eadb4f..2518ef3511 100644 --- a/spec/example_app/config/environments/test.rb +++ b/spec/example_app/config/environments/test.rb @@ -43,9 +43,14 @@ config.active_support.disallowed_deprecation_warnings = [] # Raises error for missing translations. - if Gem::Version.new(Rails.version) <= Gem::Version.new("6.1") + + if Rails.gem_version <= Gem::Version.new("6.1") config.action_view.raise_on_missing_translations = true else config.i18n.raise_on_missing_translations = true end + + if Rails.gem_version >= Gem::Version.new("7.0") + config.active_support.cache_format_version = 7.0 + end end diff --git a/spec/generators/dashboard_generator_spec.rb b/spec/generators/dashboard_generator_spec.rb index 924c939e09..892cae33b3 100644 --- a/spec/generators/dashboard_generator_spec.rb +++ b/spec/generators/dashboard_generator_spec.rb @@ -15,7 +15,7 @@ run_generator ["customer"] - expect(dashboard).to exist + expect(Pathname.new(dashboard)).to exist expect(dashboard).to have_correct_syntax end @@ -424,7 +424,7 @@ class Foo < Administrate::Generators::TestRecord run_generator ["customer"] - expect(controller).to exist + expect(Pathname.new(controller)).to exist expect(controller).to have_correct_syntax end diff --git a/spec/generators/install_generator_spec.rb b/spec/generators/install_generator_spec.rb index 6b3181629e..549a349f6c 100644 --- a/spec/generators/install_generator_spec.rb +++ b/spec/generators/install_generator_spec.rb @@ -13,7 +13,7 @@ run_generator - expect(controller).to exist + expect(Pathname.new(controller)).to exist expect(controller).to have_correct_syntax expect(controller).to contain <<-RB.strip_heredoc module Admin @@ -27,7 +27,7 @@ class ApplicationController < Administrate::ApplicationController run_generator ["--namespace", "manager"] - expect(controller).to exist + expect(Pathname.new(controller)).to exist expect(controller).to have_correct_syntax expect(controller).to contain <<-RB.strip_heredoc module Manager diff --git a/spec/lib/administrate/search_spec.rb b/spec/lib/administrate/search_spec.rb index fec9bfdf1d..dc3dffbc4e 100644 --- a/spec/lib/administrate/search_spec.rb +++ b/spec/lib/administrate/search_spec.rb @@ -73,8 +73,8 @@ class FooDashboard < Administrate::BaseDashboard Administrate.send(:remove_const, :SearchSpecMocks) end - before { ActiveSupport::Deprecation.silenced = true } - after { ActiveSupport::Deprecation.silenced = false } + before { Administrate.deprecator.silenced = true } + after { Administrate.deprecator.silenced = false } describe "#run" do it "returns all records when no search term" do @@ -159,7 +159,7 @@ class User < ApplicationRecord; end context "when searching through associations" do before do - allow(ActiveSupport::Deprecation).to receive(:warn) + allow(Administrate.deprecator).to receive(:warn) end let(:scoped_object) { Administrate::SearchSpecMocks::Foo } @@ -217,7 +217,7 @@ class User < ApplicationRecord; end search.run - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/:class_name is deprecated/) end end diff --git a/spec/lib/fields/belongs_to_spec.rb b/spec/lib/fields/belongs_to_spec.rb index 14be213bbd..90148f986f 100644 --- a/spec/lib/fields/belongs_to_spec.rb +++ b/spec/lib/fields/belongs_to_spec.rb @@ -88,7 +88,7 @@ describe "class_name option" do before do - allow(ActiveSupport::Deprecation).to receive(:warn) + allow(Administrate.deprecator).to receive(:warn) end it "determines the associated_class" do @@ -134,7 +134,7 @@ resource: line_item, ) field.associated_class - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/:class_name is deprecated/) end end @@ -179,7 +179,7 @@ describe "primary_key option" do before do - allow(ActiveSupport::Deprecation).to receive(:warn) + allow(Administrate.deprecator).to receive(:warn) Foo = Class.new FooDashboard = Class.new @@ -217,14 +217,14 @@ field = association.new(:foo, double(uuid: nil), :baz) field.selected_option - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/:primary_key is deprecated/) end end describe "foreign_key option" do before do - allow(ActiveSupport::Deprecation).to receive(:warn) + allow(Administrate.deprecator).to receive(:warn) end it "determines what foreign key is used on the relationship for the form" do @@ -244,7 +244,7 @@ field.permitted_attribute - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/:foreign_key is deprecated/) end end diff --git a/spec/lib/fields/deferred_spec.rb b/spec/lib/fields/deferred_spec.rb index 45ff380602..1d5eec25bc 100644 --- a/spec/lib/fields/deferred_spec.rb +++ b/spec/lib/fields/deferred_spec.rb @@ -8,7 +8,7 @@ describe "#permitted_attribute" do context "when given a `foreign_key` option" do before do - allow(ActiveSupport::Deprecation).to receive(:warn) + allow(Administrate.deprecator).to receive(:warn) end it "returns the value given" do @@ -26,7 +26,7 @@ foreign_key: :bar, ) deferred.permitted_attribute(:foo, resource_class: LineItem) - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/:foreign_key is deprecated/) end end diff --git a/spec/lib/fields/has_many_spec.rb b/spec/lib/fields/has_many_spec.rb index 027681f69c..a7c07306b1 100644 --- a/spec/lib/fields/has_many_spec.rb +++ b/spec/lib/fields/has_many_spec.rb @@ -36,7 +36,7 @@ let(:dashboard_double) { double(collection_attributes: []) } before do - allow(ActiveSupport::Deprecation).to receive(:warn) + allow(Administrate.deprecator).to receive(:warn) FooDashboard = Class.new allow(FooDashboard).to receive(:new).and_return(dashboard_double) @@ -63,14 +63,14 @@ field = association.new(:customers, [], :show) field.associated_collection - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/:class_name is deprecated/) end end describe "primary_key option" do before do - allow(ActiveSupport::Deprecation).to receive(:warn) + allow(Administrate.deprecator).to receive(:warn) Foo = Class.new FooDashboard = Class.new @@ -108,7 +108,7 @@ field = association.new(:customers, [], :show) field.associated_resource_options - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/:primary_key is deprecated/) end end diff --git a/spec/lib/fields/has_one_spec.rb b/spec/lib/fields/has_one_spec.rb index 52cbb76702..213ec21c4d 100644 --- a/spec/lib/fields/has_one_spec.rb +++ b/spec/lib/fields/has_one_spec.rb @@ -43,7 +43,7 @@ describe ".permitted_attribute" do context "with custom class_name" do before do - allow(ActiveSupport::Deprecation).to receive(:warn) + allow(Administrate.deprecator).to receive(:warn) end it "returns attributes from correct dashboard" do @@ -71,7 +71,7 @@ field_name, resource_class: Product, ) - expect(ActiveSupport::Deprecation).to have_received(:warn). + expect(Administrate.deprecator).to have_received(:warn). with(/:class_name is deprecated/) end end