I am using the mandrill-api (from MailChimp) to send an email to many people in a single API call. It takes a HTML parameter that I would like to be generated from a view rather than directly in my model where I have no access to simple_format to apply to the plain text (self.body) I have saved in the database.
Example code:
class Request < ActiveRecord::Base
after_create :attempt_send
def attempt_send
require 'mandrill'
m = Mandrill::API.new
message = {
:subject=> "Subject",
:html => "#{self.body}"
}
sending = m.messages.send(message)
puts sending
end
end
This scenario is causing me an MVC headache. The rest of my app uses Rails action mailer so email content is handled cleanly in the view layer.
If you could tell me how to use simple_format somehow that would be a quick fix to achieve the formatting I need on self.body. I'm open to more elegant solutions to make this function more like Rails mailer though. One option I've considered is using a controller method to return the email html but then I think that would result in a self-referencing http request which wouldn't be an efficient way to go about it. Open to ideas!
You can include the module ActionView::Helpers::TextHelper
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
class Request < ActiveRecord::Base
include ActionView::Helpers::TextHelper
after_create :attempt_send
# ...
:html => "#{simple_format(self.body})"
--
Another option which doesn't include all of the TextHelper methods, just the one(s) you need is to use delegate.
delegate :simple_format, to: 'ActionController::Base.helpers'