Skip to content

Commit

Permalink
Use a dedicated ActiveSupport::Deprecation instance (#2479)
Browse files Browse the repository at this point in the history
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 <nick@nickcharlton.net>
  • Loading branch information
maciek-rr and nickcharlton committed Jan 15, 2024
1 parent 024296f commit 0b740c4
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 39 deletions.
2 changes: 1 addition & 1 deletion app/controllers/concerns/administrate/punditize.rb
Expand Up @@ -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.",
)
Expand Down
12 changes: 8 additions & 4 deletions lib/administrate.rb
Expand Up @@ -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" +
Expand All @@ -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 " +
Expand All @@ -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. " +
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/administrate/field/deferred.rb
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/admin/application_controller_spec.rb
Expand Up @@ -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/),
)
Expand All @@ -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/),
)
Expand Down
12 changes: 6 additions & 6 deletions spec/controllers/admin/orders_controller_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion spec/example_app/config/environments/test.rb
Expand Up @@ -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
4 changes: 2 additions & 2 deletions spec/generators/dashboard_generator_spec.rb
Expand Up @@ -15,7 +15,7 @@

run_generator ["customer"]

expect(dashboard).to exist
expect(Pathname.new(dashboard)).to exist
expect(dashboard).to have_correct_syntax
end

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/generators/install_generator_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/administrate/search_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions spec/lib/fields/belongs_to_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/fields/deferred_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/fields/has_many_spec.rb
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/fields/has_one_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0b740c4

Please sign in to comment.