Skip to content

Commit

Permalink
Merge pull request #405 from platanus/feat/mjml
Browse files Browse the repository at this point in the history
Feat/mjml
  • Loading branch information
flouMicaza authored Jun 13, 2022
2 parents 534c7dd + 6e699d9 commit 4b2213c
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 46 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Potassium Rails apps includes the following gems and technologies:
- [Tzinfo-Data](https://github.com/tzinfo/tzinfo-data) for updating timezone information
- [Faker](https://github.com/stympy/faker) for creating development data
- [Scout](https://github.com/scoutapp/scout_apm_ruby) for monitoring performance
- [Mjml](https://github.com/sighmon/mjml-rails) for mails style

The following optional integrations are also added:

Expand Down
2 changes: 1 addition & 1 deletion lib/potassium/assets/app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class ApplicationMailer < ActionMailer::Base
layout 'mailer'
layout 'default_mail'
end
6 changes: 6 additions & 0 deletions lib/potassium/assets/app/mailers/example_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ExampleMailer < ApplicationMailer
def example_mail
@email = params[:email]
mail(from: '[email protected]', to: @email, subject: 'Welcome to Potassium')
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<mj-section>
<mj-column>
<mj-text>
Hello <%= @email %>, welcome to Potassium
</mj-text>
</mj-column>
</mj-section>
49 changes: 49 additions & 0 deletions lib/potassium/assets/app/views/layouts/default_mail.html.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<mjml>
<mj-head>
<mj-attributes>
<mj-all font-family="'Helvetica Neue', Helvetica, Arial, sans-serif"></mj-all>
<mj-text font-weight="400" font-size="16px" color="#000000" line-height="24px" font-family="'Helvetica Neue', Helvetica, Arial, sans-serif"></mj-text>
</mj-attributes>
<mj-style inline="inline">
.body-section {
-webkit-box-shadow: 1px 4px 11px 0px rgba(0, 0, 0, 0.15);
-moz-box-shadow: 1px 4px 11px 0px rgba(0, 0, 0, 0.15);
box-shadow: 1px 4px 11px 0px rgba(0, 0, 0, 0.15);
}
</mj-style>
<mj-style inline="inline">
.text-link {
color: #5e6ebf
}
</mj-style>
<mj-style inline="inline">
.footer-link {
color: #888888
}
</mj-style>

</mj-head>
<mj-body width="600px">
<mj-section full-width="full-width" background-color="#71717A" padding-bottom="0">
<mj-column width="100%" background-color="#A1A1AA">
<mj-spacer height="25px" />
</mj-column>
</mj-section>
<mj-section background-color="#A1A1AA" padding-bottom="0" padding-top="0">
<mj-column>
<mj-image width="100px" src="<%= image_url('/mails/platanus-logo.png') %>" align="center">
</mj-column>
<mj-column width="100%">
<mj-spacer height="25px" />
</mj-column>
</mj-section>
<mj-wrapper padding-top="0" padding-bottom="0" css-class="body-section" background-color="#ffffff" padding-left="15px" padding-right="15px">
<%= yield %>
</mj-wrapper>
<mj-section background-color="#A1A1AA" padding-bottom="0" padding-top="0">
<mj-column width="100%">
<mj-spacer height="25px" />
</mj-column>
</mj-section>
</mj-body>
</mjml>
2 changes: 0 additions & 2 deletions lib/potassium/assets/config/mailer.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Rails.application.config.action_mailer.default_url_options = {
}

Rails.application.config.action_mailer.default_options = { from: ENV['DEFAULT_EMAIL_ADDRESS'] }
ASSET_HOST = ENV.fetch("ASSET_HOST", ENV.fetch("APPLICATION_HOST"))
Rails.application.config.action_mailer.asset_host = ASSET_HOST

if ENV["EMAIL_RECIPIENTS"].present?
Mail.register_interceptor RecipientInterceptor.new(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 22 additions & 10 deletions lib/potassium/recipes/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,32 @@ def dependencies(service)
gather_gem 'recipient_interceptor'
end

def config_environments
gsub_file 'config/environments/production.rb', /$\s*config.action_mailer.*/, ''
asset_host_dev = <<~RUBY
config.action_mailer.asset_host = "http://\#{ENV.fetch('APPLICATION_HOST')}"
RUBY
application asset_host_dev, env: "development"
asset_host_prod = <<~RUBY
config.action_mailer.asset_host = "https://\#{ENV.fetch('APPLICATION_HOST')}"
RUBY
application asset_host_prod, env: "production"
mailer_config = <<~RUBY
require Rails.root.join("config", "mailer")
RUBY

prepend_file "config/environments/production.rb", mailer_config
end

def config(service)
template "../assets/config/mailer.rb.erb", 'config/mailer.rb'
gsub_file 'config/environments/production.rb', /$\s*config.action_mailer.*/, ''

append_to_file '.env.development', "APPLICATION_HOST=localhost:3000\n"
append_to_file '.env.development', "EMAIL_RECIPIENTS=\n"

mailer_config =
<<~RUBY
require Rails.root.join("config", "mailer")
RUBY

prepend_file "config/environments/production.rb", mailer_config
copy_file '../assets/app/mailers/application_mailer.rb', 'app/mailers/application_mailer.rb', force: true

copy_file '../assets/app/mailers/application_mailer.rb', 'app/mailers/application_mailer.rb',
force: true
config_environments
send(service[:name])
end

Expand All @@ -88,7 +100,7 @@ def sendgrid
}
RUBY
inject_into_file 'config/mailer.rb', sendgrid_settings,
after: "Rails.application.config.action_mailer.delivery_method = :sendgrid\n"
after: "Rails.application.config.action_mailer.delivery_method = :sendgrid\n"
sendgrid_dev_settings = <<~RUBY
Rails.application.config.action_mailer.sendgrid_dev_settings = {
api_key: ENV['SENDGRID_API_KEY']
Expand Down
31 changes: 31 additions & 0 deletions lib/potassium/recipes/mjml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Recipes::Mjml < Rails::AppBuilder
def create
return if get(:email_service).to_s.downcase.to_sym == :none

gather_gem 'mjml-rails'
after(:gem_install) do
run 'bin/yarn add mjml'
mjml_config
end
end

def installed?
gem_exists?(/mjml-rails/)
end

def install
create
end
end

private

def mjml_config
copy_file '../assets/app/views/layouts/default_mail.html.mjml',
'app/views/layouts/default_mail.html.mjml', force: true
copy_file '../assets/app/mailers/example_mailer.rb', 'app/mailers/example_mailer.rb', force: true
copy_file '../assets/app/views/example_mailer/example_mail.html.mjml',
'app/views/example_mailer/example_mail.html.mjml', force: true
copy_file '../assets/public/mails/platanus-logo.png',
'public/mails/platanus-logo.png', force: true
end
1 change: 1 addition & 0 deletions lib/potassium/templates/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
create :admin
create :vue_admin
create :google_tag_manager
create :mjml
end

info "Gathered enough information. Applying the template. Wait a minute."
Expand Down
112 changes: 79 additions & 33 deletions spec/features/mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,104 @@
# rubocop:disable Rspec/MultipleMemoizedHelpers
require "spec_helper"

RSpec.describe "Mailer" do
let(:gemfile) { IO.read("#{project_path}/Gemfile") }
let(:mailer_config) { IO.read("#{project_path}/config/mailer.rb") }
let(:env) { IO.read("#{project_path}/.env.development") }
let(:dev_config) { IO.read("#{project_path}/config/environments/development.rb") }
let(:sidekiq_config) { IO.read("#{project_path}/config/sidekiq.yml") }
let(:prod_config) { IO.read("#{project_path}/config/environments/production.rb") }
let(:asset_host_dev) do
<<~RUBY
config.action_mailer.asset_host = "http://\#{ENV.fetch('APPLICATION_HOST')}"
RUBY
end
let(:asset_host_prod) do
<<~RUBY
config.action_mailer.asset_host = "https://\#{ENV.fetch('APPLICATION_HOST')}"
RUBY
end
let(:mailer_config_text) do
<<~RUBY
require Rails.root.join("config", "mailer")
RUBY
end

before(:all) { drop_dummy_database }

context "when selecting sendgrid as mailer" do
before(:all) do
remove_project_directory
create_dummy_project("email_service" => "sendgrid")
end
describe 'with mailer' do
let(:mailer_config) { IO.read("#{project_path}/config/mailer.rb") }
let(:sidekiq_config) { IO.read("#{project_path}/config/sidekiq.yml") }

context "when selecting sendgrid as mailer" do
before(:all) do
remove_project_directory
create_dummy_project("email_service" => "sendgrid")
end

it_behaves_like "name"
it { expect(gemfile).to include("send_grid_mailer") }

it { expect(gemfile).to include("send_grid_mailer") }
it "adds configuration to mailer.rb" do
expect(mailer_config).to include("delivery_method = :sendgrid")
expect(mailer_config).to include("sendgrid_settings = {")
expect(mailer_config).to include("api_key: ENV['SENDGRID_API_KEY']")
end

it "adds configuration to mailer.rb" do
expect(mailer_config).to include("delivery_method = :sendgrid")
expect(mailer_config).to include("sendgrid_settings = {")
expect(mailer_config).to include("api_key: ENV['SENDGRID_API_KEY']")
it "adds configuration to development.rb" do
expect(dev_config).to include("delivery_method = :sendgrid_dev")
expect(dev_config).to include("sendgrid_dev_settings = {")
expect(dev_config).to include("api_key: ENV['SENDGRID_API_KEY']")
end

it "adds configuration to production" do
expect(prod_config).to include(mailer_config_text)
expect(prod_config).to include(asset_host_prod)
end
end

it "adds configuration to development.rb" do
expect(dev_config).to include("delivery_method = :sendgrid_dev")
expect(dev_config).to include("sendgrid_dev_settings = {")
expect(dev_config).to include("api_key: ENV['SENDGRID_API_KEY']")
context "when selecting aws_ses as mailer" do
before(:all) do
remove_project_directory
create_dummy_project("email_service" => "aws_ses")
end

it_behaves_like "name"
it { expect(gemfile).to include("aws-sdk-rails") }
it { expect(gemfile).to include("letter_opener") }
it { expect(mailer_config).to include("delivery_method = :aws_sdk") }
it { expect(dev_config).to include("delivery_method = :letter_opener") }
end

it { expect(sidekiq_config).to include("- mailers") }
context "when selecting a mailer and sidekiq" do
before :all do
drop_dummy_database
remove_project_directory
create_dummy_project(
"background_processor" => true, "email_service" => 'sendgrid'
)
end

it { expect(sidekiq_config).to include("- mailers") }
end
end

context "when selecting aws_ses as mailer" do
context "when there is no mailer" do
before(:all) do
remove_project_directory
create_dummy_project("email_service" => "aws_ses")
create_dummy_project
end

it { expect(gemfile).to include("aws-sdk-rails") }
it { expect(gemfile).to include("letter_opener") }
it { expect(mailer_config).to include("delivery_method = :aws_sdk") }
it { expect(dev_config).to include("delivery_method = :letter_opener") }
it { expect(sidekiq_config).to include("- mailers") }
end
it { expect(gemfile).not_to include("letter_opener") }
it { expect(File.exists?("#{project_path}/config/mailer.rb")).to eq(false) }
it { expect(env).not_to include("EMAIL_RECIPIENTS") }

context "when selecting a mailer and sidekiq" do
before :all do
drop_dummy_database
remove_project_directory
create_dummy_project(
"background_processor" => true, "email_service" => 'sendgrid'
)
it "adds configuration to development" do
expect(dev_config).not_to include(asset_host_dev)
expect(dev_config).not_to include("delivery_method")
end

it { expect(sidekiq_config).to include("- mailers") }
it "adds configuration to production" do
expect(prod_config).not_to include(mailer_config_text)
expect(prod_config).not_to include(asset_host_prod)
end
end
end
53 changes: 53 additions & 0 deletions spec/features/mjml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require "spec_helper"

RSpec.describe "Mjml" do
let(:gemfile) { IO.read("#{project_path}/Gemfile") }
let(:mailer_config) { IO.read("#{project_path}/config/mailer.rb") }
let(:dev_config) { IO.read("#{project_path}/config/environments/development.rb") }
let(:sidekiq_config) { IO.read("#{project_path}/config/sidekiq.yml") }
let(:yarn_lock) { IO.read("#{project_path}/yarn.lock") }

before(:all) { drop_dummy_database }

context "when selecting a mailer" do
before(:all) do
remove_project_directory
create_dummy_project("email_service" => "sendgrid")
end

it 'adds gems and packages' do
expect(yarn_lock).to include("mjml")
expect(gemfile).to include("mjml-rails")
end

it 'creates example mailer' do
expect(File.exists?("#{project_path}/app/views/layouts/default_mail.html.mjml")).to eq(true)
expect(File.exists?("#{project_path}/app/mailers/example_mailer.rb")).to eq(true)
expect(File.exists?("#{project_path}/app/views/example_mailer/example_mail.html.mjml"))
.to eq(true)
expect(File.exists?("#{project_path}/public/mails/platanus-logo.png")).to eq(true)
end
end

context "when there is no mailer selected" do
before(:all) do
remove_project_directory
create_dummy_project
end

it 'adds gems and packages' do
expect(yarn_lock).not_to include("mjml")
expect(gemfile).not_to include("mjml-rails")
end

it 'creates example mailer' do
expect(File.exists?("#{project_path}/app/views/layouts/default_mail.html.mjml"))
.not_to eq(true)
expect(File.exists?("#{project_path}/app/mailers/example_mailer.rb"))
.not_to eq(true)
expect(File.exists?("#{project_path}/app/views/example_mailer/example_mail.html.mjml"))
.not_to eq(true)
expect(File.exists?("#{project_path}/public/mails/platanus-logo.png")).not_to eq(true)
end
end
end
5 changes: 5 additions & 0 deletions spec/support/shared_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RSpec.shared_examples "name" do
it { expect(sidekiq_config).to include("- mailers") }
it { expect(env).to include("EMAIL_RECIPIENTS") }
it { expect(dev_config).to include(asset_host_dev) }
end

0 comments on commit 4b2213c

Please sign in to comment.