Sha256: 5c0ef3a45b9da01d19a48d35e2615833367d92cc80dd6291843c3d591291714c

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module EffectiveActsAsEmailFormHelper

  def email_form_fields(form, action = nil, skip: true, to: nil, variables: nil, partial: nil)
    raise('expected a form') unless form.respond_to?(:object)

    resource = form.object

    # Intended for acts_as_email_form but sometimes we use a duck typed object to save these fields as well
    unless resource.class.respond_to?(:acts_as_email_form?) || resource.respond_to?("email_form_action=")
      raise('expected an acts_as_email_form resource or one that responds to email_form_action') 
    end

    # Load the template.
    email_template = if action.present? && defined?(EffectiveEmailTemplates)
      action.kind_of?(Effective::EmailTemplate) ? action : Effective::EmailTemplate.where(template_name: action).first!
    end

    # These defaults are only used when there is no email_template
    email_defaults = form.object.email_form_defaults(action) unless email_template.present?

    from = email_template&.from || email_defaults[:from] || EffectiveResources.mailer_froms.first
    subject = email_template&.subject || email_defaults[:subject] || ''
    body = email_template&.body || email_defaults[:body] || ''
    content_type = email_template&.content_type || email_defaults[:content_type] || ''

    locals = {
      form: form,
      email_to: to,
      email_from: from,
      email_subject: subject,
      email_body: body,
      email_content_type: content_type,
      email_skip: skip,
      email_action: (action || true),
      email_template: email_template,
      email_variables: variables
    }

    render(partial: (partial || 'effective/acts_as_email_form/fields'), locals: locals)
  end

  def mailer_froms_collection(froms: nil)
    froms ||= EffectiveResources.mailer_froms

    froms.map do |from|
      html = content_tag(:span, escape_once(from))
      [from, from, 'data-html': html]
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
effective_resources-2.23.0 app/helpers/effective_acts_as_email_form_helper.rb