Rails: how to represent available view actions in a stateful model?

Posted by Greg on Stack Overflow See other posts from Stack Overflow or by Greg
Published on 2010-05-24T04:25:31Z Indexed on 2010/05/24 4:30 UTC
Read the original article Hit count: 246

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about mvc