From 5dd3db86157ce2bd08c4ec07826d3aaf5c29f458 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 5 Jun 2007 19:10:59 +0000 Subject: Resources: url_for([parent, child]) generates /parents/1/children/2 for the nested resource. Likewise with the other simply helpful methods like form_for and link_to. Closes #6432. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6951 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/test/template/form_helper_test.rb | 57 ++++++++++++++++++++++-- actionpack/test/template/url_helper_test.rb | 65 ++++++++++++++++++++++++++-- 2 files changed, 115 insertions(+), 7 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index c9e7aeaeb3..c504190960 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -15,8 +15,21 @@ silence_warnings do @new_record end end + + class Comment + attr_reader :id + attr_reader :post_id + def save; @id = 1; @post_id = 1 end + def new_record?; @id.nil? end + def name + @id.nil? ? 'new comment' : "comment ##{@id}" + end + end end +class Comment::Nested < Comment; end + + class FormHelperTest < Test::Unit::TestCase include ActionView::Helpers::FormHelper include ActionView::Helpers::FormTagHelper @@ -28,6 +41,7 @@ class FormHelperTest < Test::Unit::TestCase def setup @post = Post.new + @comment = Comment.new def @post.errors() Class.new{ def on(field); "can't be empty" if field == "author_name"; end @@ -579,6 +593,25 @@ class FormHelperTest < Test::Unit::TestCase assert_equal expected, _erbout end + def test_form_for_with_existing_object_in_list + @post.new_record = false + @comment.save + _erbout = '' + form_for([@post, @comment]) {} + + expected = %(
) + assert_dom_equal expected, _erbout + end + + def test_form_for_with_new_object_in_list + @post.new_record = false + _erbout = '' + form_for([@post, @comment]) {} + + expected = %(
) + assert_dom_equal expected, _erbout + end + def test_form_for_with_existing_object_and_custom_url _erbout = '' @@ -600,11 +633,27 @@ class FormHelperTest < Test::Unit::TestCase protected - def polymorphic_path(record) - if record.new_record? - "/posts" + def comments_path(post) + "/posts/#{post.id}/comments" + end + + def comment_path(post, comment) + "/posts/#{post.id}/comments/#{comment.id}" + end + + def polymorphic_path(object, *nested_objects) + if nested_objects.empty? + if object.new_record? + "/posts" + else + "/posts/#{object.id}" + end else - "/posts/#{record.id}" + if object.new_record? + "/posts/123/comments" + else + "/posts/123/comments/#{object.id}" + end end end end diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 43ec8ca0d3..1a5521b3e4 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -375,6 +375,22 @@ class Workshop end end +class Session + attr_accessor :id, :workshop_id, :new_record + + def initialize(id, new_record) + @id, @new_record = id, new_record + end + + def new_record? + @new_record + end + + def to_s + id.to_s + end +end + class PolymorphicControllerTest < Test::Unit::TestCase class WorkshopsController < ActionController::Base self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"] @@ -394,15 +410,36 @@ class PolymorphicControllerTest < Test::Unit::TestCase def rescue_action(e) raise e end end + class SessionsController < ActionController::Base + self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"] + + def self.controller_path; 'sessions' end + + def index + @workshop = Workshop.new(params[:workshop_id], false) + @session = Session.new(1, true) + render :inline => "<%= url_for([@workshop, @session]) %>\n<%= link_to('Session', [@workshop, @session]) %>" + end + + def show + @workshop = Workshop.new(params[:workshop_id], false) + @session = Session.new(params[:id], false) + render :inline => "<%= url_for([@workshop, @session]) %>\n<%= link_to('Session', [@workshop, @session]) %>" + end + + def rescue_action(e) raise e end + end + include ActionView::Helpers::UrlHelper def setup @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new - @controller = WorkshopsController.new end def test_new_resource + @controller = WorkshopsController.new + with_restful_routing do get :index assert_equal "/workshops\nWorkshop", @response.body @@ -410,19 +447,41 @@ class PolymorphicControllerTest < Test::Unit::TestCase end def test_existing_resource + @controller = WorkshopsController.new + with_restful_routing do get :show, :id => 1 assert_equal "/workshops/1\nWorkshop", @response.body end end + def test_new_nested_resource + @controller = SessionsController.new + + with_restful_routing do + get :index, :workshop_id => 1 + assert_equal "/workshops/1/sessions\nSession", @response.body + end + end + + def test_existing_nested_resource + @controller = SessionsController.new + + with_restful_routing do + get :show, :workshop_id => 1, :id => 1 + assert_equal "/workshops/1/sessions/1\nSession", @response.body + end + end + protected def with_restful_routing with_routing do |set| set.draw do |map| - map.resources :workshops + map.resources :workshops do |w| + w.resources :sessions + end end yield end end -end \ No newline at end of file +end -- cgit v1.2.3