Created
July 29, 2011 20:19
-
-
Save sdrew/24a14d0de2778346ff05 to your computer and use it in GitHub Desktop.
Admin::ContestsController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Admin::ContestsController < ApplicationController | |
def draw | |
redirect_to(root_url, :alert => 'Unauthorized') and return unless(current_user.present? && current_user.admin?) | |
contestants = Contestant.select([:id, :points]).elegible.for_contest(params[:id]) | |
entries = [] | |
contestants.each { |c| c.points.times { entries << c.id } } | |
unless entries.blank? | |
entries.shuffle! | |
index = Random.rand(entries.count) | |
selected = Contestant.find(entries[index], :include => :user) | |
selected.winner = true | |
selected.save | |
flash[:notice] = "Winner is <a href=\"#{url_for rails_admin_edit_path(:model_name => 'users', :id => selected.user.id)}\" target=\"_blank\">#{selected.user.full_name}</a>".html_safe | |
else | |
flash[:error] = 'No Contestants available.' | |
end | |
redirect_to rails_admin_edit_path(:model_name => 'contests', :id => params[:id]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment