Sha256: 27488b0f1c153ff687112fd56a5357da08ef53d10cd46659e1401b617fb7038a

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

# HasOneEmailReview
# Allows any model to easily review an email template and make changes to the body

module EffectiveEmailTemplatesMailer
  extend ActiveSupport::Concern

  def mail(headers = {}, &block)
    email_template = Effective::EmailTemplate.where(template_name: action_name).first!
    assigns = (@assigns || {})

    # Parse assigns. Special keys for body and subject.
    body = assigns.delete(:body) || headers[:body] || headers['body']
    email_template.body = body if body.present?

    subject = assigns.delete(:subject) || headers[:subject] || headers['subject']
    email_template.subject = subject if subject.present?

    # Add any _url helpers
    assigns = route_url_assigns(email_template).merge(assigns)

    # Render from the template, possibly with updated body
    rendered = email_template.render(assigns)

    super(rendered.merge(headers.except(:body, :subject, 'body', 'subject')))
  end

  private

  def route_url_assigns(email_template)
    email_template.template_variables.select { |name| name.ends_with?('_url') }.inject({}) do |h, name|
      h[name] = public_send(name) if respond_to?(name); h
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
effective_email_templates-1.1.2 app/mailers/concerns/effective_email_templates_mailer.rb
effective_email_templates-1.1.1 app/mailers/concerns/effective_email_templates_mailer.rb