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

Can't pass an empty hash as callback parameters to transition_from since 5.5.0 #829

Open
y-yagi opened this issue May 31, 2023 · 2 comments · May be fixed by #833
Open

Can't pass an empty hash as callback parameters to transition_from since 5.5.0 #829

y-yagi opened this issue May 31, 2023 · 2 comments · May be fixed by #833

Comments

@y-yagi
Copy link
Contributor

y-yagi commented May 31, 2023

Describe the bug

Can't pass an empty hash as callback parameters to transition_from since 5.5.0

To Reproduce

Reproduce script is here.

# frozen_string_literal: true
# bug_report_aasm.rb 

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "aasm", "5.5.0"
  gem "rspec"
end

class Job
  include AASM

  aasm do
    state :sleeping, initial: true
    state :running, :cleaning

    event :run do
      transitions from: :sleeping, to: :running, after: :prepare_run!
    end

    event :clean do
      transitions from: :running, to: :cleaning
    end
  end

  def prepare_run!(params)
    puts params
  end
end

require "aasm/rspec"
require "rspec/autorun"

RSpec.describe Job do
  it "move to 'run' with parameters" do
    job = Job.new
    expect(job).to transition_from(:sleeping).to(:running).on_event(:run, {})
  end
end
$ ruby bug_report_aasm.rb   
F

Failures:

  1) Job move to 'run' with parameters
     Failure/Error:
       def prepare_run!(params)
         puts params
       end

     ArgumentError:
       wrong number of arguments (given 0, expected 1)
     # bug_report_aasm.rb:28:in `prepare_run!'
     # bug_report_aasm.rb:39:in `block (2 levels) in <main>'

Finished in 0.00177 seconds (files took 0.05501 seconds to load)
1 example, 1 failure

Failed examples:

rspec bug_report_aasm.rb:37 # Job move to 'run' with parameters

Expected behavior

Allow to pass an empty hash as callback parameters via transition_from

Additional context

This has happened since #808.

y-yagi added a commit to y-yagi/aasm that referenced this issue Jun 11, 2023
Since Ruby 2.7, a double splat with an empty hash passes no arguments.
So if an argument is an empty hash we should avoid it.
Ref: https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/#other-minor-changes-empty-hash

Fix aasm#829.
@ccartano
Copy link

Would this be caused because in your event transition definition

transitions from: :sleeping, to: :running, after: :prepare_run!

the after: :prepare_run! isnt passing anything. You'd have to do something like after: proc { prepare_run!({thing: other})
or you could default the def prepare_run!(params = {})

@y-yagi
Copy link
Contributor Author

y-yagi commented Jun 14, 2023

@ccartano
This works until v5.4.0 and I've already sent the PR to fix this bug #833.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants