开发者

How to return a value on aasm event?

开发者 https://www.devze.com 2023-01-29 15:29 出处:网络
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开发者_开发知识库

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
0

精彩评论

暂无评论...
验证码 换一张
取 消