Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Commit

Permalink
Merge pull request #180 from 18F/develop
Browse files Browse the repository at this point in the history
Deploy #2 12/30
  • Loading branch information
adelevie committed Dec 30, 2015
2 parents a62b0c4 + dace2a7 commit 58a762f
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 28 deletions.
9 changes: 9 additions & 0 deletions app/helpers/auction_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module AuctionHelper
def auction_status(auction)
if auction.available?
'Open'
else
'Closed'
end
end
end
8 changes: 8 additions & 0 deletions app/models/presenter/auction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@ def available?
)
end

def over?
model.end_datetime < Time.now
end

def user_is_winning_bidder?(user)
return false if !current_bid?
user.id == current_bid.bidder_id
end

def user_is_bidder?(user)
bids.detect {|b| user.id == b.bidder_id } != nil
end

def html_description
return '' if description.blank?
markdown.render(description)
Expand Down
54 changes: 54 additions & 0 deletions app/views/auctions/_win_header.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<% if auction.over? %>
<% if auction.bids? %>
<% if current_user %>
<% if auction.user_is_winning_bidder?(current_user) %>
<div class="usa-alert usa-alert-success auction-alert">
<div class="usa-alert-body"><h3>You are the winner</h3></div>
<p class="usa-alert-text">Congratulations! We will contact you with further instructions.</p>
</div>
<% elsif auction.user_is_bidder?(current_user) %>
<div class="usa-alert usa-alert-error auction-alert">
<div class="usa-alert-body"><h3>You are not the winner</h3></div>
<p class="usa-alert-text">Someone else placed a lower bid than you.</p>
</div>
<% end %>
<% else %>
<div class="usa-alert usa-alert-info auction-alert">
<div class="usa-alert-body">
<h3 class="usa-alert-heading">Auction Now Closed</h3>
</div>
</div>
<% end %>
<% else %>
<div class="usa-alert usa-alert-info auction-alert">
<div class="usa-alert-body">
<h3 class="usa-alert-heading">Auction Now Closed</h3>
<p class="usa-alert-text">This auction ended with no bids.</p>
</div>
</div>
<% end %>
<% elsif auction.available? && auction.bids? && current_user && auction.user_is_bidder?(current_user) %>
<% if flash["bid"] %>
<div class="usa-alert usa-alert-success">
<div class="usa-alert-body">
<h3 class="usa-alert-heading">Bid Submitted! You currently have the winning bid.</h3>
<p class="usa-alert-text">If your bid is selected as the winner, we will contact you with further instructions. <a href="<%= my_bids_path %>">View your bids »</a></p>
</div>
</div>
<% elsif auction.user_is_winning_bidder?(current_user) %>
<div class="usa-alert usa-alert-success auction-alert">
<div class="usa-alert-body">
<h3 class="usa-alert-heading">You currently have the winning bid.</h3>
<p class="usa-alert-text">If your bid is selected as the winner, we will contact you with further instructions.</p>
</div>
</div>
<% else %>
<div class="usa-alert usa-alert-error auction-alert">
<div class="usa-alert-body">
<h3 class="usa-alert-heading">You currently do not have the winning bid.</h3>
<p class="usa-alert-text">You must enter a new lower bid to win this auction.</p>
</div>
</div>
<% end %>
<%# display nothing if in the future %>
<% end %>
34 changes: 16 additions & 18 deletions app/views/auctions/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<% if flash["bid"] %>
<div class="usa-alert usa-alert-success">
<div class="usa-alert-body">
<h3 class="usa-alert-heading">Bid Submitted! You currently have the winning bid.</h3>
<p class="usa-alert-text">If your bid is selected as the winner, we will contact you with further instructions. <a href="<%= my_bids_path %>">View your bids »</a></p>
</div>
</div>
<% end %>
<%= render partial: 'win_header', locals: {auction: @auction} %>

<div class="usa-grid-full">
<p><a href="<%= root_path %>">« Back to open projects</a></p>
Expand All @@ -19,18 +12,23 @@
<div class="auction-detail-panel">
<div class="auction-info">
<p class="auction-label">Status:</p>
<p class="auction-label-info"><% if @auction.available? %>Open<% else %>Closed<% end %></p>
<p class="auction-label">Current Bid:</p>
<p class="auction-label-info"><%= auction_status(@auction) %></p>
<!-- <hr/> -->
<% if @auction.over? && @auction.bids? %>
<p class="auction-label">Winning bid (<%= @auction.current_bidder_name %>):</p>
<% else %>
<p class="auction-label">Current bid:</p>
<% end %>
<p class="auction-label-info"><span class="bid-amount"><%= number_to_currency(@auction.current_bid_amount) %></span>
<span class=""><%=
link_to(
pluralize(@auction.bids.length, 'bid'),
auction_bids_path(@auction.id)
)
%>
</span>
<span class=""><%=
link_to(
pluralize(@auction.bids.length, 'bid'),
auction_bids_path(@auction.id)
)
%>
</span>
</p>
<p class="auction-label">Bid deadline:</p>
<p class="auction-label"><% if @auction.over? %>Auction ended at:<% else %>Bid deadline:<% end %></p>
<p class="auction-label-info"><%= @auction.end_datetime.strftime("%m/%d/%Y at %I:%M %Z") %></p>
<p>
<a href="<%= new_auction_bid_path(@auction) %>" class='usa-button usa-button'>BID »</a>
Expand Down
11 changes: 7 additions & 4 deletions spec/factories/auctions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@

trait :with_bidders do
after(:build) do |instance|
(1..rand(10)+1).each do |i|
amount = 3499 - (100 * i) - rand(30)
instance.bids << FactoryGirl.create(:bid, auction: instance, amount: amount)
Timecop.freeze(instance.start_datetime) do
Timecop.scale(3600)
(1..4).each do |i|
amount = 3499 - (20 * i) - rand(10)
instance.bids << FactoryGirl.create(:bid, auction: instance, amount: amount)
end
end
end
end

trait :closed do
end_datetime { Time.now - 1.day }
end
Expand Down
101 changes: 99 additions & 2 deletions spec/features/bidder_interacts_with_auction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
click_on("Submit")

# returns us back to the bid page
expect(page).to have_content("Current Bid:")
expect(page).to have_content("Current bid:")
expect(page).to have_content("$800.00")
end

Expand All @@ -103,7 +103,7 @@
click_on("Submit")

# returns us back to the bid page
expect(page).to have_content("Current Bid:")
expect(page).to have_content("Current bid:")
expect(page).to have_content("$999.00")
expect(page).to have_content("You currently have the winning bid.")
end
Expand Down Expand Up @@ -194,6 +194,103 @@
end
end

scenario "Viewing auction page for a closed auction where a user is not authenticated" do
create_closed_auction
auction = Presenter::Auction.new(@auction)
visit auction_path(auction.id)

expect(page).to have_css('.usa-alert-info')
expect(page).to have_css('.auction-alert')
expect(page).to have_content("Winning bid (#{auction.current_bidder_name}):")
expect(page).not_to have_content("Current bid:")

expect(page).to have_content("Auction ended at:")
expect(page).not_to have_content("Bid deadline:")
end

scenario "Viewing auction page for a closed auction where authenticated user is the winner" do
create_closed_auction

visit "/"
sign_in_bidder

bid = nil
Timecop.freeze(@auction.end_datetime) do
bid = @auction.bids.create(bidder_id: @bidder.id, amount: 1)
end
expect(bid).to be_valid
@auction.reload

auction = Presenter::Auction.new(@auction)

expect(auction.current_bidder_name).to eq(@bidder.name)
visit auction_path(auction.id)

expect(page).to have_css('.usa-alert-success')
expect(page).to have_content("You are the winner")
expect(page).to have_content("Winning bid (#{auction.current_bidder_name}):")
expect(page).not_to have_content("Current bid:")

expect(page).to have_content("Auction ended at:")
expect(page).not_to have_content("Bid deadline:")
end

scenario "Viewing auction page for a closed auction where authenticated user is not the winner" do
create_closed_auction

visit '/'
sign_in_bidder

auction = Presenter::Auction.new(@auction)

bid = nil
Timecop.freeze(@auction.start_datetime) do
bid = @auction.bids.create(bidder_id: @bidder.id, amount: 3498)
end
expect(bid).to be_valid

visit auction_path(auction.id)

expect(page).to have_css('.usa-alert-error')
expect(page).to have_content("You are not the winner")
expect(page).to have_content("Winning bid (#{auction.current_bidder_name}):")
expect(page).not_to have_content("Current bid:")

expect(page).to have_content("Auction ended at:")
expect(page).not_to have_content("Bid deadline:")
end

scenario "Viewing auction page for a closed auction where authenticated user has not placed bids" do
create_closed_auction
auction = Presenter::Auction.new(@auction)

visit '/'
sign_in_bidder

visit auction_path(auction.id)

expect(page).to_not have_css('.usa-alert-error')
expect(page).to_not have_content("You are not the winner")
expect(page).to have_content("Winning bid (#{auction.current_bidder_name}):")
expect(page).not_to have_content("Current bid:")

expect(page).to have_content("Auction ended at:")
expect(page).not_to have_content("Bid deadline:")
end

scenario "Viewing auction page for a closed auction with no bidders" do
create_closed_bidless_auction
auction = Presenter::Auction.new(@auction)
visit auction_path(auction.id)

expect(page).to have_content("This auction ended with no bids.")
expect(page).to have_content("Current bid:")

expect(page).to have_content("Auction ended at:")
expect(page).not_to have_content("Bid deadline:")
end


scenario "Viewing bid history for a closed auction" do
Timecop.scale(36000) do
create_closed_auction
Expand Down
72 changes: 69 additions & 3 deletions spec/models/presenter/auction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,94 @@
end
end

describe '#user_is_winning_bidder?' do
let(:ar_auction) { FactoryGirl.create(:auction, :with_bidders) }

context 'when the user is currently the winner' do
let(:bidder) { auction.bids.first.bidder }

it 'should return true' do
expect(auction.user_is_winning_bidder?(bidder)).to be_truthy
end
end

context 'when the user is not the current winner' do
let(:bidder) { auction.bids.last.bidder }

it 'should return false' do
expect(auction.user_is_winning_bidder?(bidder)).to be_falsey
end
end
end

describe '#user_is_bidder?' do
let(:ar_auction) { FactoryGirl.create(:auction, :with_bidders) }

context 'when the user has placed a bid on the project' do
let(:bidder) { auction.bids.last.bidder }

it 'should return true' do
expect(auction.user_is_bidder?(bidder)).to be_truthy
end
end

context 'when the user has not placed a bid on the project' do
let(:bidder) { FactoryGirl.create(:user) }

it 'should return false' do
expect(auction.user_is_bidder?(bidder)).to be_falsey
end
end
end

describe '#available?' do
context 'when the auction has expired' do
let(:ar_auction) { FactoryGirl.create(:auction, :closed) }

it 'should be false' do
expect(auction.available?).to eq(false)
expect(auction).to_not be_available
end
end

context 'when the auction has not started yet' do
let(:ar_auction) { FactoryGirl.create(:auction, :future) }

it 'should be false' do
expect(auction.available?).to eq(false)
expect(auction).to_not be_available
end
end

context 'when between dates' do
let(:ar_auction) { FactoryGirl.create(:auction) }

it 'should be false' do
expect(auction.available?).to eq(true)
expect(auction).to be_available
end
end
end

describe '#over?' do
context 'when the auction has expired' do
let(:ar_auction) { FactoryGirl.create(:auction, :closed) }

it 'should be true' do
expect(auction).to be_over
end
end

context 'when the auction is still running' do
let(:ar_auction) { FactoryGirl.create(:auction) }

it 'should not be true' do
expect(auction).to_not be_over
end
end

context 'when the auction has not started' do
let(:ar_auction) { FactoryGirl.create(:auction, :future) }

it 'should be false' do
expect(auction).to_not be_over
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion spec/support/feature_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ def create_bidless_auction(end_datetime: Time.now + 3.days)
return @auction, @bidders
end

def create_closed_bidless_auction
create_bidless_auction
@auction.end_datetime = Time.now - 1.day
@auction.save
end

def create_current_auction
@auction = FactoryGirl.create(:auction, :with_bidders)
@bidders = @auction.bids
Expand Down Expand Up @@ -37,7 +43,7 @@ def sign_in_bidder
end

def create_authed_bidder
@bidder = FactoryGirl.create(:user, github_id: current_user_uid)
@bidder = FactoryGirl.create(:user, github_id: current_user_uid, name: 'Doris Doogooder')
end

def show_page
Expand Down

0 comments on commit 58a762f

Please sign in to comment.