diff options
author | Rick Olson <technoweenie@gmail.com> | 2006-12-07 15:36:08 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2006-12-07 15:36:08 +0000 |
commit | d0fa4d3e3336df858b8aa03e0c67087ba0ee37cb (patch) | |
tree | 301b83737c1157962e0f08c8f5993d5498af978b /actionpack/test | |
parent | e44f7fb07ac48f8211034bc7c78082ad40559dc9 (diff) | |
download | rails-d0fa4d3e3336df858b8aa03e0c67087ba0ee37cb.tar.gz rails-d0fa4d3e3336df858b8aa03e0c67087ba0ee37cb.tar.bz2 rails-d0fa4d3e3336df858b8aa03e0c67087ba0ee37cb.zip |
Add singleton resources. [Rick Olson]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5701 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/controller/resources_test.rb | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb index 32b554604d..c4fabc5f52 100644 --- a/actionpack/test/controller/resources_test.rb +++ b/actionpack/test/controller/resources_test.rb @@ -2,6 +2,7 @@ require File.dirname(__FILE__) + '/../abstract_unit' class ResourcesController < ActionController::Base def index() render :nothing => true end + alias_method :show, :index def rescue_action(e) raise e end end @@ -9,6 +10,8 @@ class ThreadsController < ResourcesController; end class MessagesController < ResourcesController; end class CommentsController < ResourcesController; end +class AccountController < ResourcesController; end +class AdminController < ResourcesController; end class ResourcesTest < Test::Unit::TestCase def test_should_arrange_actions @@ -172,6 +175,66 @@ class ResourcesTest < Test::Unit::TestCase end end + def test_should_create_singleton_resource_routes + with_singleton_resources :account do + assert_singleton_restful_for :account + end + end + + def test_should_create_multiple_singleton_resource_routes + with_singleton_resources :account, :admin do + assert_singleton_restful_for :account + assert_singleton_restful_for :admin + end + end + + def test_should_create_nested_singleton_resource_routes + with_routing do |set| + set.draw do |map| + map.resource :admin do |admin| + admin.resource :account + end + end + + assert_singleton_restful_for :admin + assert_singleton_restful_for :account, :path_prefix => 'admin/' + end + end + + def test_singleton_resource_with_member_action + [:put, :post].each do |method| + with_singleton_resources :account, :member => { :reset => method } do + reset_options = {:action => 'reset'} + reset_path = "/account;reset" + assert_singleton_routes_for :account do |options| + assert_recognizes(options.merge(reset_options), :path => reset_path, :method => method) + end + + assert_singleton_named_routes_for :account do |options| + assert_named_route reset_path, :reset_account_path, reset_options + end + end + end + end + + def test_singleton_resource_with_two_member_actions_with_same_method + [:put, :post].each do |method| + with_singleton_resources :account, :member => { :reset => method, :disable => method } do + %w(reset disable).each do |action| + action_options = {:action => action} + action_path = "/account;#{action}" + assert_singleton_routes_for :account do |options| + assert_recognizes(options.merge(action_options), :path => action_path, :method => method) + end + + assert_singleton_named_routes_for :account do |options| + assert_named_route action_path, "#{action}_account_path".to_sym, action_options + end + end + end + end + end + protected def with_restful_routing(*args) with_routing do |set| @@ -179,6 +242,13 @@ class ResourcesTest < Test::Unit::TestCase yield end end + + def with_singleton_resources(*args) + with_routing do |set| + set.draw { |map| map.resource(*args) } + yield + end + end # runs assert_restful_routes_for and assert_restful_named_routes for on the controller_name and options, without passing a block. def assert_simply_restful_for(controller_name, options = {}) @@ -186,6 +256,11 @@ class ResourcesTest < Test::Unit::TestCase assert_restful_named_routes_for controller_name, options end + def assert_singleton_restful_for(singleton_name, options = {}) + assert_singleton_routes_for singleton_name, options + assert_singleton_named_routes_for singleton_name, options + end + def assert_restful_routes_for(controller_name, options = {}) (options[:options] ||= {})[:controller] = controller_name.to_s @@ -242,6 +317,35 @@ class ResourcesTest < Test::Unit::TestCase yield options[:options] if block_given? end + def assert_singleton_routes_for(singleton_name, options = {}) + (options[:options] ||= {})[:controller] ||= singleton_name.to_s + + full_path = "/#{options[:path_prefix]}#{singleton_name}" + with_options options[:options] do |controller| + controller.assert_routing full_path, :action => 'show' + controller.assert_routing "#{full_path}.xml", :action => 'show', :format => 'xml' + end + + assert_recognizes(options[:options].merge(:action => 'update'), :path => full_path, :method => :put) + assert_recognizes(options[:options].merge(:action => 'destroy'), :path => full_path, :method => :delete) + + yield options[:options] if block_given? + end + + def assert_singleton_named_routes_for(singleton_name, options = {}) + (options[:options] ||= {})[:controller] ||= singleton_name.to_s + @controller = "#{options[:options][:controller].camelize}Controller".constantize.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + get :show, options[:options] + options[:options].delete :action + + full_path = "/#{options[:path_prefix]}#{singleton_name}" + + assert_named_route "#{full_path}", "#{singleton_name}_path", options[:options] + assert_named_route "#{full_path}.xml", "formatted_#{singleton_name}_path", options[:options].merge(:format => 'xml') + end + def assert_named_route(expected, route, options) actual = @controller.send(route, options) rescue $!.class.name assert_equal expected, actual, "Error on route: #{route}(#{options.inspect})" |