Skip to content

Instantly share code, notes, and snippets.

@welldan97
Last active December 17, 2015 12:49
Show Gist options
  • Save welldan97/5612051 to your computer and use it in GitHub Desktop.
Save welldan97/5612051 to your computer and use it in GitHub Desktop.
class VehicleEmail
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :recipients, :subject, :message, :vehicle_id
validates_presence_of :subject, :message, :vehicle_id, :recipients
def deliver_from(admin_user)
if valid?
recipients.each do |recipient|
first_name = recipient[:first_name]
last_name = recipient[:last_name]
email = recipient[:email]
UserMailer.delay.vehicle_email(:first_name => first_name,
:last_name => last_name,
:to => "#{first_name} #{last_name} <#{email}>",
:from => "#{admin_user.first_name} #{admin_user.last_name} <#{admin_user.email}>",
:sender => admin_user.email,
:subject => subject,
:message => message,
:vehicle_id => vehicle_id)
end
else
false
end
end
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
require 'spec_helper'
describe VehicleEmail do
before :each do
Delayed::Worker.delay_jobs = false
end
it 'is initialized with attributes hash' do
vehicle_email = VehicleEmail.new(:subject => 'Take a look at this vehicle', :message => 'Hello', :vehicle_id => 777)
vehicle_email.subject.should == 'Take a look at this vehicle'
vehicle_email.message.should == 'Hello'
vehicle_email.vehicle_id.should == 777
end
it 'delivers an email for each recipient' do
admin_user = AdminUser.make
UserMailer.stub_chain(:vehicle_email, :deliver)
UserMailer.should_receive(:vehicle_email).twice
vehicle_email = VehicleEmail.new(:subject => 'Take a look at this vehicle',
:message => 'Hello',
:vehicle_id => 777,
:recipients => [
{
:first_name => 'John',
:last_name => 'Locke',
:email => '[email protected]'
},
{
:first_name => 'Bill',
:last_name => 'Murray',
:email => '[email protected]'
}
]
)
vehicle_email.deliver_from admin_user
end
it "doesn't deliver an email with invalid options" do
admin_user = AdminUser.make
vehicle_email = VehicleEmail.new(:subject => 'Take a look at this vehicle',
:message => 'Hello',
:vehicle_id => 777
)
vehicle_email.should_not be_valid
vehicle_email.deliver_from(admin_user).should be_false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment