Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace RequestStore with native ActiveSupport::CurrentAttributes #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/lograge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
require 'active_support'
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/string/inflections'
require 'active_support/current_attributes'

# rubocop:disable Metrics/ModuleLength
module Lograge
module_function

mattr_accessor :logger, :application, :ignore_tests

class Current < ActiveSupport::CurrentAttributes
attribute :lograge_location, :lograge_unpermitted_params
end

# Custom options that will be appended to log line
#
# Currently supported formats are:
Expand Down
14 changes: 7 additions & 7 deletions lib/lograge/log_subscribers/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ def process_action(event)
end

def redirect_to(event)
RequestStore.store[:lograge_location] = event.payload[:location]
Lograge::Current.lograge_location = event.payload[:location]
end

def unpermitted_parameters(event)
RequestStore.store[:lograge_unpermitted_params] ||= []
RequestStore.store[:lograge_unpermitted_params].concat(event.payload[:keys])
Lograge::Current.lograge_unpermitted_params ||= []
Lograge::Current.lograge_unpermitted_params.concat(event.payload[:keys])
end

private
Expand Down Expand Up @@ -56,18 +56,18 @@ def extract_runtimes(event, payload)
end

def extract_location
location = RequestStore.store[:lograge_location]
location = Lograge::Current.lograge_location
return {} unless location

RequestStore.store[:lograge_location] = nil
Lograge::Current.lograge_location = nil
{ location: strip_query_string(location) }
end

def extract_unpermitted_params
unpermitted_params = RequestStore.store[:lograge_unpermitted_params]
unpermitted_params = Lograge::Current.lograge_unpermitted_params
return {} unless unpermitted_params

RequestStore.store[:lograge_unpermitted_params] = nil
Lograge::Current.lograge_unpermitted_params = nil
{ unpermitted_params: unpermitted_params }
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/lograge/log_subscribers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'active_support'
require 'active_support/core_ext/class/attribute'
require 'active_support/log_subscriber'
require 'request_store'

module Lograge
module LogSubscribers
Expand Down
1 change: 0 additions & 1 deletion lograge.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'actionpack', '>= 4'
s.add_runtime_dependency 'activesupport', '>= 4'
s.add_runtime_dependency 'railties', '>= 4'
s.add_runtime_dependency 'request_store', '~> 1.0'
end
12 changes: 6 additions & 6 deletions spec/log_subscribers/action_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@

it 'stores the location in a thread local variable' do
subscriber.redirect_to(redirect_event)
expect(RequestStore.store[:lograge_location]).to eq('http://example.com')
expect(Lograge::Current.lograge_location).to eq('http://example.com')
end
end

Expand All @@ -94,7 +94,7 @@

it 'stores the parameters in a thread local variable' do
subscriber.unpermitted_parameters(unpermitted_parameters_event)
expect(RequestStore.store[:lograge_unpermitted_params]).to eq(%w[foo bar])
expect(Lograge::Current.lograge_unpermitted_params).to eq(%w[foo bar])
end
end

Expand Down Expand Up @@ -170,7 +170,7 @@

context 'with a redirect' do
before do
RequestStore.store[:lograge_location] = 'http://www.example.com?key=value'
Lograge::Current.lograge_location = 'http://www.example.com?key=value'
end

it 'adds the location to the log line' do
Expand All @@ -180,7 +180,7 @@

it 'removes the thread local variable' do
subscriber.process_action(event)
expect(RequestStore.store[:lograge_location]).to be_nil
expect(Lograge::Current.lograge_location).to be_nil
end
end

Expand All @@ -191,7 +191,7 @@

context 'with unpermitted_parameters' do
before do
RequestStore.store[:lograge_unpermitted_params] = %w[florb blarf]
Lograge::Current.lograge_unpermitted_params = %w[florb blarf]
end

it 'adds the unpermitted_params to the log line' do
Expand All @@ -201,7 +201,7 @@

it 'removes the thread local variable' do
subscriber.process_action(event)
expect(RequestStore.store[:lograge_unpermitted_params]).to be_nil
expect(Lograge::Current.lograge_unpermitted_params).to be_nil
end
end

Expand Down
Loading