Skip to content

Commit

Permalink
Update after_commit behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloh committed Feb 4, 2019
1 parent c68ed2a commit 400536d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.9.0] - 2019-04-01
### Changed
- Changed behavior for `:after_commit` step wrapper, on `:sequel_models` plugin, to capture current state and reuse it later when executing.

## [0.8.0] - 2018-10-01
### Changed
- Added support for `dry-validation` 0.12.x
Expand Down
11 changes: 6 additions & 5 deletions lib/pathway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ module ClassMethods
def process(&bl)
dsl = self::DSL
define_method(:call) do |input|
dsl.new(self, input).run(&bl).then(&:result)
dsl.new(State.new(self, input: input), self)
.run(&bl)
.then(&:result)
end
end

Expand Down Expand Up @@ -120,9 +122,8 @@ def self.apply(klass)
end

module DSLMethods
def initialize(operation, input)
@result = wrap(State.new(operation, input: input))
@operation = operation
def initialize(state, operation)
@result, @operation = wrap(state), operation
end

def run(&bl)
Expand Down Expand Up @@ -155,7 +156,7 @@ def map(callable)

def around(wrapper, &steps)
@result.then do |state|
seq = -> { @result = dup.run(&steps) }
seq = -> (dsl = self) { @result = dsl.run(&steps) }
_callable(wrapper).call(seq, state)
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/pathway/plugins/sequel_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ def transaction(&bl)
end

def after_commit(&bl)
around(-> steps, _ {
around(-> steps, state {
dsl = self.class::DSL.new(State.new(self, state.to_hash.dup), self)

db.after_commit do
steps.call
steps.call(dsl)
end
}, &bl)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pathway/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Pathway
VERSION = '0.8.0'
VERSION = '0.9.0'
end
48 changes: 44 additions & 4 deletions spec/plugins/sequel_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,62 @@ class MyOperation < Operation

model MyModel, search_by: :email

process do
step :fetch_model
end
end

class MailerOperation < MyOperation
process do
transaction do
step :fetch_model
after_commit do
step :send_emails
end
end
step :as_hash
end

def as_hash(state)
state[:my_model] = { model: state[:my_model] }
end

def send_emails(my_model:,**)
@mailer.send_emails(my_model) if @mailer
end
end

class SubOperation < MyOperation; end
class ChainedOperation < MyOperation
result_at :result

let(:mailer) { double.tap { |d| allow(d).to receive(:send_emails) } }
let(:operation) { MyOperation.new(mailer: mailer) }
process do
transaction do
set :chain_operation, to: :result
end
end

def chain_operation(input:,**)
opr = MailerOperation.new(mailer: @mailer)
opr.call(input)
end
end

class SubOperation < MyOperation; end

describe 'DSL' do
let(:result) { operation.call(params) }
let(:params) { { email: 'asd@fgh.net' } }
let(:model) { double }

let(:operation) { MailerOperation.new(mailer: mailer) }
let(:mailer) { double.tap { |d| allow(d).to receive(:send_emails) } }

describe '#transaction' do
it 'returns the result state provided by the inner transaction when successful' do
allow(MyModel).to receive(:first).with(params).and_return(model)

expect(result).to be_a_success
expect(result.value).to eq(model)
expect(result.value).to eq(model: model)
end

it "returns the error state provided by the inner transaction when there's a failure" do
Expand All @@ -71,9 +97,23 @@ class SubOperation < MyOperation; end
expect(mailer).to_not receive(:send_emails)
expect(result).to be_a_failure
end

context 'when the state after if changed after the callback is set' do
let(:operation) { ChainedOperation.new(mailer: mailer) }

it 'ignores state changes that took place on the remaining steps' do
allow(MyModel).to receive(:first).with(params).and_return(model)
expect(mailer).to receive(:send_emails).with(model)

expect(result).to be_a_success
expect(result.value).to eq(model: model)
end
end
end
end

let(:operation) { MyOperation.new }

describe '.model' do
it "sets the 'result_key' using the model class name" do
expect(operation.result_key).to eq(:my_model)
Expand Down

0 comments on commit 400536d

Please sign in to comment.