views:

36

answers:

1

I have a model that is stateful. In each state there are a selection of actions that the user might want to perform on an instance of the model. Currently I am translating the model state to actions that get represented in the view using a view helper.

Something like this... in the model:

Class Thing

  def state_is_A?
    state == 'A'
  end

end

In the helper:

def display_available_actions(thing)
  if thing.state_is_A?
    link_to <action1>
    link_to <action2>
  end
end

And in the view:

<%= display_available_actions(@thing) %>

I don't like the fact that the model state is translated into view actions in the helper. I would like this to be incorporated into the model. On the other hand, it doesn't seem healthy for the model and view to get so coupled.

Is there a Ruby or Rails idiom that suits this kind of situation better than my approach? Should each state be a separate model perhaps?

+1  A: 

I'm always using aasm which gives you those state_is_A? methods automatically as well as SomeModel.states and information about the available transitions.

Thomas R. Koll
Thanks Thomas, I'm going to give aasm a try.
Greg
Thomas, is your link to the definitive AASM? In looking around the web for more info I came across many mentions of multiple AASM forks. Also, any documentation beyond the readme that you would suggest as a starting point?
Greg
for rails2 yes, in rails3 activesupport came with a statemachine but they've excluded it again. funny little bastards ;-)documentation wise, the README is all there is, an rdoc should give you more from the internal API.
Thomas R. Koll