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 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
3 changes: 1 addition & 2 deletions lib/trailblazer/macro/contract/persist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ def self.Persist(method: :save, name: "default")
step = ->(ctx, **) { ctx[path].send(method) }

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

{
task: task,
id: "persist.save"
id: "persist.#{method}"
}
end
end
Expand Down
15 changes: 13 additions & 2 deletions test/operation/contract_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Form < Reform::Form
->(*) { validate(options["params"][:song]) } # <-- TODO
end

class UpdateHit < Update
step Contract::Persist( method: :sync ), id: 'persist.syncer'
step Contract::Persist()
end

# success
it do
result = Update.(params: {title: "SVG"})
Expand All @@ -38,6 +43,12 @@ class Form < Reform::Form
result[:"result.contract.default"].errors.messages.must_equal({:title=>["can't be blank"]})
end

# override id
it 'override id' do
railway = Trailblazer::Developer.railway(UpdateHit)
assert_equal railway, %([>model.build,>contract.build,>contract.default.validate,>persist.syncer,>persist.save])
end

#---
# Contract::Validate[key: :song]
class Upsert < Trailblazer::Operation
Expand Down Expand Up @@ -72,14 +83,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
2 changes: 1 addition & 1 deletion trailblazer-macro-contract.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
spec.email = ["[email protected]"]
spec.description = 'Operation macros for form objects'
spec.summary = 'Macros for form objects: Build, Validate, Persist'
spec.homepage = "http://trailblazer.to"
spec.homepage = "https://trailblazer.to"
spec.license = "LGPL-3.0"

spec.files = `git ls-files -z`.split("\x0").reject do |f|
Expand Down
Loading