From ee923f8b7c75f1cf051332f32cbeb9dd293c0b90 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 11 Oct 2023 09:12:30 -0300 Subject: [PATCH] Create a deprecator for the library and register it with the Rails app We can no longer use a single deprecator instance from Active Support, instead each library should create their own and register with the app so the application deprecators can control things like silencing and other behaviors across the board. --- CHANGELOG.md | 1 + lib/has_scope.rb | 8 +++++++- lib/has_scope/railtie.rb | 10 ++++++++++ test/has_scope_test.rb | 4 ++-- 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 lib/has_scope/railtie.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index eecb6ff..8fb1bac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Unreleased * Add support for Rails 7.1. (no changes required.) +* Add `HasScope.deprecator` to integrate with new application deprecators in Rails 7.1. ## 0.8.1 diff --git a/lib/has_scope.rb b/lib/has_scope.rb index d9e62df..1f28f3a 100644 --- a/lib/has_scope.rb +++ b/lib/has_scope.rb @@ -11,6 +11,10 @@ module HasScope default: [[ String, Numeric ]], } + def self.deprecator + @deprecator ||= ActiveSupport::Deprecation.new("1.0", "HasScope") + end + def self.included(base) base.class_eval do extend ClassMethods @@ -201,7 +205,7 @@ def apply_scope_to_action?(options) #:nodoc: def applicable?(string_proc_or_symbol, expected) #:nodoc: case string_proc_or_symbol when String - ActiveSupport::Deprecation.warn <<-DEPRECATION.squish + HasScope.deprecator.warn <<-DEPRECATION.squish [HasScope] Passing a string to determine if the scope should be applied is deprecated and it will be removed in a future version of HasScope. DEPRECATION @@ -222,6 +226,8 @@ def current_scopes end end +require 'has_scope/railtie' if defined?(Rails) + ActiveSupport.on_load :action_controller do include HasScope helper_method :current_scopes if respond_to?(:helper_method) diff --git a/lib/has_scope/railtie.rb b/lib/has_scope/railtie.rb new file mode 100644 index 0000000..a0abf4f --- /dev/null +++ b/lib/has_scope/railtie.rb @@ -0,0 +1,10 @@ +require 'rails/railtie' + +module HasScope + class Railtie < Rails::Railtie + initializer "has_scope.deprecator" do |app| + app.deprecators[:has_scope] = HasScope.deprecator if app.respond_to?(:deprecators) + end + end +end + diff --git a/test/has_scope_test.rb b/test/has_scope_test.rb index d0cf862..c61abdc 100644 --- a/test/has_scope_test.rb +++ b/test/has_scope_test.rb @@ -57,7 +57,7 @@ def apply_scopes(*) if params[:eval_plant] super else - ActiveSupport::Deprecation.silence { super } + HasScope.deprecator.silence { super } end end @@ -175,7 +175,7 @@ def test_scope_with_eval_string_if_and_unless_options_is_deprecated Tree.expects(:eval_plant).with('value').returns(Tree) Tree.expects(:all).returns([mock_tree]) - assert_deprecated(/Passing a string to determine if the scope should be applied is deprecated/) do + assert_deprecated(/Passing a string to determine if the scope should be applied is deprecated/, HasScope.deprecator) do get :index, params: { eval_plant: 'value', skip_eval_plant: nil } end