mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Added Controller Generators.
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
module Erb
|
||||
module Generators
|
||||
class ControllerGenerator < Base
|
||||
argument :actions, :type => :array, :default => [], :banner => "action action"
|
||||
|
||||
def create_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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,2 @@
|
||||
<h1><%= class_name %>#<%= @action %></h1>
|
||||
<p>Find me in <%= @path %></p>
|
||||
@@ -30,7 +30,7 @@ module Rails
|
||||
# superclass. The from_superclass method used below is from Thor.
|
||||
#
|
||||
def class_collisions #:nodoc:
|
||||
@class_collisions ||= from_superclass(:class_collisions, nil)
|
||||
@class_collisions ||= from_superclass(:class_collisions, nil) rescue nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
30
railties/lib/generators/rails/controller/USAGE
Normal file
30
railties/lib/generators/rails/controller/USAGE
Normal file
@@ -0,0 +1,30 @@
|
||||
Description:
|
||||
Stubs out a new controller and its views. Pass the controller name, either
|
||||
CamelCased or under_scored, and a list of views as arguments.
|
||||
|
||||
To create a controller within a module, specify the controller name as a
|
||||
path like 'parent_module/controller_name'.
|
||||
|
||||
This generates a controller class in app/controllers, view templates in
|
||||
app/views/controller_name and then invokes the helper generator, the
|
||||
current template engine and finally the test framework.
|
||||
|
||||
Example:
|
||||
`./script/generate controller CreditCard open debit credit close`
|
||||
|
||||
Credit card controller with URLs like /credit_card/debit.
|
||||
Controller: app/controllers/credit_card_controller.rb
|
||||
Functional Test: test/functional/credit_card_controller_test.rb
|
||||
Views: app/views/credit_card/debit.html.erb [...]
|
||||
Helper: app/helpers/credit_card_helper.rb
|
||||
Helper Test: test/unit/helpers/credit_card_helper_test.rb
|
||||
|
||||
Modules Example:
|
||||
`./script/generate controller 'admin/credit_card' suspend late_fee`
|
||||
|
||||
Credit card admin controller with URLs /admin/credit_card/suspend.
|
||||
Controller: app/controllers/admin/credit_card_controller.rb
|
||||
Functional Test: test/functional/admin/credit_card_controller_test.rb
|
||||
Views: app/views/admin/credit_card/debit.html.erb [...]
|
||||
Helper: app/helpers/admin/credit_card_helper.rb
|
||||
Helper Test: test/unit/helpers/admin/credit_card_helper_test.rb
|
||||
@@ -0,0 +1,18 @@
|
||||
module Rails
|
||||
module Generators
|
||||
class ControllerGenerator < NamedBase
|
||||
argument :actions, :type => :array, :default => [], :banner => "action action"
|
||||
check_class_collision :suffix => "Controller"
|
||||
|
||||
def create_controller_files
|
||||
template 'controller.rb', File.join('app/controllers', class_path, "#{file_name}_controller.rb")
|
||||
end
|
||||
|
||||
invoke_for :template_engine, :test_framework
|
||||
|
||||
def invoke_helper
|
||||
invoke "rails:generators:helper"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,7 @@
|
||||
class <%= class_name %>Controller < ApplicationController
|
||||
<% for action in actions -%>
|
||||
def <%= action %>
|
||||
end
|
||||
|
||||
<% end -%>
|
||||
end
|
||||
@@ -0,0 +1,11 @@
|
||||
module TestUnit
|
||||
module Generators
|
||||
class ControllerGenerator < Base
|
||||
check_class_collision :suffix => "ControllerTest"
|
||||
|
||||
def create_test_files
|
||||
template 'functional_test.rb', File.join('test/functional', class_path, "#{file_name}_controller_test.rb")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
require 'test_helper'
|
||||
|
||||
class <%= class_name %>ControllerTest < ActionController::TestCase
|
||||
# Replace this with your real tests.
|
||||
test "the truth" do
|
||||
assert true
|
||||
end
|
||||
end
|
||||
62
railties/test/generators/controller_generator_test.rb
Normal file
62
railties/test/generators/controller_generator_test.rb
Normal file
@@ -0,0 +1,62 @@
|
||||
require 'abstract_unit'
|
||||
require 'generators/generators_test_helper'
|
||||
require 'generators/erb/controller/controller_generator'
|
||||
require 'generators/rails/controller/controller_generator'
|
||||
require 'generators/rails/helper/helper_generator'
|
||||
require 'generators/test_unit/controller/controller_generator'
|
||||
require 'generators/test_unit/helper/helper_generator'
|
||||
|
||||
ObjectController = Class.new
|
||||
|
||||
class ControllerGeneratorTest < GeneratorsTestCase
|
||||
|
||||
def test_controller_skeleton_is_created
|
||||
run_generator
|
||||
assert_file "app/controllers/account_controller.rb", /class AccountController < ApplicationController/
|
||||
end
|
||||
|
||||
def test_check_class_collision
|
||||
content = capture(:stderr){ run_generator ["object"] }
|
||||
assert_match /The name 'ObjectController' is either already used in your application or reserved/, content
|
||||
end
|
||||
|
||||
def test_invokes_helpers
|
||||
run_generator
|
||||
assert_file "app/helpers/account_helper.rb"
|
||||
assert_file "test/unit/helpers/account_helper_test.rb"
|
||||
end
|
||||
|
||||
def test_invokes_default_test_framework
|
||||
run_generator
|
||||
assert_file "test/functional/account_controller_test.rb"
|
||||
end
|
||||
|
||||
def test_invokes_default_template_engine
|
||||
run_generator
|
||||
assert_file "app/views/account/foo.html.erb", /app\/views\/account\/foo/
|
||||
assert_file "app/views/account/bar.html.erb", /app\/views\/account\/bar/
|
||||
end
|
||||
|
||||
def test_invokes_default_template_engine_even_with_no_action
|
||||
run_generator ["account"]
|
||||
assert_file "app/views/account"
|
||||
end
|
||||
|
||||
def test_template_engine_with_class_path
|
||||
run_generator ["admin/account"]
|
||||
assert_file "app/views/admin/account"
|
||||
end
|
||||
|
||||
def test_actions_are_turned_into_methods
|
||||
run_generator
|
||||
assert_file "app/controllers/account_controller.rb", /def foo/
|
||||
assert_file "app/controllers/account_controller.rb", /def bar/
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def run_generator(args=["Account", "foo", "bar"])
|
||||
silence(:stdout) { Rails::Generators::ControllerGenerator.start args, :root => destination_root }
|
||||
end
|
||||
|
||||
end
|
||||
@@ -25,8 +25,8 @@ class MailerGeneratorTest < GeneratorsTestCase
|
||||
|
||||
def test_invokes_default_template_engine
|
||||
run_generator
|
||||
assert_file "app/views/notifier/foo.erb"
|
||||
assert_file "app/views/notifier/bar.erb"
|
||||
assert_file "app/views/notifier/foo.erb", /app\/views\/notifier\/foo/
|
||||
assert_file "app/views/notifier/bar.erb", /app\/views\/notifier\/bar/
|
||||
end
|
||||
|
||||
def test_invokes_default_template_engine_even_with_no_action
|
||||
|
||||
Reference in New Issue
Block a user