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

Commit

Permalink
Merge 59e2028 into d1df546
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Harris committed Apr 14, 2016
2 parents d1df546 + 59e2028 commit d56b930
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 127 deletions.
6 changes: 3 additions & 3 deletions app/models/place_bid.rb
Expand Up @@ -43,8 +43,8 @@ def auction_available?
Presenter::Auction.new(auction).available?
end

def current_max_bid
Presenter::Auction.new(auction).current_max_bid
def max_allowed_bid
Presenter::Auction.new(auction).max_allowed_bid
end

def auction_is_single_bid?
Expand Down Expand Up @@ -83,7 +83,7 @@ def validate_bid_data
fail UnauthorizedError, 'Bid amount out of range'
end

if auction_is_multi_bid? && amount > current_max_bid
if auction_is_multi_bid? && amount > max_allowed_bid
fail UnauthorizedError, "Bids cannot be greater than the current max bid"
end
end
Expand Down
59 changes: 16 additions & 43 deletions app/models/presenter/auction.rb
Expand Up @@ -12,20 +12,11 @@ def initialize(auction)
@auction = auction
end

def current_bid?
current_bid_record != nil
end

def current_bid
return Presenter::Bid::Null.new unless current_bid_record
Presenter::Bid.new(current_bid_record)
end

def current_max_bid
if current_bid.is_a?(Presenter::Bid::Null)
def max_allowed_bid
if lowest_bid.is_a?(Presenter::Bid::Null)
return start_price - PlaceBid::BID_INCREMENT
else
return current_bid.amount - PlaceBid::BID_INCREMENT
return lowest_bid.amount - PlaceBid::BID_INCREMENT
end
end

Expand All @@ -37,24 +28,20 @@ def current_max_bid
to: :model

delegate :amount, :time,
to: :current_bid, prefix: :current_bid
to: :lowest_bid, prefix: :lowest_bid

delegate :bidder_name, :bidder_duns_number,
to: :current_bid, prefix: :current

def current_bid_amount_as_currency
number_to_currency(current_bid_amount)
end
to: :lowest_bid, prefix: :lowest

def bids?
bid_count > 0
end

def bids
model.bids.to_a
.map {|bid| Presenter::Bid.new(bid) }
.sort_by(&:created_at)
.reverse
@bids ||= model.bids.to_a
.map {|bid| Presenter::Bid.new(bid) }
.sort_by(&:created_at)
.reverse
end

def veiled_bids(user)
Expand Down Expand Up @@ -126,21 +113,17 @@ def expiring?
available? && model.end_datetime < 12.hours.from_now
end

def winning_bidder
winning_bid.bidder rescue nil
end

def winning_bid
return single_bid_winning_bid if single_bid?
return multi_bid_winning_bid if multi_bid?
return Presenter::Bid::Null.new if single_bid? && available?
lowest_bid
end

def winning_bidder_id
winning_bid.bidder_id rescue nil
winning_bid.bidder_id
end

def winning_bid_id
winning_bid.id rescue nil
winning_bid.id
end

def single_bid?
Expand All @@ -151,16 +134,10 @@ def multi_bid?
model.type == 'multi_bid'
end

def single_bid_winning_bid
return nil if available?
return lowest_bids.first if lowest_bids.length == 1
lowest_bids.sort_by(&:created_at).first
end

def multi_bid_winning_bid
current_bid
def lowest_bid
@lowest_bid ||= (lowest_bids.first || Presenter::Bid::Null.new)
end

def lowest_bids
bids.select {|b| b.amount == lowest_amount }.sort_by(&:created_at)
end
Expand Down Expand Up @@ -190,10 +167,6 @@ def human_start_time

private

def current_bid_record
@current_bid_record ||= bids.sort_by {|bid| [bid.amount, bid.created_at, bid.id] }.first
end

def markdown
# FIXME: Do we want the lax_spacing?
@markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML,
Expand Down
18 changes: 15 additions & 3 deletions app/models/presenter/bid.rb
Expand Up @@ -22,6 +22,10 @@ def veiled_bidder(show_user = nil, message: nil)
end
end

def bidder_id
bidder.id || null
end

def bidder_duns_number
bidder.duns_number || null
end
Expand All @@ -44,7 +48,7 @@ def amount_to_currency

def amount_to_currency_with_asterisk
return "#{amount_to_currency} *" if is_winning?
return amount_to_currency
amount_to_currency
end

def is_winning?
Expand All @@ -61,9 +65,9 @@ def winning_status

def ==(other)
return false unless other.is_a?(Presenter::Bid)
self.id == other.id
id == other.id
end

def model
__getobj__
end
Expand All @@ -83,6 +87,14 @@ def bidder_name
NULL
end

def bidder_id
nil
end

def id
nil
end

def amount
NULL
end
Expand Down
43 changes: 22 additions & 21 deletions app/models/view_model/auction.rb
Expand Up @@ -10,9 +10,9 @@ def auction
end

delegate :title, :summary, :html_description, :bid_count,
:current_bid_amount_as_currency, :issue_url,
:issue_url,
:start_datetime, :end_datetime, :veiled_bids,
:created_at, :current_bidder_name, :html_summary,
:created_at, :html_summary,
:html_description, :formatted_type, :available?, :bids?,
:bids, :human_start_time, :start_price, :over?,
:multi_bid?, :single_bid?, :type, :id,
Expand All @@ -30,34 +30,35 @@ def show_bid_button?
user_can_bid? || current_user.nil?
end

def formatted_current_bid_amount
if current_bid_amount.nil?
return 'n/a'
delegate :amount,
to: :highlighted_bid, prefix: true

# This is the single bid we display under the auction as an important
# summary of the bidding. Unlike the lowest bid, it differs based
# on the type of auction and whether the auction is closed
def highlighted_bid
if auction.available? && auction.single_bid?
auction.bids.detect {|bid| bid.bidder_id == current_user.id } || Presenter::Bid::Null.new
else
return number_to_currency(current_bid_amount)
auction.lowest_bid
end
end

def current_bid_amount
current_bid.amount rescue nil
def highlighted_bid_amount_as_currency
number_to_currency(highlighted_bid_amount)
end

# This could be in the Presenter::Auction modelm but I don't want to make that change now
def current_bid
if auction.available? && auction.single_bid?
auction.bids.detect {|bid| bid.bidder_id == current_user.id }
else
auction.current_bid
end
def highlighted_bidder_name
highlighted_bid.bidder_name
end

# can we get rid of Presenter::Auction view?

def user_is_bidder?
user_bids_obj.has_bid?
end

def user_is_winning_bidder?
return false unless auction.current_bid?
# fixme: who is calling this?
return false unless auction.bids?
current_user.id == auction.winning_bidder_id
end

Expand Down Expand Up @@ -89,11 +90,11 @@ def index_bid_summary_partial
end
end

def current_bid_info_partial
def highlighted_bid_info_partial
if auction.single_bid?
'bids/single_bid/current_bid_info'
'bids/single_bid/highlighted_bid_info'
elsif auction.multi_bid?
'bids/multi_bid/current_bid_info'
'bids/multi_bid/highlighted_bid_info'
end
end

Expand Down
7 changes: 4 additions & 3 deletions app/models/view_model/auction_show.rb
Expand Up @@ -7,16 +7,17 @@ def auction
end

delegate :title, :summary, :html_description, :status, :id,
:bid_count, :current_bid_amount_as_currency, :issue_url,
:bid_count, :issue_url,
:user_bid_amount_as_currency, :show_bid_button?,
to: :auction, prefix: true

delegate :formatted_current_bid_amount, :current_bid_amount, :current_bid, :auction_type,
delegate :highlighted_bid_amount_as_currency,
:highlighted_bid_amount, :highlighted_bid, :auction_type,
to: :auction

def auction_status_header
if auction_won?
"Winning bid (#{auction.current_bidder_name}):"
"Winning bid (#{auction.highlighted_bidder_name}):"
else
if auction.single_bid?
"Your bid:"
Expand Down
2 changes: 1 addition & 1 deletion app/models/view_model/auction_status/open.rb
Expand Up @@ -28,7 +28,7 @@ def tag_data_value_2
if auction.single_bid?
"Sealed"
else
"#{number_to_currency(auction.current_bid_amount)} - #{auction.bids.length} bids"
"#{number_to_currency(auction.highlighted_bid_amount)} - #{auction.bids.length} bids"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/view_model/auction_status/over.rb
Expand Up @@ -24,7 +24,7 @@ def tag_data_label_2
end

def tag_data_value_2
number_to_currency(auction.current_bid_amount)
number_to_currency(auction.highlighted_bid_amount)
end
end
end
Expand Down
10 changes: 2 additions & 8 deletions app/views/admin/auctions/show.html.erb
Expand Up @@ -19,14 +19,8 @@
<p><%=h raw @auction.html_description %></p>

<div class='usa-grid'>
<% if @auction.current_bid? %>
<h2>Current Bid: </h2>
<div class='usa-width-one-whole'>
<div class='usa-width-one-fourth bidder-name'><%= @auction.current_bidder_name %></div>
<div class='usa-width-one-fourth bidder-duns-number'><%= @auction.current_bidder_duns_number %></div>
<div class='usa-width-one-fourth amount'><%= number_to_currency(@auction.current_bid_amount) %></div>
<div class='usa-width-one-fourth amount'><%= @auction.current_bid_time %></div>
</div>
<% if @auction.bids? %>
<h2>Current Bids:</h2>

<% @auction.bids.each do |bid| %>
<div class='usa-width-one-whole'>
Expand Down
2 changes: 1 addition & 1 deletion app/views/auctions/_winning_bid.html.erb
Expand Up @@ -6,7 +6,7 @@
<% if auction.bids? %>
<div class="issue-bids-info-item"><% if defined? for_winners_page %><% else %>
<div class="issue-bids-info-item">Winning bid: </div>
<% end %><%= number_to_currency(auction.current_bid_amount) %></div>
<% end %><%= auction.highlighted_bid_amount_as_currency %></div>
<% else %>
<div class="issue-bids-info-item">No bids</div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/auctions/multi_bid/_auction_status.html.erb
@@ -1,4 +1,4 @@
<span class="bid-amount"><%= @view_model.auction_current_bid_amount_as_currency %></span>
<span class="bid-amount"><%= @view_model.highlighted_bid_amount_as_currency %></span>
<span class=""><%=
link_to(
@view_model.auction_link_text,
Expand Down
2 changes: 1 addition & 1 deletion app/views/auctions/multi_bid/_index_bid_summary.html.erb
@@ -1,6 +1,6 @@
<% if auction.bids? %>
<span class="current-bid-header">Current bid:</span>
<span class="current-bid"><%= number_to_currency(auction.current_bid_amount) %> </span><br/>
<span class="current-bid"><%= auction.highlighted_bid_amount_as_currency %> </span><br/>
<span class="current-bid-link"><%=
link_to(
pluralize(auction.bids.length, 'bid'),
Expand Down
@@ -1,5 +1,5 @@
<h2 class="center">Current bid:</h2>
<p class="current-bid-amount center"><%= number_to_currency(@auction.current_bid_amount) %></p>
<p class="current-bid-amount center"><%= number_to_currency(@auction.highlighted_bid_amount) %></p>
<% if @auction.user_is_winning_bidder? %>
<h3 class="center">You are currently the winning bidder.</h3>
<% elsif @auction.bids? %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/bids/new.html.erb
@@ -1,9 +1,9 @@
<% content_for :title do %>18F Micro-purchase - New bid<% end %>
<% content_for :description do %>
<% if @auction.available? %>
Place a bid for "<%= @auction.title %>". The current bid is <%= number_to_currency(@auction.current_bid_amount) %>.
Place a bid for "<%= @auction.title %>". The current bid is <%= @auction.highlighted_bid_amount_as_currency %>.
<% else %>
Bidding on this auction closed on <%= @auction.end_datetime %>. The winning bid was <%= number_to_currency(@auction.current_bid_amount) %>.
Bidding on this auction closed on <%= @auction.end_datetime %>. The winning bid was <%= @auction.highlighted_bid_amount_as_currency %>.
<% end %>
<% end %>
<% content_for :data_tags do %>
Expand All @@ -18,7 +18,7 @@ Bidding on this auction closed on <%= @auction.end_datetime %>. The winning bid
<%= form_for @bid, url: confirm_auction_bids_path(@auction), html: {class: 'bid-form'} do |f| %>
<div class="usa-width-one-whole">
<div class="usa-width-one-half">
<%= render partial: @auction.current_bid_info_partial %>
<%= render partial: @auction.highlighted_bid_info_partial %>
</div>

<div class="usa-width-one-half">
Expand Down
6 changes: 3 additions & 3 deletions features/step_definitions/auction_steps.rb
Expand Up @@ -54,11 +54,11 @@

Then(/^I should see the winning bid for the auction$/) do
auction = Presenter::Auction.new(@auction)
current_bid_amount = ApplicationController.helpers.number_to_currency(
auction.current_bid.amount
lowest_bid_amount = ApplicationController.helpers.number_to_currency(
auction.lowest_amount
)

expect(page).to have_text(current_bid_amount)
expect(page).to have_text(lowest_bid_amount)
end

Then(/^I should see the auction's (.+)$/) do |field|
Expand Down
8 changes: 4 additions & 4 deletions features/step_definitions/closed_auctions_steps.rb
Expand Up @@ -21,15 +21,15 @@
end

Then(/^I should see the auction had a winning bid$/) do
auction = Presenter::Auction.new(@auction)
expect(page).to have_content("Winning bid: #{auction.current_bid_amount_as_currency}")
auction = ViewModel::Auction.new(nil, @auction)
expect(page).to have_content("Winning bid: #{auction.highlighted_bid_amount_as_currency}")
expect(page).not_to have_content("Current bid:")
end


Then(/^I should see the auction had a winning bid with name$/) do
auction = Presenter::Auction.new(@auction)
expect(page).to have_content("Winning bid (#{auction.current_bidder_name}): #{auction.current_bid_amount_as_currency}")
auction = ViewModel::Auction.new(nil, @auction)
expect(page).to have_content("Winning bid (#{auction.highlighted_bidder_name}): #{auction.highlighted_bid_amount_as_currency}")
expect(page).not_to have_content("Current bid:")
end

Expand Down

0 comments on commit d56b930

Please sign in to comment.