Class: Nisetegami::Template

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/nisetegami/template.rb

Constant Summary collapse

CONTENT =

constants

[:subject, :body_html, :body_text]
HEADERS =
[:cc, :bcc, :reply_to, :from]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.lookup(mailer_instance) ⇒ Object

Accepts an instance of the ActionMailer::Base subclass and attempts to find associated template using name and action_name of the mailer.



42
43
44
# File 'app/models/nisetegami/template.rb', line 42

def self.lookup(mailer_instance)
  relation.where(mailer: mailer_instance.class.name, action: mailer_instance.action_name).first
end

Instance Method Details

#headers(variables = {}) ⇒ Object



97
98
99
100
101
102
# File 'app/models/nisetegami/template.rb', line 97

def headers(variables = {})
  options = { subject: render_subject(variables) }
  options[:css] = associated_stylesheets unless only_text?
  HEADERS.each { |header| options[header] = self[header] unless self[header].blank? }
  options
end

#layout_html_pathObject



46
47
48
# File 'app/models/nisetegami/template.rb', line 46

def layout_html_path
  layout_html && File.join(Nisetegami.layouts_path, layout_html)
end

#layout_text_pathObject



50
51
52
# File 'app/models/nisetegami/template.rb', line 50

def layout_text_path
  layout_text && File.join(Nisetegami.layouts_path, layout_text)
end

#mailerObject



75
76
77
78
# File 'app/models/nisetegami/template.rb', line 75

def mailer
  @_mailer ||= self[:mailer].constantize
rescue NameError
end

#mappingObject

Returns a mapping for self, as a Hash.



55
56
57
# File 'app/models/nisetegami/template.rb', line 55

def mapping
  @_mapping ||= ::Nisetegami.mapping.lookup(mailer, action)
end

#message(recipient, variables) ⇒ Object



104
105
106
107
# File 'app/models/nisetegami/template.rb', line 104

def message(recipient, variables)
  return nil unless valid?
  mailer.testing(action, recipient, variables)
end

#recentObject

scopes



14
# File 'app/models/nisetegami/template.rb', line 14

scope :recent,    order('id DESC')

#variable_mappingObject



65
66
67
68
69
70
71
72
73
# File 'app/models/nisetegami/template.rb', line 65

def variable_mapping
  @_variable_mapping ||= begin
    mapping.each_with_object([]) do |(variable, thing), array|
      # @todo: store variable_mapping in mapping instead of classes [?]
      meths = Nisetegami::Utils.liquid_methods_for(Nisetegami.cast[thing])
      array << (meths.blank? ? variable.to_sym : { variable.to_sym => meths })
    end
  end
end

#variable_namesObject

Returns a list of variables for self, using a Nisetegami::Mapping definitions.



61
62
63
# File 'app/models/nisetegami/template.rb', line 61

def variable_names
  @_variable_names ||= mapping.keys
end