diff options
Diffstat (limited to 'railties')
10 files changed, 160 insertions, 3 deletions
diff --git a/railties/lib/generators/erb/controller/controller_generator.rb b/railties/lib/generators/erb/controller/controller_generator.rb new file mode 100644 index 0000000000..171ec23b97 --- /dev/null +++ b/railties/lib/generators/erb/controller/controller_generator.rb @@ -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 diff --git a/railties/lib/generators/erb/controller/templates/view.html.erb b/railties/lib/generators/erb/controller/templates/view.html.erb new file mode 100644 index 0000000000..cd54d13d83 --- /dev/null +++ b/railties/lib/generators/erb/controller/templates/view.html.erb @@ -0,0 +1,2 @@ +<h1><%= class_name %>#<%= @action %></h1> +<p>Find me in <%= @path %></p> diff --git a/railties/lib/generators/named_base.rb b/railties/lib/generators/named_base.rb index f29916b5f6..6ff5bb7644 100644 --- a/railties/lib/generators/named_base.rb +++ b/railties/lib/generators/named_base.rb @@ -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 diff --git a/railties/lib/generators/rails/controller/USAGE b/railties/lib/generators/rails/controller/USAGE new file mode 100644 index 0000000000..42d38bd745 --- /dev/null +++ b/railties/lib/generators/rails/controller/USAGE @@ -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 diff --git a/railties/lib/generators/rails/controller/controller_generator.rb b/railties/lib/generators/rails/controller/controller_generator.rb new file mode 100644 index 0000000000..b5d875eb38 --- /dev/null +++ b/railties/lib/generators/rails/controller/controller_generator.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 diff --git a/railties/lib/generators/rails/controller/templates/controller.rb b/railties/lib/generators/rails/controller/templates/controller.rb new file mode 100644 index 0000000000..cda2659e69 --- /dev/null +++ b/railties/lib/generators/rails/controller/templates/controller.rb @@ -0,0 +1,7 @@ +class <%= class_name %>Controller < ApplicationController +<% for action in actions -%> + def <%= action %> + end + +<% end -%> +end diff --git a/railties/lib/generators/test_unit/controller/controller_generator.rb b/railties/lib/generators/test_unit/controller/controller_generator.rb new file mode 100644 index 0000000000..d0d12d79e9 --- /dev/null +++ b/railties/lib/generators/test_unit/controller/controller_generator.rb @@ -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 diff --git a/railties/lib/generators/test_unit/controller/templates/functional_test.rb b/railties/lib/generators/test_unit/controller/templates/functional_test.rb new file mode 100644 index 0000000000..62fa5d86fd --- /dev/null +++ b/railties/lib/generators/test_unit/controller/templates/functional_test.rb @@ -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 diff --git a/railties/test/generators/controller_generator_test.rb b/railties/test/generators/controller_generator_test.rb new file mode 100644 index 0000000000..88b54a54ed --- /dev/null +++ b/railties/test/generators/controller_generator_test.rb @@ -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 diff --git a/railties/test/generators/mailer_generator_test.rb b/railties/test/generators/mailer_generator_test.rb index d702379822..710847587d 100644 --- a/railties/test/generators/mailer_generator_test.rb +++ b/railties/test/generators/mailer_generator_test.rb @@ -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 |