开发者

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

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 tha开发者_高级运维t 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?


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜