pluginaweek / state_machine

Adds support for creating state machines for attributes on any Ruby class
http://www.pluginaweek.org
MIT License
3.74k stars 508 forks source link

Class.human_state_name stops returning real human name after transition #246

Open iabdulin opened 11 years ago

iabdulin commented 11 years ago

In Ticket.rb I have

state_machine do
    event :next_step do
      transition :new => :issue_details_collected
      transition :issue_details_collected => :carrier_details_collected
      transition :carrier_details_collected => :closed
    end

    state :new, :human_name => 'Issue Details'

    state :issue_details_collected, :human_name => 'Carrier Details' do
    end

    state :carrier_details_collected, :human_name => 'Resolution' do
    end

    state :closed, :human_name => 'Closed' do
    end
  end

In console:

Ticket.human_state_name :issue_details_collected
=> "Carrier Details"

Ticket.first.next_step

Ticket.human_state_name :issue_details_collected
=> "issue details collected"

What's wrong?

obrie commented 11 years ago

Hey @iabdulin -

Any chance the code that's referencing the state name is modifying the string in-place? I can't reproduce in a simple example using state_machine 1.2.0:

require 'state_machine'

class Ticket
  state_machine do
    event :next_step do
      transition :new => :issue_details_collected
      transition :issue_details_collected => :carrier_details_collected
      transition :carrier_details_collected => :closed
    end

    state :new, :human_name => 'Issue Details'

    state :issue_details_collected, :human_name => 'Carrier Details' do
    end

    state :carrier_details_collected, :human_name => 'Resolution' do
    end

    state :closed, :human_name => 'Closed' do
    end
  end
end

Ticket.human_state_name(:issue_details_collected) # => "Carrier Details" 
ticket = Ticket.new                               # => #<Ticket:0x87c6e1c>
ticket.state = 'new'                              # => "new"
ticket.next_step                                  # => true
Ticket.human_state_name(:issue_details_collected) # => "Carrier Details"

If you could provide a reproducible example or ensure that no other code is referencing the human name value, that'd be great. There's really nothing special that state_machine is doing with that string under the hood.

iabdulin commented 11 years ago

Here's the example repo: https://github.com/iabdulin/state_machine_human_name_issue

obrie commented 11 years ago

@iabdulin Awesome find... looks like it has to do with the ActiveModel integration assuming that you'll be specifying the human name option in the locale. I'll look into fixing this over the next week or so.

iabdulin commented 11 years ago

I've updated the repo with another test. There are 2 models Ticket and Ticket2, both in ticket.rb. The only diff between classes is order of defining event and states. But the behaviour is different. Run Ticket.test in console.

obrie commented 11 years ago

Just FYI -- haven't forgotten about this .. just been a little busy lately.

iabdulin commented 11 years ago

cool

mibamur commented 10 years ago

is solution ready?