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

Commit

Permalink
Merge 6fc77d7 into 533db70
Browse files Browse the repository at this point in the history
  • Loading branch information
jessieay committed May 9, 2016
2 parents 533db70 + 6fc77d7 commit 80faf8e
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 24 deletions.
9 changes: 9 additions & 0 deletions app/models/null_bid.rb
@@ -0,0 +1,9 @@
class NullBid
def amount
nil
end

def bidder_id
nil
end
end
13 changes: 13 additions & 0 deletions app/models/rules/base_rules.rb
@@ -0,0 +1,13 @@
class Rules::BaseRules < Struct.new(:auction)
def partial_path(name, base_dir = 'auctions')
"#{base_dir}/#{partial_prefix}/#{name}.html.erb"
end

def user_can_bid?(user)
auction.available? && user.present? && user.sam_accepted?
end

def partial_prefix
fail NotImplementedError
end
end
20 changes: 1 addition & 19 deletions app/models/rules/basic.rb
@@ -1,4 +1,4 @@
class Rules::Basic < Struct.new(:auction)
class Rules::Basic < Rules::BaseRules
def winning_bid
auction.lowest_bid
end
Expand All @@ -7,12 +7,6 @@ def veiled_bids(user)
auction.bids
end

def user_can_bid?(user)
return false unless auction.available?
return false if user.nil? || !user.sam_accepted?
true
end

def max_allowed_bid
if auction.lowest_bid.is_a?(NullBidPresenter)
return auction.start_price - PlaceBid::BID_INCREMENT
Expand All @@ -29,14 +23,6 @@ def show_bids?
true
end

def partial_path(name, base_dir = 'auctions')
if partial_prefix.blank?
"#{base_dir}/#{name}.html.erb"
else
"#{base_dir}/#{partial_prefix}/#{name}.html.erb"
end
end

def partial_prefix
'multi_bid'
end
Expand All @@ -45,10 +31,6 @@ def formatted_type
'multi-bid'
end

def rules_type
'basic'
end

def highlighted_bid_label
'Current bid:'
end
Expand Down
7 changes: 3 additions & 4 deletions app/models/rules/sealed_bid.rb
@@ -1,4 +1,4 @@
class Rules::SealedBid < Rules::Basic
class Rules::SealedBid < Rules::BaseRules
def winning_bid
if auction.available?
NullBidPresenter.new
Expand All @@ -9,15 +9,14 @@ def winning_bid

def veiled_bids(user)
if auction.available?
return [] if user.nil?
auction.bids.select { |bid| bid.bidder_id == user.id }
auction.bids.select { |bid| bid.bidder == user }
else
auction.bids
end
end

def user_can_bid?(user)
super && !auction.bids.any? { |b| b.bidder_id == user.id }
super && !auction.bids.any? { |b| b.bidder == user }
end

def max_allowed_bid
Expand Down
21 changes: 20 additions & 1 deletion app/serializers/auction_serializer.rb
Expand Up @@ -11,7 +11,8 @@ class AuctionSerializer < ActiveModel::Serializer
:start_price,
:summary,
:title,
:updated_at
:updated_at,
:winning_bid
)

def bids
Expand All @@ -34,4 +35,22 @@ def end_datetime
def start_datetime
object.start_datetime.iso8601
end

def winning_bid
WinningBidSerializer.new(find_winning_bid)
end

private

def find_winning_bid
if auction_status.available?
NullBid.new
else
RulesFactory.new(object).create.winning_bid
end
end

def auction_status
@_auction_status ||= AuctionStatus.new(object)
end
end
44 changes: 44 additions & 0 deletions spec/serializers/auction_serializer_spec.rb
@@ -0,0 +1,44 @@
require 'rails_helper'

describe AuctionSerializer do
describe '#winning_bid' do
context 'auction has no bids' do
it 'returns null attributes' do
auction = create(:auction)

serializer = AuctionSerializer.new(auction)

expect(serializer.winning_bid.to_json).to eq(
WinningBidSerializer.new(NullBid.new).to_json
)
end
end

context 'auction is available' do
it 'returns null attributes' do
auction = create(:auction)
allow(AuctionStatus).to receive(:new).with(auction).and_return(double(available?: true))

serializer = AuctionSerializer.new(auction)

expect(serializer.winning_bid.to_json).to eq(
WinningBidSerializer.new(NullBid.new).to_json
)
end

context 'auction is over' do
it 'returns winning bid' do
auction = create(:auction)
bid = create(:bid, auction: auction)
allow(AuctionStatus).to receive(:new).with(auction).and_return(double(available?: false))

serializer = AuctionSerializer.new(auction)

expect(serializer.winning_bid.to_json).to eq(
WinningBidSerializer.new(bid).to_json
)
end
end
end
end
end
11 changes: 11 additions & 0 deletions spec/support/api/schemas/auction.json
Expand Up @@ -48,6 +48,17 @@
"type": "string",
"minLength": 1
},
"winning_bid": {
"type": "object",
"properties": {
"amount": {
"type": ["number", "null"]
},
"bidder_id": {
"type": ["number", "null"]
}
}
},
"bids": {
"type": "array",
"uniqueItems": true,
Expand Down
11 changes: 11 additions & 0 deletions spec/support/api/schemas/auctions.json
Expand Up @@ -64,6 +64,17 @@
"type": "string",
"minLength": 1
},
"winning_bid": {
"type": "object",
"properties": {
"amount": {
"type": ["number", "null"]
},
"bidder_id": {
"type": ["number", "null"]
}
}
},
"bids": {
"type": "array",
"uniqueItems": true,
Expand Down

0 comments on commit 80faf8e

Please sign in to comment.