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

Commit

Permalink
Merge 9e5b168 into b30d155
Browse files Browse the repository at this point in the history
  • Loading branch information
adelevie committed Jun 29, 2016
2 parents b30d155 + 9e5b168 commit 933f640
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 19 deletions.
5 changes: 3 additions & 2 deletions app/controllers/admin/auctions_controller.rb
Expand Up @@ -20,9 +20,10 @@ def new
end

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

if auction.save
if saved
flash[:success] = I18n.t('controllers.admin.auctions.create.success')
redirect_to admin_auctions_path
else
Expand Down
14 changes: 14 additions & 0 deletions app/models/auction_ended.rb
@@ -0,0 +1,14 @@
class AuctionEnded
def initialize(auction)
@auction = auction
end

def perform
WinningBidderEmailSender.new(auction).perform
LosingBidderEmailSender.new(auction).perform
end

private

attr_reader :auction
end
@@ -1,4 +1,4 @@
class CreateAuction
class BuildAuction
attr_reader :auction

def initialize(params, user)
Expand Down
26 changes: 26 additions & 0 deletions app/services/save_auction.rb
@@ -0,0 +1,26 @@
class SaveAuction
def initialize(auction)
@auction = auction
end

def perform
saved = auction.save

if should_schedule_auction_ended_job?(saved)
AuctionEnded
.new(auction)
.delay(run_at: auction.ended_at)
.perform
end

saved
end

private

attr_reader :auction

def should_schedule_auction_ended_job?(saved)
saved && !auction.ended_at.nil?
end
end
14 changes: 0 additions & 14 deletions config/clock.rb
Expand Up @@ -3,20 +3,6 @@
require_relative "environment"

module Clockwork
every(1.day, "losing_bidder_emails.send", at: "17:05", tz: 'Eastern Time (US & Canada)') do
puts "Sending losing bidder emails"
AuctionQuery.new.closed_within_last_24_hours do |auction|
LosingBidderEmailSender.new(auction).delay.perform
end
end

every(1.day, "winning_bidder_emails.send", at: "17:05", tz: 'Eastern Time (US & Canada)') do
puts "Sending winning bidder emails"
AuctionQuery.new.closed_within_last_24_hours do |auction|
WinningBidderEmailSender.new(auction).delay.perform
end
end

every(1.day, "tock_projects.import", at: "02:00", tz: 'Eastern Time (US & Canada)') do
puts "Importing Tock projects"
TockImporter.new.delay.perform
Expand Down
4 changes: 2 additions & 2 deletions docs/technical_debt.md
Expand Up @@ -238,11 +238,11 @@ administrator creates a new auction, the relevant code in the
controller looks like this:

``` ruby
@auction = CreateAuction.new(params, current_user).perform
@auction = BuildAuction.new(params, current_user).perform

```

1. [CreateAuction](../app/services/create_auction) is another Service
1. [BuildAuction](../app/services/build_auction.rb) is another Service
object that represents the action of creating an auction and sticks
to the familiar pattern with a `perform` method.
2. Within this class, the
Expand Down
1 change: 1 addition & 0 deletions features/admin_creates_auction.feature
Expand Up @@ -2,6 +2,7 @@ Feature: Admin creates auctions in the admins panel
As an admin
I should be able to create auctions

@background_jobs_off
Scenario: Adding an auction
Given I am an administrator
And there is a client account to bill to
Expand Down
1 change: 1 addition & 0 deletions features/contracting_officer_creates_auction.feature
Expand Up @@ -2,6 +2,7 @@ Feature: Contracting Officers
As an admin and contracting officer
I should be able to create auctions over $3500

@background_jobs_off
Scenario: contracting office creates auction over $3500
Given I am a contracting officer
And there is a client account to bill to
Expand Down
37 changes: 37 additions & 0 deletions spec/models/auction_ended_spec.rb
@@ -0,0 +1,37 @@
require 'rails_helper'

describe AuctionEnded do
describe '#perform' do
context 'an auction ends' do
it 'sends the winning bidder email' do
auction = create(:auction)

winning_bidder_email_sender_double = instance_double("WinningBidderEmailSender")
allow(winning_bidder_email_sender_double).to receive(:perform)
allow(WinningBidderEmailSender).to receive(:new)
.with(any_args)
.and_return(winning_bidder_email_sender_double)

expect(winning_bidder_email_sender_double).to receive(:perform)

AuctionEnded.new(auction).perform
end

it 'sends the losing bidders email' do
auction = create(:auction)

auction = create(:auction, :closed, :with_bidders)

losing_bidder_email_sender_double = instance_double("LosingBidderEmailSender")
allow(losing_bidder_email_sender_double).to receive(:perform)
allow(LosingBidderEmailSender).to receive(:new)
.with(any_args)
.and_return(losing_bidder_email_sender_double)

expect(losing_bidder_email_sender_double).to receive(:perform)

AuctionEnded.new(auction).perform
end
end
end
end
16 changes: 16 additions & 0 deletions spec/services/build_auction_spec.rb
@@ -0,0 +1,16 @@
require 'rails_helper'

describe BuildAuction do
describe '#perform' do
context 'building with all valid params' do
it 'builds the auction object' do
user = create(:user)
params = { auction: FactoryGirl.attributes_for(:auction) }

auction = BuildAuction.new(params, user).perform

expect(auction).to be_an(Auction)
end
end
end
end
44 changes: 44 additions & 0 deletions spec/services/save_auction_spec.rb
@@ -0,0 +1,44 @@
require 'rails_helper'

describe SaveAuction do
describe '#perform' do
context 'given a valid auction' do
it 'saves the auction' do
auction = build(:auction)
SaveAuction.new(auction).perform

expect(auction).to be_persisted
end

it 'returns true' do
auction = build(:auction)
saved = SaveAuction.new(auction).perform

expect(saved).to eq true
end
end

context 'given an invalid auction' do
it 'does not save the auction' do
auction = build(:auction, title: nil)

expect { SaveAuction.new(auction).perform }
.not_to change { Auction.count }
end

it 'is not persisted' do
auction = build(:auction, title: nil)
SaveAuction.new(auction).perform

expect(auction).to_not be_persisted
end

it 'returns false' do
auction = build(:auction, title: nil)
saved = SaveAuction.new(auction).perform

expect(saved).to be(false)
end
end
end
end

0 comments on commit 933f640

Please sign in to comment.