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

feat: Add dynamic ID generation for contract persistence #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions lib/trailblazer/macro/contract/persist.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Trailblazer
module Macro
module Contract
Expand All @@ -6,10 +8,14 @@ def self.Persist(method: :save, name: "default")
step = ->(ctx, **) { ctx[path].send(method) }

task = Activity::Circuit::TaskAdapter.for_step(step)

id = if name == 'default'
"persist.#{method}"
else
"#{path}.persist.#{method}"
end
{
task: task,
id: "persist.save"
id: id
}
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/operation/contract_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ class Form < Reform::Form
class New < Upsert
end

it { Trailblazer::Developer.railway(New).must_equal %{[>model.build,>contract.build,>contract.default.validate,>persist.save]} }
it { Trailblazer::Developer.railway(New).must_equal %{[>model.build,>contract.build,>contract.default.validate,>persist.sync]} }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the path variable nil?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no.


#- overwriting Validate
class NewHit < Upsert
step Contract::Validate(key: :hit), override: true
end

it { Trailblazer::Developer.railway(NewHit).must_equal %{[>model.build,>contract.build,>contract.default.validate,>persist.save]} }
it { Trailblazer::Developer.railway(NewHit).must_equal %{[>model.build,>contract.build,>contract.default.validate,>persist.sync]} }
it { NewHit.(params: {:hit => { title: "Hooray For Me" }}).inspect(:model).must_equal %{<Result:true [#<struct ContractTest::Song title=\"Hooray For Me\">] >} }
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/operation/persist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def self.call(options, **); options["2. fail"] = "Persist" end
step Model( Song, :new )
step Contract::Build( constant: Form )
step Contract::Validate()
fail Fail1
left Fail1
step Contract::Persist()
fail Fail2
left Fail2
end

it { Create.(params: { title: "In Recital" })[:model].title.must_equal "In Recital" }
Expand Down
Loading