Skip to content

Commit

Permalink
Fix a bug where, within a helper Path(), the last step couldn't
Browse files Browse the repository at this point in the history
explicitly define additional `Output()`, e.g. for `:failure`.

```ruby
... => Path(connect_to: Track(:success)) do
  step :upload,
    Output(Trailblazer::Activity::Left, :failure) => Track(:failure) # FIXME: this configuration gets lost.
end
```
  • Loading branch information
apotonick committed Mar 1, 2024
1 parent a45c2dd commit 4ab466e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/trailblazer/activity/dsl/linear/helper/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,29 @@ def convert_path_to_track(track_color: "track_#{rand}", connect_to: nil, before:
# Connect last row of the {sequence} to the given step via its {Id}
# Useful when steps needs to be inserted in between {Start} and {connect Id()}.
private def connect_for_sequence(sequence, connect_to:)
output, _ = sequence[-1][2][0].(sequence, sequence[-1]) # FIXME: the Forward() proc contains the row's Output, and the only current way to retrieve it is calling the search strategy. It should be Forward#to_h
last_step_on_path = sequence[-1]
output_searches = last_step_on_path[2]

last_step_outputs =
output_searches.collect do |search_strategy|
# TODO: introduce {search_strategy.to_h} so we don't need to execute it here.
output, _ = search_strategy.(sequence, last_step_on_path) # FIXME: the Forward() proc contains the row's Output, and the only current way to retrieve it is calling the search strategy. It should be Forward#to_h
output
end

# we want to reconnect the last step's {:success} output, everything else we keep.
success_output = last_step_outputs.find { |output| output.to_h[:semantic] == :success } or raise

# FIXME: what about End()?
success_search = Sequence::Search.ById(success_output, connect_to.value) if connect_to.instance_of?(Linear::Normalizer::OutputTuples::Id)
success_search = Sequence::Search.Forward(success_output, connect_to.color) if connect_to.instance_of?(Linear::Normalizer::OutputTuples::Track) # FIXME: use existing mapping logic!

success_output_index = last_step_outputs.index(success_output)

# searches = [Search.ById(output, connect_to.value)]
searches = [Sequence::Search.ById(output, connect_to.value)] if connect_to.instance_of?(Linear::Normalizer::OutputTuples::Id)
searches = [Sequence::Search.Forward(output, connect_to.color)] if connect_to.instance_of?(Linear::Normalizer::OutputTuples::Track) # FIXME: use existing mapping logic!
output_searches[success_output_index] = success_search # replace the success search strategy. # DISCUSS: a bit cryptical with this index.

row = sequence[-1]
row = row[0..1] + [searches] + [row[3]] # FIXME: not mutating an array is so hard: we only want to replace the "searches" element, index 2
row = last_step_on_path
row = row[0..1] + [output_searches] + [row[3]] # FIXME: not mutating an array is so hard: we only want to replace the "searches" element, index 2
row = Sequence::Row[*row]

sequence[0..-2] + [row]
Expand Down
36 changes: 36 additions & 0 deletions test/docs/path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,39 @@ class Charge < Trailblazer::Activity::Railway
}
end
end

class DocsPathWithRailwayOptionTest < Minitest::Spec
Memo = Class.new

module Memo::Activity
class Attach < Trailblazer::Activity::Railway
step :upload_exists?,
Output(:failure) => Path(connect_to: Track(:success)) do
step :aws_signin,
Output(Trailblazer::Activity::Left, :failure) => Track(:failure)
step :upload,
Output(Trailblazer::Activity::Left, :failure) => Track(:failure) # FIXME: this configuration gets lost.
end
end
end

it "we can explicitly connect {:failure} outputs in a Path(), even the last one" do
assert_process_for Memo::Activity::Attach, :success, :failure, %(
#<Start/:default>
{Trailblazer::Activity::Right} => <*upload_exists?>
<*upload_exists?>
{Trailblazer::Activity::Left} => <*aws_signin>
{Trailblazer::Activity::Right} => #<End/:success>
<*aws_signin>
{Trailblazer::Activity::Right} => <*upload>
{Trailblazer::Activity::Left} => #<End/:failure>
<*upload>
{Trailblazer::Activity::Right} => #<End/:success>
{Trailblazer::Activity::Left} => #<End/:failure>
#<End/:success>
#<End/:failure>)
end
end

# TODO: add {Path(railway: true)}

0 comments on commit 4ab466e

Please sign in to comment.