9

I successfully integrated the most recent AASM gem into an application, using it for the creation of a wizard. In my case I have a model order

class Order < ActiveRecord::Base

  belongs_to :user
  has_one :billing_plan, :dependent => :destroy
  named_scope :with_user, ..... <snip>

  include AASM

  aasm_column :aasm_state
  aasm_initial_state :unauthenticated_user

  aasm_state :unauthenticated_user, :after_exit => [:set_state_completed]
  aasm_state : <snip>

  <and following the event definitions>

end

Now I would like to give an administrator the possibility to create his own graphs through the AASM states. Therefore I created two additional models called OrderFlow and Transition where there order_flow has many transitions and order belongs_to order_flow.

No problem so far. Now I would like to give my admin the possibility to dynamically add existing transitions / events to an order_flow graph.

The problem now is, that I do not find any possibility to get a list of all events / transitions out of my order model. aasm_states_for_select seems to be the correct candidate, but I cannot call it on my order model.

Can anyone help?

Thx in advance. J.

John Topley
  • 113,588
  • 46
  • 195
  • 237
Jason Nerer
  • 551
  • 7
  • 19

6 Answers6

8

With version 3.0.18, you can should use ClassName.aasm.states

sguha
  • 2,057
  • 1
  • 22
  • 36
  • 4
    `ClassName.aasm.states.map(&:name)` will get you an Array of Symbols, which may be what you want. – rattray May 16 '16 at 19:05
3

I don't understand how OrderFlow works with Order and Transitions, but I assumed you just included that to explain your scenario better.

ClassName.aasm_states_for_select gives you a list of states that is declared in the model.

Ramon Tayag
  • 15,224
  • 9
  • 43
  • 69
2

For events in 3.1.1 I used Model.aasm.events.keys to get an array of event name symbols. In recent versions .map(&:name) won't do it for you.

The Worker Ant
  • 119
  • 2
  • 7
1

also, not 100% sure what you're asking for, but if you want all the states and events declared for your model you could get those by calling Order.aasm_states and Order.aasm_events respectively.

hubert
  • 278
  • 3
  • 12
1

In version 5.4.0 this seems to now be an array, so we are back to

Model.aasm.events.map(&:name)
aqwan
  • 470
  • 4
  • 6
0

A more elegant Ruby syntax can be used, as in this example in IRB below. You get all the allowable states in an array of symbols.

1.9.3-p0 :011 > ApprovalRequest.aasm_states.map(&:name)

=> [:created, :submitted, :rejected, :approved]

Zack Xu
  • 11,505
  • 9
  • 70
  • 78