-
-
Save ralphos/cf75ac4f3f9efdea2045 to your computer and use it in GitHub Desktop.
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
class BidOfferedJob < Struct.new(:bid_id) | |
PRIORITY = 1 | |
def self.enqueue(bid) | |
Delayed::Job.enqueue new(bid.id), priority: PRIORITY | |
end | |
def perform | |
Mailer.bid_offered(owner, bid).deliver | |
Activity.create activity_type: 'bid_offered', subject: bid, user: aide | |
Activity.create activity_type: 'bid_offered', subject: bid, user: owner | |
end | |
private | |
def aide | |
bid.user | |
end | |
def bid | |
Bid.find bid_id | |
end | |
def owner | |
bid.job.owner | |
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
require 'spec_helper' | |
describe BidOfferedJob do | |
context '.enqueue' do | |
before do | |
@bid = build_stubbed(:bid) | |
BidOfferedJob.enqueue @bid | |
end | |
it do | |
should enqueue_delayed_job('BidOfferedJob'). | |
with_attributes(bid_id: @bid.id). | |
priority(1) | |
end | |
end | |
context '#perform' do | |
before do | |
@bid = build_stubbed(:bid) | |
Bid.stubs find: @bid | |
mailer = stub('mailer', :deliver) | |
Mailer.stubs bid_offered: mailer | |
Activity.stubs :create | |
BidOfferedJob.new(@bid.id).perform | |
end | |
it 'emails owner' do | |
Mailer.should have_received(:bid_offered).with(@bid.job.owner, @bid) | |
end | |
it 'creates a bid_offered activity for owner' do | |
Activity.should have_received(:create). | |
with(activity_type: 'bid_offered', user: @bid.job.owner, subject: @bid) | |
end | |
it 'creates a bid_offered activity for aide' do | |
Activity.should have_received(:create). | |
with(activity_type: 'bid_offered', user: @bid.user, subject: @bid) | |
end | |
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
module DelayedJob | |
module Matchers | |
def enqueue_delayed_job(handler) | |
DelayedJobMatcher.new handler | |
end | |
class DelayedJobMatcher | |
def initialize(handler) | |
@handler = handler | |
@attributes = {} | |
@priority = 0 | |
@failure_message = '' | |
end | |
def description | |
"enqueue a #{@handler} delayed job" | |
end | |
def failure_message | |
@failure_message || <<-message.strip_heredoc | |
Expected #{@handler} to be enqueued as a delayed job. Try: | |
Delayed::Job.enqueue #{@handler}.new | |
message | |
end | |
def matches?(subject) | |
@subject = subject | |
enqueued? && correct_attributes? && correct_priority? | |
end | |
def priority(priority) | |
@priority = priority | |
self | |
end | |
def with_attributes(attributes) | |
@attributes = attributes | |
self | |
end | |
private | |
def correct_attributes? | |
@attributes.each do |key, value| | |
payload_object.send(key).should == value | |
end | |
end | |
def correct_priority? | |
if @priority == job.priority | |
true | |
else | |
@failure_message = <<-message.strip_heredoc | |
Expected priority to be #{@priority} but was #{job.priority}" | |
message | |
false | |
end | |
end | |
def enqueued? | |
payload_object.kind_of? @handler.constantize | |
end | |
def job | |
Delayed::Job.last | |
end | |
def payload_object | |
job.payload_object | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment