Skip to content

Commit

Permalink
Merge pull request #24164 from prathamesh-sonpatki/fix-application-ma…
Browse files Browse the repository at this point in the history
…iler

Correctly generate application_mailer.rb in mountable engines
  • Loading branch information
rafaelfranca committed Mar 24, 2016
2 parents fbe6e4e + 4d0bf49 commit 878c2bb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions actionmailer/lib/rails/generators/mailer/mailer_generator.rb
Expand Up @@ -11,8 +11,8 @@ def create_mailer_file
template "mailer.rb", File.join('app/mailers', class_path, "#{file_name}_mailer.rb")

in_root do
if self.behavior == :invoke && !File.exist?('app/mailers/application_mailer.rb')
template 'application_mailer.rb', 'app/mailers/application_mailer.rb'
if self.behavior == :invoke && !File.exist?(application_mailer_file_name)
template 'application_mailer.rb', application_mailer_file_name
end
end
end
Expand All @@ -23,6 +23,15 @@ def create_mailer_file
def file_name
@_file_name ||= super.gsub(/_mailer/i, '')
end

private
def application_mailer_file_name
@_application_mailer_file_name ||= if mountable_engine?
"app/mailers/#{namespaced_path}/application_mailer.rb"
else
"app/mailers/application_mailer.rb"
end
end
end
end
end
@@ -1,4 +1,6 @@
<% module_namespacing do %>
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
layout 'mailer'
end
<% end %>
13 changes: 13 additions & 0 deletions railties/test/generators/plugin_generator_test.rb
Expand Up @@ -656,6 +656,19 @@ def test_generate_application_record_when_does_not_exist_in_mountable_engine
end
end

def test_generate_application_mailer_when_does_not_exist_in_mountable_engine
run_generator [destination_root, '--mountable']
FileUtils.rm "#{destination_root}/app/mailers/bukkits/application_mailer.rb"
capture(:stdout) do
`#{destination_root}/bin/rails g mailer User`
end

assert_file "#{destination_root}/app/mailers/bukkits/application_mailer.rb" do |mailer|
assert_match(/module Bukkits/, mailer)
assert_match(/class ApplicationMailer < ActionMailer::Base/, mailer)
end
end

def test_after_bundle_callback
path = 'http://example.org/rails_template'
template = %{ after_bundle { run 'echo ran after_bundle' } }
Expand Down

0 comments on commit 878c2bb

Please sign in to comment.