Class: Spree::Emails::TemplateResolver

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

Overview

Finds the Liquid template behind an email key.

With a store, an editable template (see Spree.editable_email_templates) is looked up in the store first: its published version in the email's language, then its version for every language. Otherwise, and for every other template, the app's own file wins over the one a gem ships. drafts puts unsaved templates in front of everything, for previews and for checking a draft before it is published.

Defined Under Namespace

Classes: InvalidKey

Constant Summary collapse

KEY_FORMAT =
%r{\A[a-z0-9_]+(?:/[a-z0-9_]+)*\z}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_paths, store: nil, locale: nil, drafts: {}) ⇒ TemplateResolver

Returns a new instance of TemplateResolver.

Parameters:

  • view_paths (Array<String, Pathname>) —

    directories to search, highest precedence first

  • store (Spree::Store, nil) (defaults to: nil) —

    the store whose published templates apply

  • locale (String, Symbol, nil) (defaults to: nil) —

    the email's language

  • drafts (Hash{String => Spree::Emails::Template}) (defaults to: {}) —

    unsaved templates by key



29
30
31
32
33
34
35
# File 'app/services/spree/emails/template_resolver.rb', line 29

def initialize(view_paths, store: nil, locale: nil, drafts: {})
  @view_paths = view_paths.map(&:to_s).uniq
  @store = store
  @locale = locale.to_s
  @drafts = drafts
  @stored = {}
end

Instance Attribute Details

#store ⇒ Spree::Store? (readonly)

Returns the store whose published templates apply.

Returns:

  • (Spree::Store, nil) —

    the store whose published templates apply



15
16
17
# File 'app/services/spree/emails/template_resolver.rb', line 15

def store
  @store
end

Class Method Details

.for_mailers(**options) ⇒ Spree::Emails::TemplateResolver

Returns searching the directories Spree's mailers render from.

Parameters:

Returns:



21
22
23
# File 'app/services/spree/emails/template_resolver.rb', line 21

def self.for_mailers(**options)
  new(Spree::BaseMailer.view_paths.paths.map(&:path), **options)
end

Instance Method Details

#find(key) ⇒ Spree::Emails::Template?

Parameters:

  • key (String) —

    e.g. "spree/order_mailer/confirm_email"

Returns:



39
40
41
42
# File 'app/services/spree/emails/template_resolver.rb', line 39

def find(key)
  validate!(key)
  @drafts[key] || stored(key) || find_file(key, '.liquid')
end

#find_default(key) ⇒ Spree::Emails::Template?

The default: what answers for the key without the store's templates or drafts.

Parameters:

  • key (String)

Returns:



48
49
50
51
# File 'app/services/spree/emails/template_resolver.rb', line 48

def find_default(key)
  validate!(key)
  Spree.editable_email_templates[key]&.kind == :partial ? partial_file(key) : find_file(key, '.liquid')
end

#find_partial(name) ⇒ Spree::Emails::Template?

Returns the store's version, or "spree/shared/_line_item.liquid".

Parameters:

  • name (String) —

    a partial name, e.g. "spree/shared/line_item"

Returns:



67
68
69
70
# File 'app/services/spree/emails/template_resolver.rb', line 67

def find_partial(name)
  validate!(name)
  @drafts[name] || stored(name) || partial_file(name)
end

#find_text(key) ⇒ Spree::Emails::Template?

A hand-written plain-text part. A store's saved template has none, so its text is always generated from its HTML.

Parameters:

  • key (String)

Returns:



58
59
60
61
62
63
# File 'app/services/spree/emails/template_resolver.rb', line 58

def find_text(key)
  validate!(key)
  return if @drafts[key] || stored(key)

  find_file(key, '.text.liquid')
end

#stored_templates_used? ⇒ Boolean

Returns whether a store's published template answered any lookup so far.

Returns:

  • (Boolean) —

    whether a store's published template answered any lookup so far



73
74
75
# File 'app/services/spree/emails/template_resolver.rb', line 73

def stored_templates_used?
  @stored.values.any?
end