This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created CreateAuction class to wrap BuildAuction and SaveAuction
CreateAuction makes it easier to implement and test calls to ChangeState. In CreateAuction we will make explicit an previously implicit state: 'unpublished'. unpublished is the default published state for auctions thanks to a default call in a migration. But we will also ensure that any newly-created auctions also get a State object created reflecting this state.
- Loading branch information
Showing
4 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class CreateAuction | ||
def initialize(params, current_user) | ||
@params = params | ||
@current_user = current_user | ||
end | ||
|
||
def perform | ||
build_auction | ||
save_auction | ||
|
||
auction | ||
end | ||
|
||
private | ||
|
||
attr_reader :params, :current_user, :auction | ||
|
||
def build_auction | ||
@auction ||= BuildAuction.new(params, current_user).perform | ||
end | ||
|
||
def save_auction | ||
SaveAuction.new(auction).perform | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require 'rails_helper' | ||
|
||
describe CreateAuction do | ||
describe '#perform' do | ||
it 'returns an auction' do | ||
params = { | ||
auction: { | ||
title: 'hello' | ||
} | ||
} | ||
current_user = create(:user) | ||
create_auction = CreateAuction.new(params, current_user) | ||
auction = create_auction.perform | ||
|
||
expect(auction).to be_a Auction | ||
end | ||
|
||
context 'when the auction is valid' do | ||
it 'saves the auction' do | ||
auction_params = { | ||
"title"=>"This is the form-edited title", | ||
"started_at"=>"2017-01-30", | ||
"started_at(1i)"=>"11", | ||
"started_at(2i)"=>"30", | ||
"started_at(3i)"=>"AM", | ||
"ended_at"=>"2017-01-30", | ||
"ended_at(1i)"=>"4", | ||
"ended_at(2i)"=>"45", | ||
"ended_at(3i)"=>"PM", | ||
"due_in_days"=>"6", | ||
"delivery_due_at"=>"2017-02-7", | ||
"start_price"=>"3500", | ||
"type"=>"sealed_bid", | ||
"summary"=>"The Summary!", | ||
"description"=>"and the admin related stuff", | ||
"skill_ids"=>[""], | ||
"github_repo"=>"https://github.com/18F/calc", | ||
"issue_url"=>"https://github.com/18F/calc/issues/255", | ||
"purchase_card"=>"default", | ||
"c2_status"=>"not_requested", | ||
"customer_id"=>"", | ||
"billable_to"=>"Client Account 1 (Billable)", | ||
"notes"=>"" | ||
} | ||
|
||
params = HashWithIndifferentAccess.new({ | ||
auction: auction_params, | ||
commit: "Create", | ||
controller: "admin/auctions", | ||
action: "create" | ||
}) | ||
|
||
current_user = create(:user) | ||
|
||
create_auction = CreateAuction.new(params, current_user) | ||
|
||
expect { | ||
create_auction.perform | ||
}.to change { Auction.count }.by(1) | ||
end | ||
end | ||
|
||
context 'when the auction is not valid' do | ||
it 'does not save the auction' do | ||
params = { | ||
auction: {title: nil} | ||
} | ||
current_user = create(:user) | ||
|
||
create_auction = CreateAuction.new(params, current_user) | ||
|
||
expect { | ||
create_auction.perform | ||
}.to change { Auction.count }.by(0) | ||
end | ||
end | ||
end | ||
end |