Method: EffectiveEmailTemplates::Importer#execute

Defined in:
lib/effective_email_templates/importer.rb

#execute(overwrite:, paths: nil, quiet: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/effective_email_templates/importer.rb', line 11

def execute(overwrite:, paths: nil, quiet: false)
  return false unless ActiveRecord::Base.connection.table_exists?(EffectiveEmailTemplates.email_templates_table_name || :email_templates)
  return false if defined?(Tenant) && Tenant.current.blank?
  return false if EffectiveEmailTemplates.mailer_froms.blank?

  paths ||= if defined?(Tenant)
    ActionController::Base.view_paths.reject { |view| view.path.include?('/apps/') }.map(&:path) + Tenant.view_paths.map(&:to_s)
  else
    ActionController::Base.view_paths.map(&:path)
  end

  paths.each do |path|
    Dir[Rails.root.join(path, '**', '*_mailer/', '*.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)}") unless quiet
        next
      end

      save(email_template, filepath, quiet: quiet)
    end
  end
end