From 2d7ed6a4664deeaa3dc56518f3768cd97d3aa6c7 Mon Sep 17 00:00:00 2001 From: Alan deLevie Date: Fri, 27 Jan 2017 15:52:10 -0500 Subject: [PATCH] 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. --- app/controllers/admin/auctions_controller.rb | 4 +- app/services/create_auction.rb | 25 +++++++ docs/moving-states-to-tables.md | 2 + spec/services/create_auction_spec.rb | 78 ++++++++++++++++++++ 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 app/services/create_auction.rb create mode 100644 spec/services/create_auction_spec.rb diff --git a/app/controllers/admin/auctions_controller.rb b/app/controllers/admin/auctions_controller.rb index 4e0b73cd..0174b49b 100644 --- a/app/controllers/admin/auctions_controller.rb +++ b/app/controllers/admin/auctions_controller.rb @@ -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 diff --git a/app/services/create_auction.rb b/app/services/create_auction.rb new file mode 100644 index 00000000..8e1c184d --- /dev/null +++ b/app/services/create_auction.rb @@ -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 diff --git a/docs/moving-states-to-tables.md b/docs/moving-states-to-tables.md index fd122f4e..f2d86459 100644 --- a/docs/moving-states-to-tables.md +++ b/docs/moving-states-to-tables.md @@ -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 diff --git a/spec/services/create_auction_spec.rb b/spec/services/create_auction_spec.rb new file mode 100644 index 00000000..7e9cf9dc --- /dev/null +++ b/spec/services/create_auction_spec.rb @@ -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