Sha256: c7f41afb132f703d6f3d6749efaa29966070a27950ddd979c23716f3da4eb934

Contents?: true

Size: 1.22 KB

Versions: 33

Compression:

Stored size: 1.22 KB

Contents

#
# Base template object for managing all communications templates
#
class Template
  include StandardModel
  #
  # Fields
  #
  field :name, type: String
  field :template, type: String
  #
  # Relationships
  #
  belongs_to :account, inverse_of: :templates
  #
  # Validations
  #
  validates :name, uniqueness: { scope: :account_id }
  validates :name, presence: true
  validates :template, presence: true
  validate :valid_liquid_template

  #
  # Retrieve the template out of the project
  #
  def self.from_file(template_name, format: 'liquid', prefix: nil, delivery_channel: 'email')
    file_name = [template_name, prefix, format].compact.join('.')
    if File.exist?(Rails.root.join('lib/templates', delivery_channel, file_name))
      File.open(Rails.root.join('lib/templates', delivery_channel, file_name))
    else
      File.open(File.join(__dir__, '../../../lib/templates', delivery_channel, file_name))
    end.read
  rescue StandardError
    nil
  end

  private

  #
  # Ensure that the template is correct from a liquid statement
  #
  def valid_liquid_template
    Liquid::Template.parse(template).nil?
  rescue StandardError => error
    errors.add(:template, "Invalid liquid text in template: #{error.message}")
    false
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
web47core-3.2.20 lib/app/models/template.rb
web47core-3.2.19 lib/app/models/template.rb
web47core-3.2.18 lib/app/models/template.rb
web47core-3.2.17 lib/app/models/template.rb
web47core-3.2.16 lib/app/models/template.rb
web47core-3.2.15 lib/app/models/template.rb
web47core-3.2.14 lib/app/models/template.rb
web47core-3.2.13 lib/app/models/template.rb
web47core-3.2.12 lib/app/models/template.rb
web47core-3.2.9 lib/app/models/template.rb
web47core-3.2.8 lib/app/models/template.rb
web47core-3.2.7 lib/app/models/template.rb
web47core-3.2.6 lib/app/models/template.rb
web47core-3.2.5 lib/app/models/template.rb
web47core-3.2.4 lib/app/models/template.rb
web47core-2.2.20 lib/app/models/template.rb
web47core-2.2.19 lib/app/models/template.rb
web47core-3.2.3 lib/app/models/template.rb
web47core-3.2.2 lib/app/models/template.rb
web47core-2.2.15 lib/app/models/template.rb