From ea8488caef77cb8cf2031d344e74981ab6ea0e57 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 7 Mar 2009 14:05:18 -0600 Subject: [PATCH] Fixed simplified render with nested models [#2042 state:resolved] --- actionpack/lib/action_controller/base.rb | 3 +++ actionpack/test/controller/fake_models.rb | 8 ++++++++ actionpack/test/controller/render_test.rb | 18 ++++++++++++++++++ .../fixtures/quiz/questions/_question.html.erb | 1 + actionpack/test/template/render_test.rb | 4 ++++ 5 files changed, 34 insertions(+) create mode 100644 actionpack/test/fixtures/quiz/questions/_question.html.erb diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 9d06d052b4c32..0facf7066d88e 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -907,6 +907,9 @@ def render(options = nil, extra_options = {}, &block) #:doc: extra_options[:template] = options end + options = extra_options + elsif !options.is_a?(Hash) + extra_options[:partial] = options options = extra_options end diff --git a/actionpack/test/controller/fake_models.rb b/actionpack/test/controller/fake_models.rb index 7420579ed8fdf..0b30c79b10c27 100644 --- a/actionpack/test/controller/fake_models.rb +++ b/actionpack/test/controller/fake_models.rb @@ -9,3 +9,11 @@ class BadCustomer < Customer class GoodCustomer < Customer end + +module Quiz + class Question < Struct.new(:name, :id) + def to_param + id.to_s + end + end +end diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 78a4e8ccbb56d..e0b3f64b76197 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -682,6 +682,14 @@ def partial_with_hash_object render :partial => "hash_object", :object => {:first_name => "Sam"} end + def partial_with_nested_object + render :partial => "quiz/questions/question", :object => Quiz::Question.new("first") + end + + def partial_with_nested_object_shorthand + render Quiz::Question.new("first") + end + def partial_hash_collection render :partial => "hash_object", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ] end @@ -1479,6 +1487,16 @@ def test_partial_with_hash_object assert_equal "Sam\nmaS\n", @response.body end + def test_partial_with_nested_object + get :partial_with_nested_object + assert_equal "first", @response.body + end + + def test_partial_with_nested_object_shorthand + get :partial_with_nested_object_shorthand + assert_equal "first", @response.body + end + def test_hash_partial_collection get :partial_hash_collection assert_equal "Pratik\nkitarP\nAmy\nymA\n", @response.body diff --git a/actionpack/test/fixtures/quiz/questions/_question.html.erb b/actionpack/test/fixtures/quiz/questions/_question.html.erb new file mode 100644 index 0000000000000..fb4dcfee6418c --- /dev/null +++ b/actionpack/test/fixtures/quiz/questions/_question.html.erb @@ -0,0 +1 @@ +<%= question.name %> \ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index b042eb3d9b957..9adf053b09322 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -145,6 +145,10 @@ def test_render_sub_template_with_errors assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name end + def test_render_object + assert_equal "Hello: david", @view.render(:partial => "test/customer", :object => Customer.new("david")) + end + def test_render_partial_collection assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ]) end