Class: Spree::Emails::TemplateResolver
- Inherits:
-
Object
- Object
- Spree::Emails::TemplateResolver
- 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
-
#store ⇒ Spree::Store?
readonly
The store whose published templates apply.
Class Method Summary collapse
-
.for_mailers(**options) ⇒ Spree::Emails::TemplateResolver
Searching the directories Spree's mailers render from.
Instance Method Summary collapse
- #find(key) ⇒ Spree::Emails::Template?
-
#find_default(key) ⇒ Spree::Emails::Template?
The default: what answers for the key without the store's templates or drafts.
-
#find_partial(name) ⇒ Spree::Emails::Template?
The store's version, or "spree/shared/_line_item.liquid".
-
#find_text(key) ⇒ Spree::Emails::Template?
A hand-written plain-text part.
-
#initialize(view_paths, store: nil, locale: nil, drafts: {}) ⇒ TemplateResolver
constructor
A new instance of TemplateResolver.
-
#stored_templates_used? ⇒ Boolean
Whether a store's published template answered any lookup so far.
Constructor Details
#initialize(view_paths, store: nil, locale: nil, drafts: {}) ⇒ TemplateResolver
Returns a new instance of TemplateResolver.
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.
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.
21 22 23 |
# File 'app/services/spree/emails/template_resolver.rb', line 21 def self.for_mailers(**) new(Spree::BaseMailer.view_paths.paths.map(&:path), **) end |
Instance Method Details
#find(key) ⇒ Spree::Emails::Template?
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.
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".
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.
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.
73 74 75 |
# File 'app/services/spree/emails/template_resolver.rb', line 73 def stored_templates_used? @stored.values.any? end |