Skip to content

Commit

Permalink
Create a deprecator for the library and register it with the Rails app
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
carlosantoniodasilva committed Oct 11, 2023
1 parent 39ee26d commit ee923f8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 7 additions & 1 deletion lib/has_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions lib/has_scope/railtie.rb
Original file line number Diff line number Diff line change
@@ -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

4 changes: 2 additions & 2 deletions test/has_scope_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def apply_scopes(*)
if params[:eval_plant]
super
else
ActiveSupport::Deprecation.silence { super }
HasScope.deprecator.silence { super }
end
end

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

Expand Down

0 comments on commit ee923f8

Please sign in to comment.