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

Commit

Permalink
Merge branch 'release/0.4.12'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Kreeftmeijer committed Sep 25, 2011
2 parents 8ce1f05 + f8d412f commit f692462
Show file tree
Hide file tree
Showing 26 changed files with 447 additions and 270 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ gem 'arnold', :git => 'git://github.com/codebrawl/arnold.git', :branch => 'devel

group :test do
gem 'rspec-rails'
gem 'shoulda-matchers'
gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git', :ref => '549e6733'
gem 'launchy'

Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ GEM
ffi (>= 1.0.7)
json_pure
rubyzip
shoulda-matchers (1.0.0.beta3)
spork (0.9.0.rc7)
sprockets (2.0.0)
hike (~> 1.2)
Expand Down Expand Up @@ -312,6 +313,7 @@ DEPENDENCIES
rpm_contrib
rspec-rails
sass-rails
shoulda-matchers
spork (~> 0.9.0.rc)
thin
timecop
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/application.css.sass
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ form
.tip a
font-size: 100%

#contests
margin-bottom: -10px

#entries, #articles
margin-bottom: -20px

Expand Down
13 changes: 4 additions & 9 deletions app/controllers/contests_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class ContestsController < ApplicationController
def index
return redirect_to root_path if request.path == '/contests'

def index
@contests = Contest.active

respond_to do |format|
Expand All @@ -11,13 +10,9 @@ def index
end

def show
not_found unless @contest = Contest.find_by_slug(params[:id])

if current_user
@voted_entries = @contest.voted_entries(current_user)
@entry = @contest.entries.where(:user_id => current_user.id).first
end
@voted_entries ||= []
@contest = Contest.by_slug(params[:id])
@entry = @contest.entries.by_user(current_user)
@voted_entries = @contest.voted_entries(current_user)
end

end
10 changes: 3 additions & 7 deletions app/controllers/entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ class EntriesController < ApplicationController
before_filter :authenticate_user!, :only => :new

def new
@contest = Contest.find_by_slug(params[:contest_id])
not_found unless @contest && @contest.open?

if @contest.has_entry_from?(current_user)
redirect_to @contest, :alert => 'You already have an entry for this contest.'
end

@contest = Contest.by_slug(params[:contest_id]).if_open
@entry = @contest.entries.new

@contest.not_found if @contest.has_entry_from?(current_user)
end

def create
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/homepage_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class HomepageController < ApplicationController

def index
@contests = Contest.active[0..1]
end

end
14 changes: 12 additions & 2 deletions app/models/contest.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'state'
require 'time_from_date_field'
require 'mongoid_extensions'

class Contest
include Mongoid::Document
Expand All @@ -19,7 +20,7 @@ class Contest
include TimeFromDateField
include State

validates :user, :name, :description, :starting_on, :presence => true
validates :user, :name, :description, :starting_on, :tagline, :presence => true
validates :tagline, :length => { :minimum => 50 }

before_create :set_voting_and_closing_dates
Expand All @@ -35,6 +36,15 @@ def self.active
scoped.order_by([:starting_on, :desc]).reject { |c| c.pending? }
end

def self.by_slug(slug)
contest = first(:conditions => {:slug => slug})
contest || raise(Mongoid::Errors::DocumentNotFound.new(Contest, slug))
end

def if_open
open? ? self : not_found
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 @@ -59,7 +69,7 @@ def has_entry_from?(user)
end

def voted_entries(user)
entries.select { |e| e.votes_from?(user) }
user ? entries.select { |e| e.votes_from?(user) } : []
end

end
4 changes: 4 additions & 0 deletions app/models/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Entry
embeds_many :comments
belongs_to :user

def self.by_user(user)
user ? first(:conditions => {:user_id => user.id}) : nil
end

def votes_from?(user)
votes.where(:user_id => user.id).any?
end
Expand Down
11 changes: 1 addition & 10 deletions app/views/contests/index.haml
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
- content_for(:description){ 'Codebrawl, a code contest website focussed on the Ruby programming language and open source software.' }

- unless @contests.map(&:state).include? 'open'
%section.tip
%h1 Get ready for next week's contest!
%p
:erb
Our next contest will start next Monday at 14:00 (UTC). If you want to be sure you don't miss it, just subscribe to our <%= link_to 'contests feed', 'http://feeds.feedburner.com/codebrawl' %>. Also, don't forget to check out and vote for the entries in <%= link_to "last week's contest", @contests.first %>.

#contests
= render @contests
= render @contests
7 changes: 0 additions & 7 deletions app/views/contests/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@
%p
:erb
You can keep updating your gist, we'll take a snapshot of it when the voting period begins. If you want, you can always <%= link_to "delete your entry", [@contest, @entry], :method => :delete %>.

%h3
:erb
You entered <%= link_to "gist #{@entry.gist_id}", "https://gist.github.com/#{@entry.gist_id}" %>
%p
:erb
You can keep updating your gist, we'll take a snapshot of it when the voting period begins. If you want, you can always <%= link_to "delete your entry", [@contest, @entry], :method => :delete %>.
- if @contest.finished?
.message
%h3 This contest is finished
Expand Down
13 changes: 13 additions & 0 deletions app/views/homepage/index.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- content_for(:description){ 'Codebrawl, a code contest website focussed on the Ruby programming language and open source software.' }

- unless @contests.map(&:state).include? 'open'
%section.tip
%h1 Get ready for next week's contest!
%p
:erb
Our next contest will start next Monday at 14:00 (UTC). If you want to be sure you don't miss it, just subscribe to our <%= link_to 'contests feed', 'http://feeds.feedburner.com/codebrawl' %>. Also, don't forget to check out and vote for the entries in <%= link_to "last week's contest", @contests.first %>.

#contests
= render @contests

= link_to 'Contest archive', contests_path
2 changes: 1 addition & 1 deletion app/views/layouts/application.haml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

%nav
%ul
%li= link_to 'Contests', :root
%li= link_to 'Contests', :contests
%li= link_to 'Hall of Fame', :users
//%li= link_to 'News', :articles
%li.user
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

get '/rules' => "application#rules", :as => 'rules'

root :to => 'contests#index'
root :to => 'homepage#index'

end
9 changes: 9 additions & 0 deletions lib/mongoid_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module MongoidExtensions

def not_found
raise Mongoid::Errors::DocumentNotFound.new(self.class, id)
end

end

Mongoid::Document.send(:include, MongoidExtensions)
174 changes: 30 additions & 144 deletions spec/acceptance/contest_index_spec.rb
Original file line number Diff line number Diff line change
@@ -1,162 +1,48 @@
require 'acceptance/acceptance_helper'

feature 'Homepage' do
feature 'Contest index' do

scenario 'be redirected to the homepage when accessing /contests' do
# TODO: Move this one to a controller spec
visit '/contests'
URI.parse(current_url).path.should == '/'
end

context "on the homepage" do

background :all do
@voting = Fabricate(
background(:all) do
VCR.use_cassette('existing_gist') do
Fabricate(
:contest,
:name => 'Fun with ChunkyPNG',
:tagline => 'Having a bit of with image manipulation in ChunkyPNG',
:voting_on => Date.yesterday.to_time
)
VCR.use_cassette('existing_gist') do
@finished = Fabricate(
:contest,
:name => 'Improving Ruby',
:tagline => 'Build verything you ever wanted, monkey-patched into Ruby',
:entries => [Fabricate.build(:entry_with_files, :user => Fabricate(:user, :login => 'bob'))] * 3,
:closing_on => Date.yesterday.to_time
)
end
@pending = Fabricate(
:contest,
:name => 'RSpec extensions',
:tagline => 'Giving back to RSpec by building the funniest or most useful RSpec formatter',
:starting_on => Date.tomorrow.to_time
:tagline => 'Having a bit of fun with image manipulation in ChunkyPNG',
:closing_on => Date.yesterday.to_time,
:entries => [
Fabricate.build(
:entry_with_files,
:user => Fabricate(:user, :login => 'bob')
)
]
)

visit '/'
end

scenario 'see the "no open contests"-message' do
within('.tip') do
page.should have_content "Get ready for next week's contest"
page.should have_content 'Our next contest will start next Monday at 14:00 (UTC)'
page.should have_link 'contests feed'
body.should include 'href="http://feeds.feedburner.com/codebrawl"'
page.should have_link "last week's contest"
body.should include 'href="/contests/fun-with-chunkypng"'
end
end

context 'when having an open contest' do

background :all do
@user = Fabricate(:user)
VCR.use_cassette('existing_gist') do
@open = Fabricate(
:contest,
:name => 'Euler #74',
:tagline => 'Get your Euler on and build the fastest solution to problem #123',
:starting_on => Date.yesterday.to_time,
:entries => [Fabricate.build(:entry_with_files, :user => Fabricate(:user, :login => 'bob'))]
)
end
end

before { visit '/' }

scenario 'see the Codebrawl header' do
page.should have_content 'Codebrawl'
end

scenario 'do not see the "no open contests"-message' do
page.should have_no_content "Get ready for next week's contest"
end

scenario 'return to the homepage after clicking the header' do
click_link 'Fun with ChunkyPNG'
click_link 'Codebrawl'
['Euler #74', 'Fun with ChunkyPNG'].each do |name|
page.should have_link name
end
end

scenario 'return to the homepage after clicking the "Contests" menu item' do
click_link 'Fun with ChunkyPNG'
click_link 'Contests'
['Euler #74', 'Fun with ChunkyPNG'].each do |name|
page.should have_link name
end
end

scenario 'see a list of contest names' do
['Euler #74', 'Fun with ChunkyPNG', 'Improving Ruby'].each do |name|
page.should have_link name
end

page.should have_no_content 'RSpec extensions'
end

scenario 'see entry counts in the contests list' do
page.should have_content "1 entry already"
end

scenario 'see the contest taglines' do
['Get your Euler on and build the fastest solution to problem #123', 'Having a bit of with image manipulation in ChunkyPNG', 'Build verything you ever wanted, monkey-patched into Ruby'].each do |tagline|
page.should have_content tagline
end
end

scenario 'see the contest states' do
within "#contest_#{@open.id}" do
page.should have_content 'Open'
end

within "#contest_#{@voting.id}" do
page.should have_content 'Voting'
end

within "#contest_#{@finished.id}" do
page.should have_content 'Finished'
end
end

scenario 'see the contest winners avatars and medals' do
within "#contest_#{@finished.id}" do
page.should have_css('img.medal')
page.should have_css('img.gravatar')
end
end

scenario "visit the winner's entry" do
within "#contest_#{@finished.id}" do
page.find(:xpath, "//ol[@class='winners']//a[1]").click
end
page.should have_content "This contest is finished"
end
visit '/contests'

scenario 'visit the submissions page' do
click_link 'Submit a contest idea'
page.should have_content 'Submit your contest idea'
end
end

scenario 'visit the hall of fame' do
click_link 'Hall of Fame'
within('#main') { page.should have_content 'Hall of Fame' }
end
scenario 'see links to the contests' do
page.should have_link 'Fun with ChunkyPNG'
end

context 'after logging in' do
scenario 'see the contest taglines' do
page.should have_content 'Having a bit of fun with image manipulation in ChunkyPNG'
end

background { login_via_github }
scenario 'see the contest states' do
page.should have_content 'Finished'
end

scenario 'visit my profile page' do
click_link 'charlie'
page.should have_content 'Total points'
end
scenario 'see the contest winners' do
page.should have_css('ol.winners li a img.gravatar')
end

end
scenario 'click a contest winner avatar' do
find('ol.winners li a').click
page.should have_content 'This contest is finished'
end

end

end

end
Loading

0 comments on commit f692462

Please sign in to comment.