diff options
author | Łukasz Strzałkowski <lukasz.strzalkowski@gmail.com> | 2013-12-10 00:36:18 +0100 |
---|---|---|
committer | Łukasz Strzałkowski <lukasz.strzalkowski@gmail.com> | 2013-12-10 11:55:43 +0100 |
commit | edacdbfaf93ac1a81ec8654da4df03b80488e85c (patch) | |
tree | 8e357214a821e2a5a322fbd27f4f340218d4f738 /actionpack/test/controller/mime | |
parent | fbb6be50a55fef24cdef97c522d1acc9787cbf2a (diff) | |
download | rails-edacdbfaf93ac1a81ec8654da4df03b80488e85c.tar.gz rails-edacdbfaf93ac1a81ec8654da4df03b80488e85c.tar.bz2 rails-edacdbfaf93ac1a81ec8654da4df03b80488e85c.zip |
Inline variants syntax
In most cases, when setting variant specific code, you're not sharing any code
within format.
Inline syntax can vastly simplify defining variants in those situations:
respond_to do |format|
format.js { render "trash" }
format.html do |variant|
variant.phone { redirect_to progress_path }
variant.none { render "trash" }
end
end
Becomes:
respond_to do |format|
format.js { render "trash" }
format.html.phone { redirect_to progress_path }
format.html.none { render "trash" }
end
Diffstat (limited to 'actionpack/test/controller/mime')
-rw-r--r-- | actionpack/test/controller/mime/respond_to_test.rb | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/actionpack/test/controller/mime/respond_to_test.rb b/actionpack/test/controller/mime/respond_to_test.rb index c258bbec06..d84eb5790d 100644 --- a/actionpack/test/controller/mime/respond_to_test.rb +++ b/actionpack/test/controller/mime/respond_to_test.rb @@ -175,6 +175,22 @@ class RespondToController < ActionController::Base end end + def variant_inline_syntax + respond_to do |format| + format.js { render text: "js" } + format.html.none { render text: "none" } + format.html.phone { render text: "phone" } + end + end + + def variant_inline_syntax_without_block + respond_to do |format| + format.js + format.html.none + format.html.phone + end + end + protected def set_layout case action_name @@ -554,10 +570,31 @@ class RespondToControllerTest < ActionController::TestCase 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 + + def test_variant_inline_syntax + get :variant_inline_syntax, format: :js + assert_equal "text/javascript", @response.content_type + assert_equal "js", @response.body + + get :variant_inline_syntax + assert_equal "text/html", @response.content_type + assert_equal "none", @response.body + + @request.variant = :phone + get :variant_inline_syntax + assert_equal "text/html", @response.content_type + assert_equal "phone", @response.body + end + + def test_variant_inline_syntax_without_block + @request.variant = :phone + get :variant_inline_syntax_without_block + assert_equal "text/html", @response.content_type + assert_equal "phone", @response.body + end end |