Sha256: 196573af96a3bc330147f4ec8b9545d343af787372cd51d2519c6f074f146ad5

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module EffectiveEmailTemplates
  class TemplateImporter
    def self.invoke(importer = new)
      importer.invoke
    end

    def invoke
      binding.pry
      Dir[Rails.root.join('app', 'views', '**', '*.liquid')].each do |liquid_template_filepath|
        slug = File.basename(liquid_template_filepath, '.liquid')
        binding.pry
        next if Effective::EmailTemplate.where(slug: slug).present?

        template = Effective::EmailTemplate.new(slug: slug)

        file = File.new(liquid_template_filepath, "r")
        template = add_template_meta(file, template)
        template.body = extract_template_body(file)
        template.save
        print_errors(template, liquid_template_filepath) unless template.valid?
      end
    end

  private

    def add_template_meta(file, template)
      template.attributes = File.open(file) do |f|
        attr = YAML::load(f)
        attr.is_a?(Hash) ? attr : {}
      end
      template
    end

    def extract_template_body file
      contents = file.read
      return unless match = contents.match(/(---+(.|\n)+---+)/)
      contents.gsub(match[1], '').strip
    end

    def print_errors(template, liquid_template_filepath)
      puts "ERROR -- There was one or more validation errors while uploading:"
      puts "  Email Template: #{liquid_template_filepath}"
      template.errors.each do |attribute, error|
        puts "  -> #{attribute} #{error}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
effective_email_templates-0.3.3 lib/effective_email_templates/template_importer.rb~