Sha256: 0098e23b080445d339b529cafb4e301416d1629a719612df2e0ad0329c19af19

Contents?: true

Size: 1.23 KB

Versions: 16

Compression:

Stored size: 1.23 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

  private

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

  #
  # 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
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
web47core-1.1.16 lib/app/models/template.rb
web47core-1.1.15 lib/app/models/template.rb
web47core-1.1.14 lib/app/models/template.rb
web47core-1.1.13 lib/app/models/template.rb
web47core-1.1.12 lib/app/models/template.rb
web47core-1.1.11 lib/app/models/template.rb
web47core-2.0.1 lib/app/models/template.rb
web47core-2.0.0 lib/app/models/template.rb
web47core-1.1.10 lib/app/models/template.rb
web47core-1.1.9 lib/app/models/template.rb
web47core-1.1.8 lib/app/models/template.rb
web47core-1.1.7 lib/app/models/template.rb
web47core-1.1.6 lib/app/models/template.rb
web47core-1.1.5 lib/app/models/template.rb
web47core-1.1.4 lib/app/models/template.rb
web47core-1.1.3 lib/app/models/template.rb