diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2013-12-10 07:05:54 -0800 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2013-12-10 07:05:54 -0800 |
commit | ea3808771b59b7d6c1d397ab9f5d6701be4bb064 (patch) | |
tree | 1339dab4077d6fcc40070deae6d009edde7d71b5 /actionpack | |
parent | e74a9f9eea4fd2dee7fb2e65c95c713d11f4cdcc (diff) | |
parent | edacdbfaf93ac1a81ec8654da4df03b80488e85c (diff) | |
download | rails-ea3808771b59b7d6c1d397ab9f5d6701be4bb064.tar.gz rails-ea3808771b59b7d6c1d397ab9f5d6701be4bb064.tar.bz2 rails-ea3808771b59b7d6c1d397ab9f5d6701be4bb064.zip |
Merge pull request #13253 from strzalek/variants-inline2
Variants inline syntax v2
Diffstat (limited to 'actionpack')
3 files changed, 65 insertions, 4 deletions
diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb index b47abb8b8c..dedff9f476 100644 --- a/actionpack/lib/action_controller/metal/mime_responds.rb +++ b/actionpack/lib/action_controller/metal/mime_responds.rb @@ -430,7 +430,8 @@ module ActionController #:nodoc: def initialize(mimes) @responses = {} - mimes.each { |mime| send(mime) } + + mimes.each { |mime| @responses["Mime::#{mime.upcase}".constantize] = nil } end def any(*args, &block) @@ -444,12 +445,18 @@ module ActionController #:nodoc: def custom(mime_type, &block) mime_type = Mime::Type.lookup(mime_type.to_s) unless mime_type.is_a?(Mime::Type) - @responses[mime_type] ||= block + @responses[mime_type] ||= if block_given? + block + else + VariantCollector.new + end end def response(variant) response = @responses.fetch(format, @responses[Mime::ALL]) - if response.nil? || response.arity == 0 + if response.is_a?(VariantCollector) + response.variant(variant) + elsif response.nil? || response.arity == 0 response else lambda { response.call VariantFilter.new(variant) } @@ -460,6 +467,22 @@ module ActionController #:nodoc: @format = request.negotiate_mime(@responses.keys) end + #Used for inline syntax + class VariantCollector #:nodoc: + def initialize + @variants = {} + end + + def method_missing(name, *args, &block) + @variants[name] = block if block_given? + end + + def variant(name) + @variants[name.nil? ? :none : name] + end + end + + #Used for nested block syntax class VariantFilter #:nodoc: def initialize(variant) @variant = variant 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 diff --git a/actionpack/test/fixtures/respond_to/variant_inline_syntax_without_block.html+phone.erb b/actionpack/test/fixtures/respond_to/variant_inline_syntax_without_block.html+phone.erb new file mode 100644 index 0000000000..cd222a4a49 --- /dev/null +++ b/actionpack/test/fixtures/respond_to/variant_inline_syntax_without_block.html+phone.erb @@ -0,0 +1 @@ +phone
\ No newline at end of file |