Hey everybody,
I’ve got a model that represents kind of a turn-based games. Certain
actions can only be made unders certain conditions. For simplicity,
we’ll say that the only condition for a particular action is that the
weekday be Tuesday.
def add_vote
raise ‘You cannot vote today’ unless Date.today.wday == 2
…
end
In my controller I want to show a link, but only if the user is
allowed to vote. I’m wondering the best way to do this. I think the
best way is to make another method, which I can call from within both
the model and controller.
def allow_vote?
Date.today.wday == 2
end
def add_vote
raise ‘You cannot vote today’ unless Date.today.wday == 2
…
end
Then in my view I can just do <% unless game.allow_vote? %> to hide
the link. In the controller actions that handle voting, I just rescue
the exception and display an error if necessary.
Does that seem like a good approach, or is there something better? I
need this as some rules vary from game to game, and are stored in the
db for each game.
Pat