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

Commit

Permalink
Created CreateAuction class to wrap BuildAuction and SaveAuction
Browse files Browse the repository at this point in the history
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
adelevie committed Jan 27, 2017
1 parent 630842c commit 2d7ed6a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/controllers/admin/auctions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def new
end

def create
auction = BuildAuction.new(params, current_user).perform
auction = CreateAuction.new(params, current_user).perform

if SaveAuction.new(auction).perform
if auction.persisted?
flash[:success] = I18n.t('controllers.admin.auctions.create.success')
redirect_to admin_auction_path(auction)
else
Expand Down
25 changes: 25 additions & 0 deletions app/services/create_auction.rb
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
2 changes: 2 additions & 0 deletions docs/moving-states-to-tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Refactoring data pattern:
* Remove old writes (in code)
* Remove old fields (in database)



---------
# try this one first:
Published: draft => published | archived
Expand Down
78 changes: 78 additions & 0 deletions spec/services/create_auction_spec.rb
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

0 comments on commit 2d7ed6a

Please sign in to comment.