diff --git a/lib/potassium/recipes/mjml.rb b/lib/potassium/recipes/mjml.rb new file mode 100644 index 00000000..81f1504b --- /dev/null +++ b/lib/potassium/recipes/mjml.rb @@ -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 diff --git a/spec/features/mjml_spec.rb b/spec/features/mjml_spec.rb new file mode 100644 index 00000000..2289fe41 --- /dev/null +++ b/spec/features/mjml_spec.rb @@ -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