I’ve got several methods I call repeatedly across a couple ActionMailer instances, and I’d like to move them into a module so that I can include them as a group and DRY things up.
module SendgridSettings
open_tracking true
add_filter_setting("subscriptiontrack", "enable", 1)
add_filter_setting("subscriptiontrack","replace","{{ unsubscribe_url }}")
uniq_args({'id' => email.id, 'organization_id' => organization.id})
substitute '{{ person.first_name }}', recipients
set_credentials(organization)
end
Here’s the relevant mailer code:
class NewChargeMailer < ActionMailer::Base
def charge_email(recipient, transaction, organization)
include SendgridSettings
I’m getting the error undefined methodopen_tracking’ for SendgridSettings:Module`, because it seems to be applying the methods on the module, not the object that is including it.
Most of the examples I see online are about defining methods in the module, then using them in the inheriting object. I guess I’m trying to do the opposite, and haven’t been able to find a way to do that. Is such a thing possible, or even a good idea? I’m not wedded to using a module, it just seemed like the natural way to isolate the methods that need to be called sometimes as a group.
You may need to do something like this:
It is also possible that this code could work inside the module too:
You can also use ActionMailer like a controller too:
Sources:
Rails 3: Trying to extend Action Mailer with a module
https://github.com/weppos/actionmailer_with_request/blob/master/lib/actionmailer_with_request.rb
http://railsdispatch.com/posts/actionmailer