Class: Spree::Emails::Template

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree/emails/template.rb

Overview

One Liquid email template: a file on disk, with optional front matter holding its subject, or a store's saved version with the subject in its own field.

Constant Summary collapse

FRONT_MATTER =
/\A---\s*\n(.*?)\n---\s*(?:\n|\z)/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, path: nil, subject: nil, body: nil) ⇒ Template

Returns a new instance of Template.

Parameters:

  • key (String) —

    the template key, e.g. "spree/order_mailer/confirm_email"

  • path (String, nil) (defaults to: nil) —

    the file on disk

  • subject (String, nil) (defaults to: nil) —

    the subject, for a template not read from a file

  • body (String, nil) (defaults to: nil) —

    the body, for a template not read from a file



15
16
17
18
19
20
# File 'app/services/spree/emails/template.rb', line 15

def initialize(key:, path: nil, subject: nil, body: nil)
  @key = key
  @path = path
  @subject = subject
  @body = body
end

Instance Attribute Details

#key ⇒ Object (readonly)

Returns the value of attribute key.



9
10
11
# File 'app/services/spree/emails/template.rb', line 9

def key
  @key
end

#path ⇒ Object (readonly)

Returns the value of attribute path.



9
10
11
# File 'app/services/spree/emails/template.rb', line 9

def path
  @path
end

Instance Method Details

#body ⇒ String

Returns the template without its front matter.

Returns:

  • (String) —

    the template without its front matter



33
34
35
# File 'app/services/spree/emails/template.rb', line 33

def body
  stored? ? @body.to_s : source.sub(FRONT_MATTER, '')
end

#stored? ⇒ Boolean

Returns whether this is a store's saved template or a draft, rather than a file.

Returns:

  • (Boolean) —

    whether this is a store's saved template or a draft, rather than a file



23
24
25
# File 'app/services/spree/emails/template.rb', line 23

def stored?
  path.nil?
end

#subject ⇒ String?

Returns the subject line as Liquid.

Returns:

  • (String, nil) —

    the subject line as Liquid



28
29
30
# File 'app/services/spree/emails/template.rb', line 28

def subject
  stored? ? @subject : front_matter['subject']
end