Skip to content

Commit

Permalink
Support deprecation.rails (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
yykamei authored Apr 4, 2022
1 parent 4481652 commit efd87b0
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ These are Rails Instrumentation API hooks supported by this gem so far.

| Event name | Supported |
| ----------------------------------------------------------------------------------------------------------- | --------- |
| [`deprecation.rails`](https://guides.rubyonrails.org/active_support_instrumentation.html#deprecation-rails) | |
| [`deprecation.rails`](https://guides.rubyonrails.org/active_support_instrumentation.html#deprecation-rails) | |

## Contributing

Expand Down
1 change: 1 addition & 0 deletions lib/rails_band.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'rails_band/version'
require 'rails_band/configuration'
require 'rails_band/base_event'
require 'rails_band/deprecation_subscriber'
require 'rails_band/railtie'

# Rails::Band unsubscribes all default LogSubscribers from Rails Instrumentation API,
Expand Down
40 changes: 40 additions & 0 deletions lib/rails_band/deprecation_subscriber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module RailsBand
# DeprecationSubscriber is responsible for logging deprecation warnings.
class DeprecationSubscriber < ::ActiveSupport::LogSubscriber
# DeprecationEvent is a wrapper around a deprecation notification event.
class DeprecationEvent < BaseEvent
def message
@message ||= @event.payload.fetch(:message)
end

def callstack
@callstack ||= @event.payload.fetch(:callstack)
end

def gem_name
@gem_name ||= @event.payload.fetch(:gem_name)
end

def deprecation_horizon
@deprecation_horizon ||= @event.payload.fetch(:deprecation_horizon)
end
end

mattr_accessor :consumers

def deprecation(event)
consumer&.call(DeprecationEvent.new(event))
end

private

def consumer
# HACK: ActiveSupport::Subscriber has the instance variable @namespace, but it's not documented.
# This hack might possibly break in the future.
namespace = self.class.instance_variable_get(:@namespace)
consumers[:"deprecation.#{namespace}"] || consumers[:deprecation] || consumers[:default]
end
end
end
3 changes: 3 additions & 0 deletions lib/rails_band/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class Railtie < ::Rails::Railtie
RailsBand::ActiveSupport::LogSubscriber.consumers = consumers
RailsBand::ActiveSupport::LogSubscriber.attach_to :active_support

RailsBand::DeprecationSubscriber.consumers = consumers
RailsBand::DeprecationSubscriber.attach_to :rails

if defined?(::ActiveJob)
require 'active_job/logging'

Expand Down
6 changes: 6 additions & 0 deletions test/dummy/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def cache3
redirect_to users_path
end

def deprecation
ActiveSupport::Deprecation.behavior = :notify
ActiveSupport::Deprecation.warn('deprecated!!!')
redirect_to users_path
end

private

def halt!
Expand Down
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
get :cache
get :cache2
get :cache3
get :deprecation
end

resources :yay, only: %i[index show]
Expand Down
56 changes: 56 additions & 0 deletions test/rails_band/deprecation_subscriber_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require 'test_helper'

class DeprecationSubscriberTest < ActionDispatch::IntegrationTest
setup do
@event = nil
end

test 'use the consumer with the exact event name' do # rubocop:disable Minitest/MultipleAssertions
RailsBand::DeprecationSubscriber.consumers = {
'deprecation.rails': ->(event) { @event = event }
}
get '/users/1/deprecation'
assert_match(/DEPRECATION WARNING: deprecated!!!/, @event.message)
assert_equal 'Rails', @event.gem_name
assert_instance_of String, @event.deprecation_horizon
@event.callstack.each do |location|
assert_instance_of Thread::Backtrace::Location, location
end
end

test 'use the consumer with namespace' do # rubocop:disable Minitest/MultipleAssertions
RailsBand::DeprecationSubscriber.consumers = {
deprecation: ->(event) { @event = event }
}
get '/users/1/deprecation'
assert_match(/DEPRECATION WARNING: deprecated!!!/, @event.message)
assert_equal 'Rails', @event.gem_name
assert_instance_of String, @event.deprecation_horizon
@event.callstack.each do |location|
assert_instance_of Thread::Backtrace::Location, location
end
end

test 'use the consumer with default' do # rubocop:disable Minitest/MultipleAssertions
RailsBand::DeprecationSubscriber.consumers = {
default: ->(event) { @event = event }
}
get '/users/1/deprecation'
assert_match(/DEPRECATION WARNING: deprecated!!!/, @event.message)
assert_equal 'Rails', @event.gem_name
assert_instance_of String, @event.deprecation_horizon
@event.callstack.each do |location|
assert_instance_of Thread::Backtrace::Location, location
end
end

test 'do not use the consumer because the event is not for the target' do
RailsBand::DeprecationSubscriber.consumers = {
'unknown.rails': ->(event) { @event = event }
}
get '/users/1/deprecation'
assert_nil @event
end
end

0 comments on commit efd87b0

Please sign in to comment.