aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-03-21 19:30:42 +0100
committerJosé Valim <jose.valim@gmail.com>2010-03-21 19:30:42 +0100
commit8b50f89ba7076a62595d040f1512859c287c2d5c (patch)
treec94aad953c6b6a4eebcb09593abfa76219cf0d12 /railties
parent87db863fa212abd45be008c40ba1fb7ea26b6b60 (diff)
downloadrails-8b50f89ba7076a62595d040f1512859c287c2d5c.tar.gz
rails-8b50f89ba7076a62595d040f1512859c287c2d5c.tar.bz2
rails-8b50f89ba7076a62595d040f1512859c287c2d5c.zip
Make ERB generators more flexible and customizable.
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/generators/erb.rb13
-rw-r--r--railties/lib/generators/erb/controller/controller_generator.rb8
-rw-r--r--railties/lib/generators/erb/mailer/mailer_generator.rb17
-rw-r--r--railties/lib/generators/erb/scaffold/scaffold_generator.rb38
-rw-r--r--railties/test/generators/mailer_generator_test.rb8
5 files changed, 38 insertions, 46 deletions
diff --git a/railties/lib/generators/erb.rb b/railties/lib/generators/erb.rb
index d468d012dc..3e6371268f 100644
--- a/railties/lib/generators/erb.rb
+++ b/railties/lib/generators/erb.rb
@@ -3,6 +3,19 @@ require 'rails/generators/named_base'
module Erb
module Generators
class Base < Rails::Generators::NamedBase #:nodoc:
+ protected
+
+ def format
+ :html
+ end
+
+ def handler
+ :erb
+ end
+
+ def filename_with_extensions(name)
+ [name, format, handler].compact.join(".")
+ end
end
end
end
diff --git a/railties/lib/generators/erb/controller/controller_generator.rb b/railties/lib/generators/erb/controller/controller_generator.rb
index ab7b273662..a8bbcf401c 100644
--- a/railties/lib/generators/erb/controller/controller_generator.rb
+++ b/railties/lib/generators/erb/controller/controller_generator.rb
@@ -5,15 +5,13 @@ module Erb
class ControllerGenerator < Base
argument :actions, :type => :array, :default => [], :banner => "action action"
- def create_view_files
+ def copy_view_files
base_path = File.join("app/views", class_path, file_name)
empty_directory base_path
actions.each do |action|
- @action = action
- @path = File.join(base_path, "#{action}.html.erb")
-
- template 'view.html.erb', @path
+ @action, @path = action, File.join(base_path, action)
+ template filename_with_extensions(:view), filename_with_extensions(@path)
end
end
end
diff --git a/railties/lib/generators/erb/mailer/mailer_generator.rb b/railties/lib/generators/erb/mailer/mailer_generator.rb
index 408c942cef..65fa4a86c5 100644
--- a/railties/lib/generators/erb/mailer/mailer_generator.rb
+++ b/railties/lib/generators/erb/mailer/mailer_generator.rb
@@ -1,19 +1,12 @@
-require 'generators/erb'
+require 'generators/erb/controller/controller_generator'
module Erb
module Generators
- class MailerGenerator < Base
- argument :actions, :type => :array, :default => [], :banner => "method method"
+ class MailerGenerator < ControllerGenerator
+ protected
- def create_view_folder
- empty_directory File.join("app/views", file_path)
- end
-
- def create_view_files
- actions.each do |action|
- @action, @path = action, File.join(file_path, action)
- template "view.text.erb", File.join("app/views", "#{@path}.text.erb")
- end
+ def format
+ :text
end
end
end
diff --git a/railties/lib/generators/erb/scaffold/scaffold_generator.rb b/railties/lib/generators/erb/scaffold/scaffold_generator.rb
index 846540476f..267c9c8063 100644
--- a/railties/lib/generators/erb/scaffold/scaffold_generator.rb
+++ b/railties/lib/generators/erb/scaffold/scaffold_generator.rb
@@ -15,39 +15,27 @@ module Erb
empty_directory File.join("app/views", controller_file_path)
end
- def copy_index_file
- return if options[:singleton]
- copy_view :index
- end
-
- def copy_edit_file
- copy_view :edit
- end
+ def copy_view_files
+ views = available_views
+ views.delete("index") if options[:singleton]
- def copy_show_file
- copy_view :show
- end
-
- def copy_new_file
- copy_view :new
- end
-
- def copy_form_file
- copy_view :_form
+ views.each do |view|
+ filename = filename_with_extensions(view)
+ template filename, File.join("app/views", controller_file_path, filename)
+ end
end
def copy_layout_file
return unless options[:layout]
- template "layout.html.erb",
- File.join("app/views/layouts", controller_class_path, "#{controller_file_name}.html.erb")
+ template filename_with_extensions(:layout),
+ File.join("app/views/layouts", controller_class_path, filename_with_extensions(controller_file_name))
end
- protected
-
- def copy_view(view)
- template "#{view}.html.erb", File.join("app/views", controller_file_path, "#{view}.html.erb")
- end
+ protected
+ def available_views
+ %w(index edit show new _form)
+ end
end
end
end
diff --git a/railties/test/generators/mailer_generator_test.rb b/railties/test/generators/mailer_generator_test.rb
index e6fc1bbb5c..abbca5852b 100644
--- a/railties/test/generators/mailer_generator_test.rb
+++ b/railties/test/generators/mailer_generator_test.rb
@@ -29,19 +29,19 @@ class MailerGeneratorTest < Rails::Generators::TestCase
def test_invokes_default_test_framework
run_generator
assert_file "test/functional/notifier_test.rb", /class NotifierTest < ActionMailer::TestCase/
- assert_file "test/fixtures/notifier/foo", /app\/views\/notifier\/foo/
- assert_file "test/fixtures/notifier/bar", /app\/views\/notifier\/bar/
+ assert_file "test/fixtures/notifier/foo", /app\/views\/notifier\/foo$/
+ assert_file "test/fixtures/notifier/bar", /app\/views\/notifier\/bar$/
end
def test_invokes_default_template_engine
run_generator
assert_file "app/views/notifier/foo.text.erb" do |view|
- assert_match /app\/views\/notifier\/foo/, view
+ assert_match /app\/views\/notifier\/foo$/, view
assert_match /<%= @greeting %>/, view
end
assert_file "app/views/notifier/bar.text.erb" do |view|
- assert_match /app\/views\/notifier\/bar/, view
+ assert_match /app\/views\/notifier\/bar$/, view
assert_match /<%= @greeting %>/, view
end
end