module LocaleMailer::Concern

Private Instance Methods

action_i18n_options(options = {}) click to toggle source
# File lib/locale_mailer/concern.rb, line 58
def action_i18n_options options = {}
  @action_i18n_options ||= instance_variables.inject({}) do |memo, name|
    if name.to_s.match(/\A@(?!_).*/) and not name.to_s.match(/\A@action_i18n_/)
      if instance_variable_get(name).try(:as_json).is_a? Hash
        flatten_hash(instance_variable_get(name).as_json).each do |(k,v)|
          memo["#{name.to_s.gsub(/\A@/,'')}_#{k}".to_sym] = v
        end
      else
        memo[name.to_s.gsub(/\A@/,'').to_sym] = instance_variable_get(name)
      end
    end
    memo
  end.update(scope: action_i18n_path(options))
end
action_i18n_path(options = {}) click to toggle source
# File lib/locale_mailer/concern.rb, line 50
def action_i18n_path options = {}
  @action_i18n_path ||= [
    Rails.configuration.locale_mailer_path_prefix,
    (options[:template_path] || mailer_name.gsub('/','.').underscore),
    (options[:template_name] || action_name)
  ].compact.join('.')
end
body_from_locale(options = {}) click to toggle source
# File lib/locale_mailer/concern.rb, line 89
def body_from_locale  options = {}
  render inline: view_context.text('body', action_i18n_options(options)), layout: options[:layout] 
end
flatten_hash(hash) click to toggle source
# File lib/locale_mailer/concern.rb, line 73
def flatten_hash(hash)
  hash.each_with_object({}) do |(k, v), h|
    if v.is_a? Hash
      flatten_hash(v).map do |h_k, h_v|
        h["#{k}_#{h_k}".to_sym] = h_v
      end
    else
      h[k] = v
    end
  end
end
mail_with_localized_templates(options = {}, &block) click to toggle source
# File lib/locale_mailer/concern.rb, line 29
def mail_with_localized_templates(options = {}, &block)
  options.symbolize_keys!

  options[:subject] ||= subject_from_locale(options)

  if template_exists? options[:template_name] || action_name, options[:template_path] || mailer_name
    mail_without_localized_templates(options, &block)
  elsif I18n.exists? action_i18n_path(options), I18n.locale
    mail_without_localized_templates options do |format|
      format.html {
        body_from_locale options.reverse_merge(layout: _layout(:html))
      }
      format.text {
        Loofah.scrub_fragment(body_from_locale(options.reverse_merge(layout: _layout(:text))), LocaleMailer::A2HREF).to_text
      }
    end
  else
    raise "LocaleMailer : Missing template and locale #{I18n.locale}.#{action_i18n_path(options)}"
  end
end
subject_from_locale(options = {}) click to toggle source
# File lib/locale_mailer/concern.rb, line 85
def subject_from_locale options = {}
  view_context.t 'subject', action_i18n_options(options)
end