Getting list of states/events from a model that AASM
Asked Answered
P

6

10

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.

Pastiche answered 7/6, 2010 at 11:7 Comment(0)
D
9

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

Douglassdougy answered 21/5, 2013 at 23:44 Comment(1)
ClassName.aasm.states.map(&:name) will get you an Array of Symbols, which may be what you want.Decontrol
D
4

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.

Dijon answered 26/10, 2010 at 15:55 Comment(1)
That's deprecated now. Use ClassName.aasm.states_for_select.Frieze
L
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.

Litchi answered 29/4, 2014 at 19:27 Comment(0)
F
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.

Fibrinolysis answered 5/12, 2010 at 3:38 Comment(0)
E
1

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

Model.aasm.events.map(&:name)
Electron answered 3/1, 2023 at 14:50 Comment(0)
P
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]

Pairs answered 5/11, 2012 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.