Skip to content

Instantly share code, notes, and snippets.

@adriand
Created July 26, 2012 17:04
Show Gist options
  • Save adriand/3183235 to your computer and use it in GitHub Desktop.
Save adriand/3183235 to your computer and use it in GitHub Desktop.
# class
class DispatchQueueCreator
class << self
def send_to_all_subscribers!(dispatch)
Subscriber.all.each do |subscriber|
qd = QueuedDispatch.create(:subscriber => subscriber, :dispatch => dispatch)
qd.send!
end
self.update_attributes(:sent_at => Time.now)
end
handle_asynchronously :send_to_all_subscribers!
def send_to_groups!(dispatch, group_ids)
end
handle_asynchronously :send_to_groups!
def send_to!(dispatch, object_conforming_to_email_recipient_protocol)
end
handle_asynchronously :send_to!
end
end
# spec
# require 'spec_helper'
# require 'delayed_job_spec_helper'
require_relative '../../app/services/dispatch_queue_creator'
describe DispatchQueueCreator do
before do
Subscriber = Struct.new(:email, :name, :id)
Dispatch = mock.as_null_object
Dispatch.stub(:id).and_return(1)
QueuedDispatch = mock.as_null_object
DispatchQueueCreator.stub(:handle_asynchronously)
end
context "sending to subscribers" do
before do
@subscribers = []
5.times do |i|
@subscribers << Subscriber.new("test#{i}@example.com", "Test Van #{i}", i)
end
Subscriber.stub(:all).and_return(@subscribers)
end
it "should create a series of queued dispatches, one for each subscriber" do
dispatch = Dispatch.new
# what am I actually testing here right now? There are two main things I want to ensure
# are happening:
# 1. A series of QueuedDispatches are being created that are associated with my dispatch
# plus one each of my subscribers.
# 2. Delayed job is setting up the same number of jobs as exist for subscribers.
# set up assertion that we expect there to be 5 queued dispatches now
# qd_count = QueuedDispatch.count
QueuedDispatch.should_receive(:create).exactly(@subscribers.size).times
DispatchQueueCreator.send_to_all_subscribers!(dispatch)
end
end
context "sending to groups" do
end
context "sending to any object that provides me with a list of recipients" do
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment