I have a fairly simple Rails application that allows users to manage their clients and create a case for each client. When a case is created the details are sent to two email addresses (different content in each email) and using a gem, sends the information to FreeagentCentral.
I have implemented delayed_job which is working well for the Freeagent API call, but I think there is probably a better, lower overhead, method of sending the emails. It seems to take quite a while.
I currently have the following code in my Rails 2.3 application.
kases_controller.rb
# POST /kases
# POST /kases.xml
def create
@company = Company.find(params[:kase][:company_id])
@kase = @company.kases.new(params[:kase])
if @kase.save
UserMailer.deliver_makeakase("dropbox@1223111.domain.highrisehq.com", "Highrise", @kase) if params[:sendtohighrise]
UserMailer.deliver_makeakaseteam("surveymanager@domain.co.uk", "Highrise", @kase) if params[:notify_team_of_creation]
@kase.delay.create_freeagent_project(current_user) if params[:send_to_freeagent]
redirect_to(@kase)
#flash[:notice] = 'Case was successfully created.'
flash[:notice] = fading_flash_message("Case was successfully created.", 5)
else
render :new
end
end
user_mailer.rb
def makeakase(email, name, kase, bccemails = [])
recipients email
from "info@domain.co.uk"
subject "FW: Case creation from Survey Manager"
bcc bccemails
sent_on Time.now
body :name => name, :kase => kase
end
def makeakaseteam(email, name, kase = [])
recipients email
from "info@domain.co.uk"
subject "A new case has been created."
sent_on Time.now
body :name => name, :kase => kase
content_type "text/html"
end
I am looking for any advice on slimming down the number of emails sent, or a way of adding the emails to the delayed jobs.
I use PostmarkApp for sending the emails, which is another gem. Details of which can be found here: postmark-gem
Not sure if this is what you want, but i would make a method that would do the sending of the emails and the creation of the freeagent, and then let it be handled by delayed_job.
Something like
and in your controller you would then write
or just handing down the
paramsis maybe simpler/cleaner.