3

How do I make an aasm event return a value other than boolean? I'm using aasm 2.2.0

E.g. There is a MusicPlayer model which randomly plays a song when started

aasm_state :started, :after_enter => :play_song
aasm_state :stopped
aasm_event :start do
  :transitions :from => :stopped, :to => :started
end

def play_song
  # select and play a song randomly and return the song object
end

Now if I want to return the song that is currently being played on starting the player, how do I do it through the 'play_song' method?

Vignesh
  • 558
  • 5
  • 15

1 Answers1

0

You can't do that. The return status is used to indicate if the transition was sussessful or not. But I'm curious what use case you have that you need that.

But you can wrap the state transition and use a variable that's set by the play_song method, like this:

aasm_state :started, :after_enter => :play_song
aasm_state :stopped
aasm_event :start do
  :transitions :from => :stopped, :to => :started
end

def play_song
  # select and play a song randomly
  @song = ...
end

def shuffle
  if start
    @song
  end
end
alto
  • 609
  • 4
  • 8
  • The scenario above is the use case. – Vignesh Dec 27 '12 at 12:39
  • Hm, I see. You not only want to know if is playing, but also what is played. This is not a problem to be easily solved by a state machine. Just imagine you have a couple of callbacks, for example one before_enter and one after_enter callback. Which result would you return then? I probably would keep that logic out of the state machine and wrap the state transition method. Let me update my answer for what I mean. – alto Dec 28 '12 at 02:23
  • That's right. And it actually makes more sense because current song gives the state of the music player. – Vignesh Jan 07 '13 at 13:37