Here's what I need to do. I have a Tournament model, which is connected to User via Signup (N:N).
The only thing that Signup adds is status of the signup. Tournament has a start time, and users can register only until there is 60 minutes before the tournament starts. After that, registered users can check in. So basically I have two options for the state
In short, models looks like this
class Signup < ActiveRecord::Base
REGISTERED = 0
CHECKED = 1
belongs_to :tournament
belongs_to :user
end
class Tournament < ActiveRecord::Base
has_many :signups
has_many :users, :through => :signups
end
class User < ActiveRecord::Base
has_many :signups
has_many :tournaments, :through => :signups
end
I skipped some code to keep this short. The problem is with the view, since I have a lot of conditions to keep in mind. Here's my actual code (using Slim as a templating engine)
- if logged_in?
- if current_user.registered_for?(@tournament)
- if @tournament.starts_at < 60.minutes.from_now
p Signups are closed, only registered users can now check in
- if current_user.registered_for?(@tournament)
= button_to "Checkin!", { :controller => :signups, :action => :update, :id => @tournament.id }, :method => :put
- else
= button_to "Cancel your registration for the tournament", { :controller => :signups, :action => :destroy, :id => @tournament.id }, :method => :delete
- elsif current_user.checked_in?(@tournament)
p You have already checked in.
- elsif @tournament.starts_at > 60.minutes.from_now
= button_to "Sign up for the tournament", :controller => :signups, :action => :create, :method => :post, :id => @tournament.id
- else
p
| The tournament starts in less than 60 minutes, you can't sign in
- else
p
| You need to
|
= link_to "log in", login_path
| to play
The problem is, I have no idea how to make this much cleaner. I mean yes I can add helpers for buttons, but that won't help me with the if if else else ugliness, because there are many different combinations. Here's a short list:
- user isn't logged in
- it's over 60 until the tournament starts and user hasn't yet registered for the tournament
- it's over 60 until the tournament starts and user is already registered
- it's under 60 minutes, but user isn't registered yet
- it's under 60 minutes and user is registered but hasn't checked in
- it's under 60 minutes and user already checked in
And this is just the tip of the iceberg, because admins should see more information than a regular user, but I don't want to complicate this question.
The main problem is, how should I handle cases like this? It just seems so terrible to do this in a view, but I don't see any other simpler way.