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?
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
精彩评论