Jason Nerer
Jason Nerer

Reputation: 561

Getting list of states/events from a model that AASM

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.

Upvotes: 10

Views: 9327

Answers (6)

aqwan
aqwan

Reputation: 500

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

Model.aasm.events.map(&:name)

Upvotes: 1

The Worker Ant
The Worker Ant

Reputation: 129

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.

Upvotes: 2

sguha
sguha

Reputation: 2197

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

Upvotes: 9

Jon Snow
Jon Snow

Reputation: 11882

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]

Upvotes: 0

hubert
hubert

Reputation: 278

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.

Upvotes: 1

Ramon Tayag
Ramon Tayag

Reputation: 16084

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.

Upvotes: 5

Related Questions