diff options
author | José Valim <jose.valim@gmail.com> | 2009-06-25 12:57:58 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-06-25 12:57:58 +0200 |
commit | 6e6c0117b30226f031771acb9715318987067f51 (patch) | |
tree | 0561577a33b94e0245ee88ef56988eb8b638b4a1 | |
parent | ed33c29a4e2a07c2a738ec13775c5cc0d7867b50 (diff) | |
download | rails-6e6c0117b30226f031771acb9715318987067f51.tar.gz rails-6e6c0117b30226f031771acb9715318987067f51.tar.bz2 rails-6e6c0117b30226f031771acb9715318987067f51.zip |
Added Mailer generaator template engine and test framework agnostic.
-rw-r--r-- | railties/lib/generators.rb | 1 | ||||
-rw-r--r-- | railties/lib/generators/base.rb | 19 | ||||
-rw-r--r-- | railties/lib/generators/erb.rb | 8 | ||||
-rw-r--r-- | railties/lib/generators/erb/mailer/mailer_generator.rb | 19 | ||||
-rw-r--r-- | railties/lib/generators/erb/mailer/templates/view.erb | 3 | ||||
-rw-r--r-- | railties/lib/generators/rails/mailer/USAGE | 15 | ||||
-rw-r--r-- | railties/lib/generators/rails/mailer/mailer_generator.rb | 18 | ||||
-rw-r--r-- | railties/lib/generators/rails/mailer/templates/mailer.rb | 15 | ||||
-rw-r--r-- | railties/lib/generators/test_unit/mailer/mailer_generator.rb | 23 | ||||
-rw-r--r-- | railties/lib/generators/test_unit/mailer/templates/fixture | 3 | ||||
-rw-r--r-- | railties/lib/generators/test_unit/mailer/templates/unit_test.rb | 20 |
11 files changed, 144 insertions, 0 deletions
diff --git a/railties/lib/generators.rb b/railties/lib/generators.rb index 4743409f0c..3ed1bd4b17 100644 --- a/railties/lib/generators.rb +++ b/railties/lib/generators.rb @@ -12,6 +12,7 @@ require 'rails/version' unless defined?(Rails::VERSION) require 'generators/base' require 'generators/named_base' +require 'generators/erb' require 'generators/test_unit' module Rails diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb index d4bce83161..e009998bfa 100644 --- a/railties/lib/generators/base.rb +++ b/railties/lib/generators/base.rb @@ -119,6 +119,25 @@ module Rails end end end + + # Small macro to add template engine option and invoke it. + # + def self.add_and_invoke_template_engine_option! + class_option :template_engine, :type => :string, :aliases => "-e", :default => "erb", + :desc => "Template engine to be invoked by this generator", :banner => "NAME" + + define_method :invoke_template_engine do + return unless options[:template_engine] + name = "#{options[:template_engine]}:generators:#{self.class.generator_name}" + + begin + invoke name + rescue Thor::UndefinedTaskError + say "Could not find and invoke '#{name}'." + end + end + end + end end end diff --git a/railties/lib/generators/erb.rb b/railties/lib/generators/erb.rb new file mode 100644 index 0000000000..da99cec895 --- /dev/null +++ b/railties/lib/generators/erb.rb @@ -0,0 +1,8 @@ +require 'generators/named_base' + +module Erb + module Generators + class Base < Rails::Generators::NamedBase + end + end +end diff --git a/railties/lib/generators/erb/mailer/mailer_generator.rb b/railties/lib/generators/erb/mailer/mailer_generator.rb new file mode 100644 index 0000000000..2458762a9d --- /dev/null +++ b/railties/lib/generators/erb/mailer/mailer_generator.rb @@ -0,0 +1,19 @@ +module Erb + module Generators + class MailerGenerator < Base + argument :actions, :type => :array, :default => [] + + desc <<DESC +Description: + Create Template engine files for mailer generator. +DESC + + def create_view_files + actions.each do |action| + @action, @path = action, File.join(file_path, action) + template "view.erb", File.join("app", "views", "#{@path}.erb") + end + end + end + end +end diff --git a/railties/lib/generators/erb/mailer/templates/view.erb b/railties/lib/generators/erb/mailer/templates/view.erb new file mode 100644 index 0000000000..cda2cd24fe --- /dev/null +++ b/railties/lib/generators/erb/mailer/templates/view.erb @@ -0,0 +1,3 @@ +<%= class_name %>#<%= @action %> + +Find me in <%= @path %> diff --git a/railties/lib/generators/rails/mailer/USAGE b/railties/lib/generators/rails/mailer/USAGE new file mode 100644 index 0000000000..638934d38d --- /dev/null +++ b/railties/lib/generators/rails/mailer/USAGE @@ -0,0 +1,15 @@ +Description: + Stubs out a new mailer and its views. Pass the mailer name, either + CamelCased or under_scored, and an optional list of emails as arguments. + + This generates a mailer class in app/models, view templates in + app/views/mailer_name and then invokes your test framework. + +Example: + `./script/generate mailer Notifications signup forgot_password invoice` + + creates a Notifications mailer class, views, test, and fixtures: + Mailer: app/models/notifications.rb + Views: app/views/notifications/signup.erb [...] + Test: test/unit/test/unit/notifications_test.rb + Fixtures: test/fixtures/notifications/signup [...] diff --git a/railties/lib/generators/rails/mailer/mailer_generator.rb b/railties/lib/generators/rails/mailer/mailer_generator.rb new file mode 100644 index 0000000000..4c77df89ab --- /dev/null +++ b/railties/lib/generators/rails/mailer/mailer_generator.rb @@ -0,0 +1,18 @@ +module Rails + module Generators + class MailerGenerator < NamedBase + argument :actions, :type => :array, :default => [] + + def check_class_collision + class_collisions class_name + end + + def create_mailer_file + template "mailer.rb", File.join('app/models', class_path, "#{file_name}.rb") + end + + add_and_invoke_template_engine_option! + add_and_invoke_test_framework_option! + end + end +end diff --git a/railties/lib/generators/rails/mailer/templates/mailer.rb b/railties/lib/generators/rails/mailer/templates/mailer.rb new file mode 100644 index 0000000000..ce15ae9de9 --- /dev/null +++ b/railties/lib/generators/rails/mailer/templates/mailer.rb @@ -0,0 +1,15 @@ +class <%= class_name %> < ActionMailer::Base + +<% for action in actions -%> + + def <%= action %>(sent_at = Time.now) + subject '<%= class_name %>#<%= action %>' + recipients '' + from '' + sent_on sent_at + + body :greeting => 'Hi,' + end +<% end -%> + +end diff --git a/railties/lib/generators/test_unit/mailer/mailer_generator.rb b/railties/lib/generators/test_unit/mailer/mailer_generator.rb new file mode 100644 index 0000000000..5fa7751076 --- /dev/null +++ b/railties/lib/generators/test_unit/mailer/mailer_generator.rb @@ -0,0 +1,23 @@ +module TestUnit + module Generators + class MailerGenerator < Base + argument :actions, :type => :array, :default => [] + + desc <<DESC +Description: + Create TestUnit files for mailer generator. +DESC + + def create_test_files + template "unit_test.rb", File.join('test', 'unit', class_path, "#{file_name}_test.rb") + end + + def create_fixtures_files + actions.each do |action| + @action, @path = action, File.join(file_path, action) + template "fixture", File.join("test", "fixtures", @path) + end + end + end + end +end diff --git a/railties/lib/generators/test_unit/mailer/templates/fixture b/railties/lib/generators/test_unit/mailer/templates/fixture new file mode 100644 index 0000000000..cda2cd24fe --- /dev/null +++ b/railties/lib/generators/test_unit/mailer/templates/fixture @@ -0,0 +1,3 @@ +<%= class_name %>#<%= @action %> + +Find me in <%= @path %> diff --git a/railties/lib/generators/test_unit/mailer/templates/unit_test.rb b/railties/lib/generators/test_unit/mailer/templates/unit_test.rb new file mode 100644 index 0000000000..4de94076e9 --- /dev/null +++ b/railties/lib/generators/test_unit/mailer/templates/unit_test.rb @@ -0,0 +1,20 @@ +require 'test_helper' + +class <%= class_name %>Test < ActionMailer::TestCase +<% for action in actions -%> + test "<%= action %>" do + @expected.subject = '<%= class_name %>#<%= action %>' + @expected.body = read_fixture('<%= action %>') + @expected.date = Time.now + + assert_equal @expected.encoded, <%= class_name %>.create_<%= action %>(@expected.date).encoded + end + +<% end -%> +<% if actions.blank? -%> + # replace this with your real tests + test "the truth" do + assert true + end +<% end -%> +end |