Skip to content

Instantly share code, notes, and snippets.

@sdrew
Created July 29, 2011 20:19
Show Gist options
  • Save sdrew/24a14d0de2778346ff05 to your computer and use it in GitHub Desktop.
Save sdrew/24a14d0de2778346ff05 to your computer and use it in GitHub Desktop.
Admin::ContestsController
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