Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #94 from mfung/add-correct-mailer-generators
Browse files Browse the repository at this point in the history
Mailer generator now creates mailer layouts and both html/text templates
  • Loading branch information
indirect committed Mar 11, 2015
2 parents 04a7501 + 7c08f6c commit 137882f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/generators/haml/mailer/mailer_generator.rb
Expand Up @@ -5,10 +5,30 @@ module Generators
class MailerGenerator < ControllerGenerator
source_root File.expand_path("../templates", __FILE__)

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

if self.behavior == :invoke
formats.each do |format|
layout_path = File.join("app/views/layouts", filename_with_extensions("mailer", format))
template filename_with_extensions(:layout, format), layout_path
end
end

actions.each do |action|
@action = action

def format
:text
formats.each do |format|
@path = File.join(view_base_path, filename_with_extensions(action, format))
template filename_with_extensions(:view, format), @path
end
end
end

protected
def formats
[:text, :html]
end

end
Expand Down
3 changes: 3 additions & 0 deletions lib/generators/haml/mailer/templates/layout.html.haml
@@ -0,0 +1,3 @@
%hmtl
%body
= yield
1 change: 1 addition & 0 deletions lib/generators/haml/mailer/templates/layout.text.haml
@@ -0,0 +1 @@
= yield
4 changes: 4 additions & 0 deletions lib/generators/haml/mailer/templates/view.html.haml
@@ -0,0 +1,4 @@
%h1= class_name + "#" + @action

%p
= @greeting + ", find me in <%= @path %>"
19 changes: 19 additions & 0 deletions test/lib/generators/haml/mailer_generator_test.rb
Expand Up @@ -11,14 +11,33 @@ class Haml::Generators::MailerGeneratorTest < Rails::Generators::TestCase

test "should invoke template engine" do
run_generator

assert_file "app/views/layouts/mailer.text.haml" do |view|
assert_match /\= yield/, view
end

assert_file "app/views/layouts/mailer.html.haml" do |view|
assert_match /\= yield/, view
end

assert_file "app/views/notifier/foo.text.haml" do |view|
assert_match %r(app/views/notifier/foo\.text\.haml), view
assert_match /\= @greeting/, view
end

assert_file "app/views/notifier/foo.html.haml" do |view|
assert_match %r(app/views/notifier/foo\.html\.haml), view
assert_match /\= @greeting/, view
end

assert_file "app/views/notifier/bar.text.haml" do |view|
assert_match %r(app/views/notifier/bar\.text\.haml), view
assert_match /\= @greeting/, view
end

assert_file "app/views/notifier/bar.html.haml" do |view|
assert_match %r(app/views/notifier/bar\.html\.haml), view
assert_match /\= @greeting/, view
end
end
end

0 comments on commit 137882f

Please sign in to comment.