From a16fa9abfd0b34026b04f4ceeb0b75e63609a74a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 7 Dec 2013 15:00:35 -0800 Subject: Allow code execution in case no variant has been set with variant.none --- actionpack/lib/action_controller/metal/mime_responds.rb | 9 +++++---- actionpack/test/controller/mime/respond_to_test.rb | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb index 54d3be68f0..4993583c29 100644 --- a/actionpack/lib/action_controller/metal/mime_responds.rb +++ b/actionpack/lib/action_controller/metal/mime_responds.rb @@ -196,9 +196,10 @@ module ActionController #:nodoc: # Respond to variants in the action just like you respond to formats: # # respond_to do |format| - # format.html do |html| - # html.tablet # renders app/views/projects/show.html+tablet.erb - # html.phone { extra_setup; render ... } + # format.html do |variant| + # variant.tablet # renders app/views/projects/show.html+tablet.erb + # variant.phone { extra_setup; render ... } + # variant.none { special_setup } # executed only if there is no variant set # end # end # @@ -465,7 +466,7 @@ module ActionController #:nodoc: end def method_missing(name) - yield if name == @variant + yield if name == @variant || (name == :none && @variant.nil?) end end end diff --git a/actionpack/test/controller/mime/respond_to_test.rb b/actionpack/test/controller/mime/respond_to_test.rb index 2b6c8739af..cd93043360 100644 --- a/actionpack/test/controller/mime/respond_to_test.rb +++ b/actionpack/test/controller/mime/respond_to_test.rb @@ -166,6 +166,15 @@ class RespondToController < ActionController::Base end end + def variant_plus_none_for_format + respond_to do |format| + format.html do |variant| + variant.phone { render text: "phone" } + variant.none { render text: "none" } + end + end + end + protected def set_layout case action_name @@ -544,4 +553,11 @@ class RespondToControllerTest < ActionController::TestCase assert_equal "text/html", @response.content_type assert_equal "tablet", @response.body end + + + def test_no_variant_in_variant_setup + get :variant_plus_none_for_format + assert_equal "text/html", @response.content_type + assert_equal "none", @response.body + end end -- cgit v1.2.3