Skip to content

Commit

Permalink
refactor: put database connector on rack env
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 22, 2019
1 parent 931079d commit 9f635c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/rack/pact_broker/database_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ def initialize app, database_connection
def call env
set_database_connector
if use_transaction? env
call_with_transaction env
call_with_transaction(add_database_connector(env))
else
call_without_transaction env
call_without_transaction(add_database_connector(env))
end
ensure
clear_database_connector
end

def add_database_connector(env)
env.merge("pactbroker.database_connector" => @default_database_connector)
end

def use_transaction? env
TRANS_METHODS.include? env[REQUEST_METHOD]
end
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/rack/pact_broker/database_transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ module PactBroker

subject { self.send(http_method, "/") }

it "sets the pactbroker.database_connector on the env" do
actual_env = nil
allow(api).to receive(:call) do | env |
actual_env = env
[200, {}, {}]
end
subject
expect(actual_env).to have_key("pactbroker.database_connector")
end

context "for get requests" do
let(:http_method) { :get }

Expand Down

0 comments on commit 9f635c3

Please sign in to comment.