Skip to content

Commit

Permalink
implement Find::Build().
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Jul 2, 2023
1 parent b17a7df commit a208fa5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
52 changes: 28 additions & 24 deletions lib/trailblazer/macro/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@ def self.Model(model_class = nil, action = :new, find_by_key = :id, id: "model.b
end

class Model
def self.for()

end

# New API for retrieving models by ID.
# Only handles keyword argument style.
def self.Find(model_class, positional_method = nil, params_key: nil, id: "model.build", not_found_terminus: false, **keyword_options, &block)
def self.Find(model_class, positional_method = nil, params_key: nil, id: "model.find", not_found_terminus: false, **keyword_options, &block)
raise "unknown options #{keyword_options}" if keyword_options.size > 1

task =
Expand All @@ -91,6 +87,14 @@ def self.Find(model_class, positional_method = nil, params_key: nil, id: "model.
)
end

options = options_for(task, id: id)

options = options.merge(Activity::Railway.Output(:failure) => Activity::Railway.End(:not_found)) if not_found_terminus

options
end

def self.options_for(task, id:)
options = Activity::Railway.Subprocess(task).merge(id: id)

inject = {
Expand All @@ -103,14 +107,17 @@ def self.Find(model_class, positional_method = nil, params_key: nil, id: "model.

options = options.merge(inject)
options = options.merge(out)

options = options.merge(Activity::Railway.Output(:failure) => Activity::Railway.End(:not_found)) if not_found_terminus

options
end

# Finder activity consists of two steps:
# {extract_id}, and the finder code.
#
# |-- model.build
# | |-- Start.default
# | |-- extract_id
# | |-- finder.Trailblazer::Macro::Model::Find::Positional
# | `-- End.success
# |-- validate
def self.finder_activity_for(params_key:, finder:, **, &block)
id_from =
if block
Expand All @@ -127,14 +134,6 @@ def self.finder_activity_for(params_key:, finder:, **, &block)
end
end

def self.no_arg(action:, **)
activity = Class.new(Activity::Railway) do
step Finder.method(:new)
end

return activity, action
end

# Runtime code.
module Find
class Positional
Expand All @@ -160,15 +159,20 @@ def call(ctx, id:, **)
end
end



def self.new(ctx, **)
model_class = ctx[:"model.class"]
action = ctx[:"model.action"]

ctx[:model] = model_class.send(action)
class NoArgument < Positional
def call(ctx, **)
ctx[:model] = @model_class.send(@find_method)
end
end
end # Find

def self.Build(model_class, method = :new, id: "model.build")
activity = Class.new(Activity::Railway) do
step Find::NoArgument.new(model_class: model_class, find_method: method)
end

options_for(activity, id: id)
end
end
end # Macro
end
29 changes: 29 additions & 0 deletions test/docs/model/find_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,32 @@ class Update < Trailblazer::Activity::Railway
# end
# #~ctx_to_result end
# end

# new
class DocsModelNewTest < Minitest::Spec
Song = Class.new(DocsModelFindTest::Song)

#:new
module Song::Activity
class Create < Trailblazer::Activity::Railway
step Model::Build(Song, :new)
step :validate
step :save
#~meths
include T.def_steps(:validate, :save)
#~meths end
end
end
#:new end

#~ctx_to_result
it do
#:new-invoke
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, seq: [])
ctx[:model] #=> #<struct Song id=1>
#:new-invoke end

assert_equal ctx[:model].inspect, %{#<struct #{Song} id=nil>}
end
#~ctx_to_result end
end

0 comments on commit a208fa5

Please sign in to comment.