1

I use pluginaweek's state_machine in an ActiveRecord for a Tester-Management. I retrieve an array of all possible states with this call:

irb(main):013:0> Tester.state_machine.states.map &:name
=> [:candidate, :contract_sent, :contract_received, :contract_wont_return, :active, :retired]

Now, I wan't to build a search form with meta_search to retrieve those testers with a particular state. I'd like to use a select:

- states = {}; Tester.state_machine.states.map(&:name).each{ |e| states[t(e.to_s)] = e }
= form.input :state_equals, :as => :select, :collection => states

It works, but it's not nice, because I build a new hash and translate each element. Also the translation must be inserted twice, because my solution don't use the I18n yaml-structure for state_machine...

Is there a a method like

Tester.state_machine.states.map(&:human_state_name)

or

Tester.state_machine.human_states_name.map(&:name)

to get the translated states?

Christian
  • 618
  • 1
  • 7
  • 15

1 Answers1

1

I've found the solution. You get the translation for the states of state_mache with:

- states = {}; Tester.state_machine.states.map() { |s| states[s.human_name] = s.name }
= form.input :state_equals, :as => :select, :collection => states
Christian
  • 618
  • 1
  • 7
  • 15
  • There is a more concise and flexible answer to this in [another answer](http://stackoverflow.com/questions/4138437/get-list-of-state-machine-states) – MikeH May 13 '13 at 15:57