1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added singleton support to resource controller.

This commit is contained in:
José Valim 2009-06-28 19:57:00 +02:00
parent 9413dba432
commit ff4c600448
2 changed files with 29 additions and 3 deletions

View file

@ -8,6 +8,9 @@ module Rails
class_option :actions, :type => :array, :default => [], :banner => "ACTION ACTION",
:desc => "Actions for the resource controller", :aliases => "-a"
class_option :singleton, :type => :boolean, :default => false, :aliases => "-i",
:desc => "Supply to create a singleton controller"
def invoke_for_resource_controller
return unless options[:resource_controller]
@ -15,7 +18,7 @@ module Rails
if klass
args = []
args << class_name.pluralize
args << pluralize?(class_name)
args << options[:actions]
say_status :invoke, options[:resource_controller], :blue
@ -25,11 +28,20 @@ module Rails
end
end
# TODO Add singleton support
def add_resource_route
route "map.resources :#{file_name.pluralize}"
route "map.resource#{"s" unless options[:singleton]} :#{pluralize?(file_name)}"
end
protected
def pluralize?(name)
if options[:singleton]
name
else
name.pluralize
end
end
end
end
end

View file

@ -78,6 +78,20 @@ class ResourceGeneratorTest < GeneratorsTestCase
end
end
def test_singleton_resource
run_generator ["account", "--singleton"]
assert_file "app/controllers/account_controller.rb", /class AccountController < ApplicationController/
assert_file "test/functional/account_controller_test.rb", /class AccountControllerTest < ActionController::TestCase/
assert_file "app/helpers/account_helper.rb", /module AccountHelper/
assert_file "test/unit/helpers/account_helper_test.rb", /class AccountHelperTest < ActionView::TestCase/
assert_file "config/routes.rb" do |route|
assert_match /map\.resource :account$/, route
end
end
protected
def run_generator(args=["account"])