This repository has been archived on 2020-05-27. You can view files and clone it, but cannot push or open issues/pull-requests.
binaryattraction/app/controllers/votes.rb

65 lines
1.7 KiB
Ruby
Raw Permalink Normal View History

2008-10-01 02:19:35 -04:00
class Votes < Application
before :validate_anonymous_user
before :fetch_allowed_user, :only => [ :show ]
def show
@page = params[:page].to_i
per_page = 4
2008-10-17 04:53:13 -04:00
@votes = @user.votes.find :all, :limit => per_page, :offset => (@page * per_page)
@page_count = (@user.votes.size.to_f / per_page.to_f).ceil
if @votes.empty?
flash[:notice] = 'You need to vote, first'
redirect url(:new_vote)
end
if request.xhr?
partial 'votes/stats_for_user'
else
render
end
end
def new
get_mah_fohtoh
2008-10-01 02:19:35 -04:00
render
end
def create
raise NotAllowed unless request.xhr?
@photo = Photo.find params[:photo_id] rescue nil
@vote = Vote.new :photo_id => (@photo.id rescue true), :vote => (params[:one].to_s == 'true')
if logged_in?
2008-10-17 18:03:28 -04:00
@vote.user_id = current_user.id
else
@vote.session_id = current_user
end
if @vote.save
get_mah_fohtoh
partial 'votes/voting'
else
emsg = "The vote failed: "
@vote.errors.each_full { |e| emsg += e + ' ' }
2008-10-17 18:03:28 -04:00
render emsg, :status => 401, :layout => false
end
end
private
def get_mah_fohtoh
# just a simple check to not allow you to vote on a photo twice. model
# business logic will fail this, too, but just to make sure you don't...
@photo = Photo.find params[:photo_id] if params[:photo_id]
2008-10-17 18:03:28 -04:00
if @photo.nil? or Vote.voted_for?(@photo, current_user)
@photo = Photo.next_available_votable_photo current_user
end
end
def validate_anonymous_user
if !logged_in? and !valid_anonymous_user?
flash[:notice] = 'You must prove that you are a human to continue.'
redirect '/validate_anonymous_user'
else
true
end
end
2008-10-01 02:19:35 -04:00
end