Sha256: 5c093c1e84e7cf0f1f285b395d09a0f5238a054b5bca73f8642c2108953fd452

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module EffectiveEmailTemplates
  class Importer
    def self.import
      new().execute(overwrite: false)
    end

    def self.overwrite
      new().execute(overwrite: true)
    end

    def execute(overwrite:)
      Dir[Rails.root.join('app', 'views', '**', '*.liquid')].each do |filepath|
        name = File.basename(filepath, '.liquid')
        email_template = Effective::EmailTemplate.find_or_initialize_by(template_name: name)

        if email_template.persisted? && !overwrite
          puts "SKIPPED #{filename(filepath)}"; next
        end

        save(email_template, filepath)
      end
    end

    private

    def save(email_template, filepath)
      file = File.new(filepath, 'r')

      attributes = begin
        content = YAML.load(file)
        content.kind_of?(Hash) ? content : {}
      end

      body = File.open(file) do |f|
        content = f.read
        match = content.match(/(---+(.|\n)+---+)/)
        content.gsub(match[1], '').strip if match
      end

      email_template.assign_attributes(attributes)
      email_template.assign_attributes(body: body)

      if email_template.save
        puts "SUCCESS #{filename(filepath)}"
      else
        puts "ERROR #{filename(filepath)}: #{email_template.errors.full_messages.to_sentence}"
      end
    end

    def filename(filepath)
      filepath.sub(Rails.root.to_s + '/app/', '')
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
effective_email_templates-1.0.1 lib/effective_email_templates/importer.rb
effective_email_templates-1.0.0 lib/effective_email_templates/importer.rb