On Mon, Apr 10, 2006 at 04:28:29AM -0600, Pat M. wrote:
} 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.
This is business logic, and is appropriate for inclusion in your model.
} 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
The above method belongs in your model. If it depended on which user was
trying to vote, the user should be passed as an argument to the method.
} def add_vote
} raise ‘You cannot vote today’ unless Date.today.wday == 2
This method should probably be an action in your controller, and it
almost certainly give a nicer response than an exception. It should also
implemented more like (pseudocode):
render :action => ‘voted’
render :action => ‘rejectvote’
} 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
} 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.
There is no need for an exception at all, just a method on your model
determines whether voting is allowed. If this determination depends on
in the database, the allow_vote? method will be somewhat more complex.
it depends on the game type, meaning it make sense to implement the
allow_vote? method separately for each game type, you probably want to
into Single Table Inheritance (STI) and implement allow_vote? separately