Class: Effective::EmailTemplate

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::TextHelper
Defined in:
app/models/effective/email_template.rb

Constant Summary collapse

CONTENT_TYPE_PLAIN =
'text/plain'
CONTENT_TYPE_HTML =
'text/html'
CONTENT_TYPES =
[CONTENT_TYPE_PLAIN, CONTENT_TYPE_HTML]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_userObject

Returns the value of attribute current_user.



7
8
9
# File 'app/models/effective/email_template.rb', line 7

def current_user
  @current_user
end

Instance Method Details

#body_html?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/effective/email_template.rb', line 64

def body_html?
  body.present? && (body.include?('</p>') || body.include?('</div>'))
end

#body_plain?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/effective/email_template.rb', line 68

def body_plain?
  body.present? && !(body.include?('</p>') || body.include?('</div>'))
end

#html?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/effective/email_template.rb', line 56

def html?
  content_type == CONTENT_TYPE_HTML
end

#plain?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/effective/email_template.rb', line 60

def plain?
  content_type == CONTENT_TYPE_PLAIN
end

#render(assigns = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/effective/email_template.rb', line 72

def render(assigns = {})
  assigns = deep_stringify_assigns(assigns)

  {
    from: from,
    cc: cc.presence,
    bcc: bcc.presence,
    content_type: content_type,
    subject: template_subject.render(assigns),
    body: template_body.render(assigns)
  }.compact
end

#save_as_html!Object



93
94
95
96
97
# File 'app/models/effective/email_template.rb', line 93

def save_as_html!
  assign_attributes(content_type: 'text/html')
  assign_attributes(body: simple_format(body)) if body_plain?
  save!
end

#save_as_plain!Object



99
100
101
102
103
# File 'app/models/effective/email_template.rb', line 99

def save_as_plain!
  assign_attributes(content_type: 'text/plain')
  assign_attributes(body: strip_tags(body)) if body_html?
  save!
end

#template_variablesObject



85
86
87
88
89
90
91
# File 'app/models/effective/email_template.rb', line 85

def template_variables
  [template_body.presence, template_subject.presence].compact.map do |template|
    Liquid::ParseTreeVisitor.for(template.root).add_callback_for(Liquid::VariableLookup) do |node|
      [node.name, *node.lookups].join('.')
    end.visit
  end.flatten.uniq.compact
end

#to_sObject



52
53
54
# File 'app/models/effective/email_template.rb', line 52

def to_s
  template_name.presence || 'New Email Template'
end