Sha256: 667d61b5b2e5db850099584b96327073e50df08384a0b83ddb510b77a726524b

Contents?: true

Size: 1.49 KB

Versions: 67

Compression:

Stored size: 1.49 KB

Contents

# Includes some shared mailer methods for effective_* gem mailers

module EffectiveMailer
  extend ActiveSupport::Concern

  included do
    default from: -> { mailer_settings.mailer_sender }
    layout -> { mailer_settings.mailer_layout }
  end

  def subject_for(action, subject, resource, opts = {})
    mailer_subject = mailer_settings.mailer_subject

    if mailer_subject.respond_to?(:call)
      subject = self.instance_exec(action, subject, resource, opts, &mailer_subject)
    end

    subject
  end

  def mailer_admin
    mailer_settings.mailer_admin
  end

  def headers_for(resource, opts = {})
    resource.respond_to?(:log_changes_datatable) ? opts.merge(log: resource) : opts
  end

  private

  # This returns the top level gem, EffectiveOrders or EffectiveResources
  def mailer_settings
    name = self.class.name.sub('::', '').sub('Mailer', '')

    # If this is in a gem mailer like Effective::OrdersMailer we use constantize
    # Otherwise this could be included in an ApplicationMailer, so we defer to EffectiveResources
    klass = if name.start_with?('Effective')
      name.constantize
    else
      name.safe_constantize || EffectiveResources
    end

    raise('expected mailer settings to respond to mailer_subject') unless klass.respond_to?(:mailer_subject)
    raise('expected mailer settings to respond to mailer_sender') unless klass.respond_to?(:mailer_sender)
    raise('expected mailer settings to respond to mailer_layout') unless klass.respond_to?(:mailer_layout)

    klass
  end

end

Version data entries

67 entries across 67 versions & 1 rubygems

Version Path
effective_resources-2.27.3 app/mailers/concerns/effective_mailer.rb
effective_resources-2.27.2 app/mailers/concerns/effective_mailer.rb
effective_resources-2.27.1 app/mailers/concerns/effective_mailer.rb
effective_resources-2.27.0 app/mailers/concerns/effective_mailer.rb
effective_resources-2.26.5 app/mailers/concerns/effective_mailer.rb
effective_resources-2.26.4 app/mailers/concerns/effective_mailer.rb
effective_resources-2.26.3 app/mailers/concerns/effective_mailer.rb
effective_resources-2.26.2 app/mailers/concerns/effective_mailer.rb
effective_resources-2.26.1 app/mailers/concerns/effective_mailer.rb
effective_resources-2.26.0 app/mailers/concerns/effective_mailer.rb
effective_resources-2.25.16 app/mailers/concerns/effective_mailer.rb
effective_resources-2.25.15 app/mailers/concerns/effective_mailer.rb
effective_resources-2.25.14 app/mailers/concerns/effective_mailer.rb
effective_resources-2.25.13 app/mailers/concerns/effective_mailer.rb
effective_resources-2.25.12 app/mailers/concerns/effective_mailer.rb
effective_resources-2.25.11 app/mailers/concerns/effective_mailer.rb
effective_resources-2.25.10 app/mailers/concerns/effective_mailer.rb
effective_resources-2.25.9 app/mailers/concerns/effective_mailer.rb
effective_resources-2.25.8 app/mailers/concerns/effective_mailer.rb
effective_resources-2.25.7 app/mailers/concerns/effective_mailer.rb