diff --git a/Gemfile b/Gemfile index afa4fcb..c129341 100644 --- a/Gemfile +++ b/Gemfile @@ -35,8 +35,6 @@ group :test do gem 'mocha' - gem 'fabrication' - gem 'faker' gem 'database_cleaner' gem 'spork', '~> 0.9.0.rc' @@ -50,3 +48,8 @@ group :test do gem 'mustang' end + +group :development, :test do + gem 'fabrication' + gem 'faker' +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8b6a294..9e7eeaa 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/contests_controller.rb b/app/controllers/contests_controller.rb index 4ec2bd9..504aa88 100644 --- a/app/controllers/contests_controller.rb +++ b/app/controllers/contests_controller.rb @@ -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 @@ -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 diff --git a/app/controllers/entries_controller.rb b/app/controllers/entries_controller.rb index 5cf662e..014df59 100644 --- a/app/controllers/entries_controller.rb +++ b/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 diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index 026f2ee..aa98291 100644 --- a/app/controllers/submissions_controller.rb +++ b/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']) diff --git a/app/models/contest.rb b/app/models/contest.rb index 2c29b82..89d8e03 100644 --- a/app/models/contest.rb +++ b/app/models/contest.rb @@ -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? @@ -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 @@ -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 diff --git a/app/models/entry.rb b/app/models/entry.rb index a28262d..418e750 100644 --- a/app/models/entry.rb +++ b/app/models/entry.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 8416482..5fce986 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/spec/models/contest_spec.rb b/spec/models/contest_spec.rb index 4d12cdc..19d96d4 100644 --- a/spec/models/contest_spec.rb +++ b/spec/models/contest_spec.rb @@ -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 diff --git a/spec/models/entry_spec.rb b/spec/models/entry_spec.rb index ae23404..28adf47 100644 --- a/spec/models/entry_spec.rb +++ b/spec/models/entry_spec.rb @@ -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 } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 148a8d3..1f86ea9 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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