How to return value on aasm event?

How to make aasm event return a value other than boolean? I am using aasm 2.2.0

eg. There is a MusicPlayer model that accidentally launches a song at startup

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 playing when the player starts, how can I do this using the play_song method?

+3
source share
1 answer

You cannot do this. The return status is used to indicate whether the transition was sutensual or not. But I'm curious about what use case you have that you need.

But you can wrap the state transition and use the variable set by the method play_song, for example:

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
0

Source: https://habr.com/ru/post/1779553/


All Articles