Skip to content

Commit

Permalink
Use Rails.application global in middleware to retrieve the config
Browse files Browse the repository at this point in the history
It looks like the @app in the middleware is not an instance of `Rails::Application`
so we cannot just read config from there. But we can use the Rails global
and read configuration from there
  • Loading branch information
jakubkosinski committed Apr 30, 2018
1 parent 0d3ef23 commit 331834b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 7 additions & 3 deletions rails_event_store/lib/rails_event_store/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def initialize(app)
end

def call(env)
if @app.config.respond_to?(:event_store)
@app.config.event_store.with_metadata(request_metadata(env)) do
if config.respond_to?(:event_store)
config.event_store.with_metadata(request_metadata(env)) do
@app.call(env)
end
else
Expand All @@ -19,9 +19,13 @@ def request_metadata(env)
end

private

def config
Rails.application.config
end

def metadata_proc
@app.config.x.rails_event_store.request_metadata if @app.config.x.rails_event_store.request_metadata.respond_to?(:call)
config.x.rails_event_store.request_metadata if config.x.rails_event_store.request_metadata.respond_to?(:call)
end

def default_request_metadata
Expand Down
8 changes: 5 additions & 3 deletions rails_event_store/spec/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
module RailsEventStore
RSpec.describe Middleware do
specify 'calls app within with_metadata block when app has configured the event store instance' do
app.config.event_store = event_store = Client.new
expect(app).to receive(:call).with(dummy_env)
middleware = Middleware.new(app)
::Rails.application.config.event_store = event_store = Client.new
::Rails.application.config.x = ::Rails::Application::Configuration::Custom.new
expect(event_store).to receive(:with_metadata).with(request_id: 'dummy_id', remote_ip: 'dummy_ip').and_call_original
middleware.call(dummy_env)
end
Expand All @@ -22,17 +23,17 @@ module RailsEventStore
end

specify 'use config.rails_event_store.request_metadata' do
app.config.x.rails_event_store.request_metadata = kaka_dudu
middleware = Middleware.new(app)
::Rails.application.config.x.rails_event_store.request_metadata = kaka_dudu

expect(middleware.request_metadata(dummy_env)).to eq({
kaka: 'dudu'
})
end

specify 'use config.rails_event_store.request_metadata is not callable' do
app.config.x.rails_event_store.request_metadata = {}
middleware = Middleware.new(app)
::Rails.application.config.x.rails_event_store.request_metadata = {}

expect(middleware.request_metadata(dummy_env)).to eq({
request_id: 'dummy_id',
Expand All @@ -42,6 +43,7 @@ module RailsEventStore

specify 'use config.rails_event_store.request_metadata is not set' do
middleware = Middleware.new(app)
::Rails.application.config.x = ::Rails::Application::Configuration::Custom.new

expect(middleware.request_metadata(dummy_env)).to eq({
request_id: 'dummy_id',
Expand Down

0 comments on commit 331834b

Please sign in to comment.