Skip to content

Commit

Permalink
no more prizes to hand out and picking winners require prizes to stil…
Browse files Browse the repository at this point in the history
…l exist.
  • Loading branch information
jwang committed Jul 24, 2011
1 parent 803c03b commit aa6aa3c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
12 changes: 8 additions & 4 deletions app/controllers/entries_controller.rb
Expand Up @@ -23,10 +23,14 @@ def show
end

def pick
winner = Entry.pick_winner
winner.won = true
winner.save
redirect_to(root_path, :notice => "Winner is #{winner.name}")
prizes = Prize.remaining
if prizes.count > 0
prize = prizes.first
winner = prize.pick_winner
winner.won = true
winner.save
redirect_to(root_path, :notice => "Winner is #{winner.name} - #{prize.name}")
end
end

end
9 changes: 9 additions & 0 deletions app/models/prize.rb
Expand Up @@ -4,4 +4,13 @@ class Prize < ActiveRecord::Base

scope :remaining, where("amount > 0")

def pick_winner
if self.amount > 0
winner = Entry.pick_winner
self.amount -= 1
self.save
winner
end
end

end
22 changes: 16 additions & 6 deletions features/contest_over.feature
Expand Up @@ -4,9 +4,19 @@ Feature: Contest over
I want to pick the winners for the prizes

Scenario: Pick the winners
Given I am a logged in admin user
And I have entries
And I have prizes
When I go to the home page
And I press "Pick a winner"
Then I should see "Winner is"
Given I am a logged in admin user
And I have entries
And I have prizes
When I go to the home page
And I press "Pick a winner"
Then I should see "Winner is"

Scenario: No More Prizes
Given I am a logged in admin user
And I have entries
And I have no more prizes
When I go to the home page
Then I should see "Sorry, no more prizes to hand out."



4 changes: 4 additions & 0 deletions features/step_definitions/entry_steps.rb
Expand Up @@ -9,6 +9,10 @@
end
end

Given /^I have no more prizes$/ do
Prize.remaining.count.should eql(0)
end

Given /^I have prizes$/ do
@prize = Factory.create(:prize)
Prize.count.should > 0
Expand Down
9 changes: 9 additions & 0 deletions spec/models/prize_spec.rb
Expand Up @@ -23,4 +23,13 @@
prize.should_not be_valid
end

it "should have one less amount after it is handed out" do
[1,2,3].each do |i|
entry = Factory.create(:entry, :name => "john #{i}")
end
@prize.amount.should eql(1)
@prize.pick_winner
@prize.amount.should eql(0)
end

end

0 comments on commit aa6aa3c

Please sign in to comment.