Skip to content
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
23 changes: 14 additions & 9 deletions lib/tapioca/dsl/compilers/aasm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,19 @@ def decorate
create_rest_param("opts", type: "T.untyped"),
create_block_param("block", type: "T.nilable(T.proc.void)"),
]
aasm_6_or_newer = ::Gem::Requirement.new(">= 6").satisfied_by?(::Gem::Version.new(::AASM::VERSION))

state_machine.events.each do |event|
model.create_method(event.name.to_s, parameters: parameters)
model.create_method("#{event.name}!", parameters: parameters)
model.create_method("#{event.name}_without_validation!", parameters: parameters)
model.create_method("may_#{event.name}?", return_type: "T::Boolean")
# As of aasm 6.0 a namespaced state machine defines only the
# namespaced event methods (e.g. `run_foo!`) and not the plain
# methods (`run!`) as aliases.
unless namespace && aasm_6_or_newer
model.create_method(event.name.to_s, parameters: parameters)
model.create_method("#{event.name}!", parameters: parameters)
model.create_method("#{event.name}_without_validation!", parameters: parameters)
model.create_method("may_#{event.name}?", return_type: "T::Boolean")
end

# For events, if there's a namespace the default methods are created in addition to
# namespaced ones.
next unless namespace

name = "#{event.name}_#{namespace}"
Expand All @@ -110,9 +115,9 @@ def decorate
model.create_method("#{name}!", parameters: parameters)
model.create_method("may_#{name}?", return_type: "T::Boolean")

# There's no namespaced method created for `_without_validation`, so skip
# defining a method for:
# "#{name}_without_validation!"
# aasm < 6 does not define a namespaced `_without_validation!`
# method.
model.create_method("#{name}_without_validation!", parameters: parameters) if aasm_6_or_newer
end
end

Expand Down
11 changes: 1 addition & 10 deletions spec/tapioca/dsl/compilers/aasm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,26 +358,17 @@ def foo_running?; end
sig { returns(T::Boolean) }
def foo_sleeping?; end

sig { returns(T::Boolean) }
def may_run?; end

sig { returns(T::Boolean) }
def may_run_foo?; end

sig { params(opts: T.untyped, block: T.nilable(T.proc.void)).returns(T.untyped) }
def run(*opts, &block); end

sig { params(opts: T.untyped, block: T.nilable(T.proc.void)).returns(T.untyped) }
def run!(*opts, &block); end

sig { params(opts: T.untyped, block: T.nilable(T.proc.void)).returns(T.untyped) }
def run_foo(*opts, &block); end

sig { params(opts: T.untyped, block: T.nilable(T.proc.void)).returns(T.untyped) }
def run_foo!(*opts, &block); end

sig { params(opts: T.untyped, block: T.nilable(T.proc.void)).returns(T.untyped) }
def run_without_validation!(*opts, &block); end
def run_foo_without_validation!(*opts, &block); end

class << self
sig { params(args: T.untyped, block: T.nilable(T.proc.bind(PrivateAASMMachine).void)).returns(PrivateAASMMachine) }
Expand Down
Loading