Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

Develop #17

Merged
merged 6 commits into from Jul 9, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions Gemfile
Expand Up @@ -35,8 +35,6 @@ group :test do

gem 'mocha'

gem 'fabrication'
gem 'faker'
gem 'database_cleaner'

gem 'spork', '~> 0.9.0.rc'
Expand All @@ -50,3 +48,8 @@ group :test do

gem 'mustang'
end

group :development, :test do
gem 'fabrication'
gem 'faker'
end
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -11,5 +11,9 @@ def logged_in?
current_user.present?
end

def authenticate_user!
redirect_to "/auth/github?origin=#{request.env['PATH_INFO']}" unless logged_in?
end

helper_method :current_user, :logged_in?
end
10 changes: 3 additions & 7 deletions app/controllers/contests_controller.rb
Expand Up @@ -2,9 +2,7 @@ class ContestsController < ApplicationController
def index
return redirect_to root_path if request.path == '/contests'

@contests = Contest.all.order_by([:starting_on, :desc]).reject do |contest|
contest.state == 'pending'
end
@contests = Contest.active

respond_to do |format|
format.html
Expand All @@ -15,10 +13,8 @@ def index
def show
@contest = Contest.find_by_slug(params[:id])
if current_user
@voted_entries = @contest.entries.select do |entry|
entry.votes.map(&:user_id).include? current_user.id
end
@entry = @contest.entries.select{ |entry| entry.user == current_user }.first
@voted_entries = current_user.voted_entries(@contest)
@entry = @contest.entries.where(:user_id => current_user.id).first
end
@voted_entries ||= []
end
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/entries_controller.rb
@@ -1,11 +1,10 @@
class EntriesController < ApplicationController
before_filter :authenticate_user!, :only => :new

def new
redirect_to "/auth/github?origin=#{request.env['PATH_INFO']}" unless logged_in?

@contest = Contest.find_by_slug(params[:contest_id])

unless @contest.entries.select{ |entry| entry.user == current_user }.blank?
if @contest.has_entry_from?(current_user)
redirect_to @contest, :alert => 'You already have an entry for this contest.'
end

Expand Down
4 changes: 1 addition & 3 deletions app/controllers/submissions_controller.rb
@@ -1,9 +1,7 @@
require 'wufoo'

class SubmissionsController < ApplicationController
def new
redirect_to "/auth/github?origin=#{request.env['PATH_INFO']}" unless logged_in?
end
before_filter :authenticate_user!, :only => :new

def create
client = Wufoo::Client.new('http://codebrawl.wufoo.com', Codebrawl.config['wufoo']['api_key'])
Expand Down
29 changes: 16 additions & 13 deletions app/models/contest.rb
Expand Up @@ -23,6 +23,10 @@ def self.not_open
Contest.all.reject { |contest| contest.open? }
end

def self.active
scoped.order_by([:starting_on, :desc]).reject { |c| c.pending? }
end

def set_voting_and_closing_dates
self.voting_on = starting_on + 1.week if self.voting_on.blank?
self.closing_on = voting_on + 1.week if self.closing_on.blank?
Expand All @@ -38,21 +42,16 @@ def state
end
end

def pending?
state == 'pending'
def inquirable_state
ActiveSupport::StringInquirer.new(state)
end

def open?
state == 'open'
end

def voting?
state == 'voting'
end

def finished?
state == 'finished'
end
delegate \
:pending?,
:open?,
:voting?,
:finished?,
:to => :inquirable_state

def starting_at
starting_on.to_time.utc + 14.hours
Expand Down Expand Up @@ -94,4 +93,8 @@ def add_participations_to_contestants!
end
end

def has_entry_from?(user)
entries.where(:user_id => user.id).any?
end

end
4 changes: 4 additions & 0 deletions app/models/entry.rb
Expand Up @@ -24,6 +24,10 @@ class Entry
embeds_many :comments
belongs_to :user

def votes_from?(user)
votes.where(:user_id => user.id).any?
end

def errors?
errors.any?
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Expand Up @@ -26,4 +26,8 @@ def calculate_points!
update_attribute(:points, calculate_points)
end

def voted_entries(contest)
contest.entries.select { |e| e.votes_from?(self) }
end

end
20 changes: 20 additions & 0 deletions spec/models/contest_spec.rb
Expand Up @@ -365,4 +365,24 @@

end

describe '#has_entry_from?' do

before do
@contest = Fabricate(:contest)
@user = Fabricate(:user)
end

subject { @contest.has_entry_from?(@user) }

context 'when the user is a participant' do
before { @contest.entries.create(:user => @user) }
it { should be_true }
end

context 'when the user is not a participant' do
it { should be_false }
end

end

end
17 changes: 17 additions & 0 deletions spec/models/entry_spec.rb
Expand Up @@ -2,6 +2,23 @@

describe Entry do

describe '#votes_from?' do
let(:entry) { Fabricate(:entry) }
let(:user) { Fabricate(:user) }

subject { entry.votes_from?(user) }

context 'with votes by the user' do
before { entry.votes.create(:user => user) }
it { should be_true }
end

context 'without votes by the user' do
it { should be_false }
end

end

context 'fabrication' do

it { Fabricate(:entry).should be_valid }
Expand Down
14 changes: 14 additions & 0 deletions spec/models/user_spec.rb
Expand Up @@ -56,4 +56,18 @@

end

describe '#voted_entries' do

let(:contest) { Fabricate(:contest) }
let(:user) { Fabricate(:user) }
let(:entry) { contest.entries.create }

before { entry.stubs(:votes_from?).with(user).returns(true) }

it 'returns entries the user has voted on' do
user.voted_entries(contest).should include(entry)
end

end

end