Permalink
Browse files

Merge pull request #13425 from kassio/master

Generates html and text templates for mailers by default.
  • Loading branch information...
2 parents 92f9ff8 + 02c814c commit f39fce5b7def0b9e6fe19a51af926c221ce3b8bb @dhh dhh committed Dec 21, 2013
View
@@ -1,3 +1,7 @@
+* Generates `html` and `text` templates for mailers by default.
+
+ *Kassio Borges*
+
* Move `secret_key_base` from `config/initializers/secret_token.rb`
to `config/secrets.yml`.
@@ -5,6 +5,10 @@ module Generators # :nodoc:
class Base < Rails::Generators::NamedBase #:nodoc:
protected
+ def formats
+ format
+ end
+
def format
:html
end
@@ -13,7 +17,7 @@ def handler
:erb
end
- def filename_with_extensions(name)
+ def filename_with_extensions(name, format)
[name, format, handler].compact.join(".")
end
end
@@ -11,8 +11,10 @@ def copy_view_files
actions.each do |action|
@action = action
- @path = File.join(base_path, filename_with_extensions(action))
- template filename_with_extensions(:view), @path
+ Array(formats).each do |format|
+ @path = File.join(base_path, filename_with_extensions(action, format))
+ template filename_with_extensions(:view, format), @path
+ end
end
end
end
@@ -5,8 +5,8 @@ module Generators # :nodoc:
class MailerGenerator < ControllerGenerator # :nodoc:
protected
- def format
- :text
+ def formats
+ [:text, :html]
end
end
end
@@ -0,0 +1,5 @@
+<h1><%= class_name %>#<%= @action %></h1>
+
+<p>
+ <%%= @greeting %>, find me in app/views/<%= @path %>
+</p>
@@ -14,8 +14,10 @@ def create_root_folder
def copy_view_files
available_views.each do |view|
- filename = filename_with_extensions(view)
- template filename, File.join("app/views", controller_file_path, filename)
+ Array(formats).each do |format|
+ filename = filename_with_extensions(view, format)
+ template filename, File.join("app/views", controller_file_path, filename)
+ end
end
end
@@ -63,7 +63,7 @@ def test_check_preview_class_collision
Object.send :remove_const, :NotifierPreview
end
- def test_invokes_default_template_engine
+ def test_invokes_default_text_template_engine
run_generator
assert_file "app/views/notifier/foo.text.erb" do |view|
assert_match(%r(app/views/notifier/foo\.text\.erb), view)
@@ -76,6 +76,19 @@ def test_invokes_default_template_engine
end
end
+ def test_invokes_default_html_template_engine
+ run_generator
+ assert_file "app/views/notifier/foo.html.erb" do |view|
+ assert_match(%r(app/views/notifier/foo\.html\.erb), view)
+ assert_match(/<%= @greeting %>/, view)
+ end
+
+ assert_file "app/views/notifier/bar.html.erb" do |view|
+ assert_match(%r(app/views/notifier/bar\.html\.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/notifier"
@@ -96,6 +109,7 @@ def test_mailer_with_namedspaced_mailer
assert_match(/class Farm::AnimalPreview < ActionMailer::Preview/, mailer)
end
assert_file "app/views/farm/animal/moos.text.erb"
+ assert_file "app/views/farm/animal/moos.html.erb"
end
def test_actions_are_turned_into_methods

0 comments on commit f39fce5

Please sign in to comment.