Skip to content

Commit

Permalink
Merge pull request rails#18393 from y-yagi/fix_mailer
Browse files Browse the repository at this point in the history
follow up to rails#18074
  • Loading branch information
rafaelfranca committed Feb 18, 2015
2 parents 3c750c4 + af3eb59 commit 39c936b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
4 changes: 2 additions & 2 deletions actionmailer/lib/rails/generators/mailer/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ Example:

creates a Notifications mailer class, views, and test:
Mailer: app/mailers/notifications_mailer.rb
Views: app/views/notifications/signup.text.erb [...]
Test: test/mailers/notifications_test.rb
Views: app/views/notifications_mailer/signup.text.erb [...]
Test: test/mailers/notifications_mailer_test.rb

6 changes: 5 additions & 1 deletion railties/lib/rails/generators/erb/mailer/mailer_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class MailerGenerator < Base # :nodoc:
argument :actions, type: :array, default: [], banner: "method method"

def copy_view_files
view_base_path = File.join("app/views", class_path, file_name)
view_base_path = File.join("app/views", class_path, file_name + '_mailer')
empty_directory view_base_path

if self.behavior == :invoke
Expand All @@ -31,6 +31,10 @@ def copy_view_files
def formats
[:text, :html]
end

def file_name
@_file_name ||= super.gsub(/\_mailer/i, '')
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def create_test_files
def create_preview_files
template "preview.rb", File.join('test/mailers/previews', class_path, "#{file_name}_mailer_preview.rb")
end

protected
def file_name
@_file_name ||= super.gsub(/\_mailer/i, '')
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class <%= class_name %>MailerTest < ActionMailer::TestCase
<% actions.each do |action| -%>
test "<%= action %>" do
mail = <%= class_name %>.<%= action %>
mail = <%= class_name %>Mailer.<%= action %>
assert_equal <%= action.to_s.humanize.inspect %>, mail.subject
assert_equal ["[email protected]"], mail.to
assert_equal ["[email protected]"], mail.from
Expand Down
31 changes: 20 additions & 11 deletions railties/test/generators/mailer_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ def test_check_preview_class_collision

def test_invokes_default_text_template_engine
run_generator
assert_file "app/views/notifier/foo.text.erb" do |view|
assert_match(%r(\sapp/views/notifier/foo\.text\.erb), view)
assert_file "app/views/notifier_mailer/foo.text.erb" do |view|
assert_match(%r(\sapp/views/notifier_mailer/foo\.text\.erb), view)
assert_match(/<%= @greeting %>/, view)
end

assert_file "app/views/notifier/bar.text.erb" do |view|
assert_match(%r(\sapp/views/notifier/bar\.text\.erb), view)
assert_file "app/views/notifier_mailer/bar.text.erb" do |view|
assert_match(%r(\sapp/views/notifier_mailer/bar\.text\.erb), view)
assert_match(/<%= @greeting %>/, view)
end

Expand All @@ -95,13 +95,13 @@ def test_invokes_default_text_template_engine

def test_invokes_default_html_template_engine
run_generator
assert_file "app/views/notifier/foo.html.erb" do |view|
assert_match(%r(\sapp/views/notifier/foo\.html\.erb), view)
assert_file "app/views/notifier_mailer/foo.html.erb" do |view|
assert_match(%r(\sapp/views/notifier_mailer/foo\.html\.erb), view)
assert_match(/<%= @greeting %>/, view)
end

assert_file "app/views/notifier/bar.html.erb" do |view|
assert_match(%r(\sapp/views/notifier/bar\.html\.erb), view)
assert_file "app/views/notifier_mailer/bar.html.erb" do |view|
assert_match(%r(\sapp/views/notifier_mailer/bar\.html\.erb), view)
assert_match(/<%= @greeting %>/, view)
end

Expand All @@ -112,7 +112,7 @@ def test_invokes_default_html_template_engine

def test_invokes_default_template_engine_even_with_no_action
run_generator ["notifier"]
assert_file "app/views/notifier"
assert_file "app/views/notifier_mailer"
assert_file "app/views/layouts/mailer.text.erb"
assert_file "app/views/layouts/mailer.html.erb"
end
Expand All @@ -133,8 +133,8 @@ def test_mailer_with_namedspaced_mailer
assert_match(/class Farm::AnimalMailerPreview < ActionMailer::Preview/, preview)
assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal\/moos/, preview)
end
assert_file "app/views/farm/animal/moos.text.erb"
assert_file "app/views/farm/animal/moos.html.erb"
assert_file "app/views/farm/animal_mailer/moos.text.erb"
assert_file "app/views/farm/animal_mailer/moos.html.erb"
end

def test_actions_are_turned_into_methods
Expand Down Expand Up @@ -173,5 +173,14 @@ def test_mailer_suffix_is_not_duplicated

assert_no_file "app/mailers/notifier_mailer_mailer.rb"
assert_file "app/mailers/notifier_mailer.rb"

assert_no_file "app/views/notifier_mailer_mailer/"
assert_file "app/views/notifier_mailer/"

assert_no_file "test/mailers/notifier_mailer_mailer_test.rb"
assert_file "test/mailers/notifier_mailer_test.rb"

assert_no_file "test/mailers/previews/notifier_mailer_mailer_preview.rb"
assert_file "test/mailers/previews/notifier_mailer_preview.rb"
end
end
10 changes: 5 additions & 5 deletions railties/test/generators/namespaced_generators_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,20 @@ def test_invokes_default_test_framework

def test_invokes_default_template_engine
run_generator
assert_file "app/views/test_app/notifier/foo.text.erb" do |view|
assert_match(%r(app/views/test_app/notifier/foo\.text\.erb), view)
assert_file "app/views/test_app/notifier_mailer/foo.text.erb" do |view|
assert_match(%r(app/views/test_app/notifier_mailer/foo\.text\.erb), view)
assert_match(/<%= @greeting %>/, view)
end

assert_file "app/views/test_app/notifier/bar.text.erb" do |view|
assert_match(%r(app/views/test_app/notifier/bar\.text\.erb), view)
assert_file "app/views/test_app/notifier_mailer/bar.text.erb" do |view|
assert_match(%r(app/views/test_app/notifier_mailer/bar\.text\.erb), view)
assert_match(/<%= @greeting %>/, view)
end
end

def test_invokes_default_template_engine_even_with_no_action
run_generator ["notifier"]
assert_file "app/views/test_app/notifier"
assert_file "app/views/test_app/notifier_mailer"
end
end

Expand Down

0 comments on commit 39c936b

Please sign in to comment.