Created
June 18, 2012 20:07
-
-
Save croaky/2950425 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 BidAcceptedJob < Struct.new(:bid_id) | |
PRIORITY = 1 | |
def self.enqueue(bid) | |
Delayed::Job.enqueue new(bid.id), priority: PRIORITY | |
end | |
def perform | |
Mailer.bid_accepted(aide, bid).deliver | |
Activity.create activity_type: 'bid_accepted', subject: bid, user: aide | |
Activity.create activity_type: 'bid_accepted', 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 hidden or 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
context 'self.enqueue' do | |
before do | |
@bid = build_stubbed(:bid) | |
BidAcceptedJob.enqueue @bid | |
end | |
it do | |
should enqueue_delayed_job('BidAcceptedJob'). | |
with_attributes(bid_id: @bid.id). | |
priority(1) | |
end | |
end |
This file contains hidden or 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 | |
<<-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