Class: Rolemodel::MailersGenerator

Inherits:
GeneratorBase
  • Object
show all
Defined in:
lib/generators/rolemodel/mailers/mailers_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_action_mailer_asset_hostObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/generators/rolemodel/mailers/mailers_generator.rb', line 20

def add_action_mailer_asset_host
  unless File.exist?(Rails.root.join('config/initializers/devise.rb'))
    inject_into_file 'config/environments/development.rb',
                     after: "config.action_mailer.perform_caching = false\n" do
      optimize_indentation <<~'RUBY', 2

        # Default mailing host suggested by Devise installation instructions
        config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
      RUBY
    end
  end

  inject_into_file 'config/environments/development.rb',
                   after: "config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }\n" do
    optimize_indentation <<~'RUBY', 2

      # Ensure premailer_rails can point to webpack compiled resources
      # https://github.com/fphilipe/premailer-rails/issues/232#issuecomment-839819705
      config.action_mailer.asset_host = 'http://localhost:3000'
    RUBY
  end
end

#add_mailer_cssObject



76
77
78
# File 'lib/generators/rolemodel/mailers/mailers_generator.rb', line 76

def add_mailer_css
  copy_file 'app/assets/stylesheets/mailer.scss'
end

#add_mailer_layoutObject



80
81
82
# File 'lib/generators/rolemodel/mailers/mailers_generator.rb', line 80

def add_mailer_layout
  copy_file 'app/views/layouts/mailer.html.slim'
end

#add_mailer_templateObject



93
94
95
96
97
# File 'lib/generators/rolemodel/mailers/mailers_generator.rb', line 93

def add_mailer_template
  copy_file 'app/mailers/example_mailer.rb'
  copy_file 'app/views/example_mailer/example_email.html.slim'
  copy_file 'spec/mailers/previews/example_mailer_preview.rb'
end

#add_mailer_template_logoObject



84
85
86
# File 'lib/generators/rolemodel/mailers/mailers_generator.rb', line 84

def 
  copy_file 'public/logo.png'
end

#add_premailer_rails_configObject



11
12
13
# File 'lib/generators/rolemodel/mailers/mailers_generator.rb', line 11

def add_premailer_rails_config
  copy_file 'config/initializers/premailer_rails.rb'
end

#add_production_mailer_defaultsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/rolemodel/mailers/mailers_generator.rb', line 43

def add_production_mailer_defaults
  prepend_to_file 'config/environments/production.rb',
                  "require Rails.root.join('app/mailers/staging_mailer_interceptor')\n"

  inject_into_file 'config/environments/production.rb', after: "config.action_mailer.perform_caching = false\n" do
    optimize_indentation <<~'RUBY', 2
      # Prevent live user emails from being sent out in staging
      if ENV['WHITELISTED_EMAILS'].present?
        ActionMailer::Base.register_interceptor(StagingMailerInterceptor)
      end

      host = ENV['PRODUCTION_HOST']

      # Ensure premailer_rails can point to webpack compiled resources
      # https://github.com/fphilipe/premailer-rails/issues/232#issuecomment-839819705
      config.action_mailer.asset_host = host

      config.action_mailer.default_url_options = { host: host }
      config.action_mailer.delivery_method = :smtp
      config.action_mailer.perform_deliveries = true
      config.action_mailer.smtp_settings = {
        user_name: 'apikey',
        password: ENV['SENDGRID_API_KEY'],
        domain: host,
        address: 'smtp.sendgrid.net',
        port: 587,
        authentication: :plain,
        enable_starttls_auto: true
      }
    RUBY
  end
end

#include_postcss_calcObject



15
16
17
18
# File 'lib/generators/rolemodel/mailers/mailers_generator.rb', line 15

def include_postcss_calc
  inject_into_file 'postcss.config.cjs', ",\n    require('postcss-calc')",
                   after: /^\s*require\('postcss-preset-env'\)\({(.|\n)*?}\)/
end

#install_premailer_railsObject



7
8
9
# File 'lib/generators/rolemodel/mailers/mailers_generator.rb', line 7

def install_premailer_rails
  bundle_command 'add premailer-rails'
end

#remove_default_mailer_templateObject



88
89
90
91
# File 'lib/generators/rolemodel/mailers/mailers_generator.rb', line 88

def remove_default_mailer_template
  remove_file 'app/views/layouts/mailer.html.erb'
  remove_file 'app/views/layouts/mailer.text.erb'
end