mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make ERB generators more flexible and customizable.
This commit is contained in:
parent
87db863fa2
commit
8b50f89ba7
5 changed files with 38 additions and 46 deletions
|
@ -3,6 +3,19 @@ require 'rails/generators/named_base'
|
||||||
module Erb
|
module Erb
|
||||||
module Generators
|
module Generators
|
||||||
class Base < Rails::Generators::NamedBase #:nodoc:
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,15 +5,13 @@ module Erb
|
||||||
class ControllerGenerator < Base
|
class ControllerGenerator < Base
|
||||||
argument :actions, :type => :array, :default => [], :banner => "action action"
|
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)
|
base_path = File.join("app/views", class_path, file_name)
|
||||||
empty_directory base_path
|
empty_directory base_path
|
||||||
|
|
||||||
actions.each do |action|
|
actions.each do |action|
|
||||||
@action = action
|
@action, @path = action, File.join(base_path, action)
|
||||||
@path = File.join(base_path, "#{action}.html.erb")
|
template filename_with_extensions(:view), filename_with_extensions(@path)
|
||||||
|
|
||||||
template 'view.html.erb', @path
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,19 +1,12 @@
|
||||||
require 'generators/erb'
|
require 'generators/erb/controller/controller_generator'
|
||||||
|
|
||||||
module Erb
|
module Erb
|
||||||
module Generators
|
module Generators
|
||||||
class MailerGenerator < Base
|
class MailerGenerator < ControllerGenerator
|
||||||
argument :actions, :type => :array, :default => [], :banner => "method method"
|
protected
|
||||||
|
|
||||||
def create_view_folder
|
def format
|
||||||
empty_directory File.join("app/views", file_path)
|
:text
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,39 +15,27 @@ module Erb
|
||||||
empty_directory File.join("app/views", controller_file_path)
|
empty_directory File.join("app/views", controller_file_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_index_file
|
def copy_view_files
|
||||||
return if options[:singleton]
|
views = available_views
|
||||||
copy_view :index
|
views.delete("index") if options[:singleton]
|
||||||
end
|
|
||||||
|
|
||||||
def copy_edit_file
|
views.each do |view|
|
||||||
copy_view :edit
|
filename = filename_with_extensions(view)
|
||||||
end
|
template filename, File.join("app/views", controller_file_path, filename)
|
||||||
|
end
|
||||||
def copy_show_file
|
|
||||||
copy_view :show
|
|
||||||
end
|
|
||||||
|
|
||||||
def copy_new_file
|
|
||||||
copy_view :new
|
|
||||||
end
|
|
||||||
|
|
||||||
def copy_form_file
|
|
||||||
copy_view :_form
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_layout_file
|
def copy_layout_file
|
||||||
return unless options[:layout]
|
return unless options[:layout]
|
||||||
template "layout.html.erb",
|
template filename_with_extensions(:layout),
|
||||||
File.join("app/views/layouts", controller_class_path, "#{controller_file_name}.html.erb")
|
File.join("app/views/layouts", controller_class_path, filename_with_extensions(controller_file_name))
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def copy_view(view)
|
|
||||||
template "#{view}.html.erb", File.join("app/views", controller_file_path, "#{view}.html.erb")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
def available_views
|
||||||
|
%w(index edit show new _form)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -29,19 +29,19 @@ class MailerGeneratorTest < Rails::Generators::TestCase
|
||||||
def test_invokes_default_test_framework
|
def test_invokes_default_test_framework
|
||||||
run_generator
|
run_generator
|
||||||
assert_file "test/functional/notifier_test.rb", /class NotifierTest < ActionMailer::TestCase/
|
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/foo", /app\/views\/notifier\/foo$/
|
||||||
assert_file "test/fixtures/notifier/bar", /app\/views\/notifier\/bar/
|
assert_file "test/fixtures/notifier/bar", /app\/views\/notifier\/bar$/
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_invokes_default_template_engine
|
def test_invokes_default_template_engine
|
||||||
run_generator
|
run_generator
|
||||||
assert_file "app/views/notifier/foo.text.erb" do |view|
|
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
|
assert_match /<%= @greeting %>/, view
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_file "app/views/notifier/bar.text.erb" do |view|
|
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
|
assert_match /<%= @greeting %>/, view
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue