aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2013-12-08 13:38:01 -0800
committerDavid Heinemeier Hansson <david@loudthinking.com>2013-12-08 13:38:01 -0800
commit186161148a189839a1e0924043f068a8d155ce69 (patch)
tree405a3150a00e6517f338ef40b4e8a3a3d1170aca
parentcad9eb178ea5eec0e27d74e93518f4ed34e2f997 (diff)
parent2647d2f656ff203f45eecd7e19182018519d4064 (diff)
downloadrails-186161148a189839a1e0924043f068a8d155ce69.tar.gz
rails-186161148a189839a1e0924043f068a8d155ce69.tar.bz2
rails-186161148a189839a1e0924043f068a8d155ce69.zip
Merge pull request #13235 from strzalek/variants-inline
Inline syntax for variants
-rw-r--r--actionpack/lib/action_controller/metal/mime_responds.rb24
-rw-r--r--actionpack/test/controller/mime/respond_to_test.rb14
2 files changed, 28 insertions, 10 deletions
diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb
index b47abb8b8c..e762ea60a5 100644
--- a/actionpack/lib/action_controller/metal/mime_responds.rb
+++ b/actionpack/lib/action_controller/metal/mime_responds.rb
@@ -215,7 +215,7 @@ module ActionController #:nodoc:
raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
if collector = retrieve_collector_from_mimes(mimes, &block)
- response = collector.response(request.variant)
+ response = collector.response
response ? response.call : render({})
end
end
@@ -357,7 +357,7 @@ module ActionController #:nodoc:
if collector = retrieve_collector_from_mimes(&block)
options = resources.size == 1 ? {} : resources.extract_options!
options = options.clone
- options[:default_response] = collector.response(request.variant)
+ options[:default_response] = collector.response
(options.delete(:responder) || self.class.responder).call(self, resources, options)
end
end
@@ -390,7 +390,7 @@ module ActionController #:nodoc:
# is available.
def retrieve_collector_from_mimes(mimes=nil, &block) #:nodoc:
mimes ||= collect_mimes_from_class_level
- collector = Collector.new(mimes)
+ collector = Collector.new(mimes, request.variant)
block.call(collector) if block_given?
format = collector.negotiate_format(request)
@@ -428,9 +428,11 @@ module ActionController #:nodoc:
include AbstractController::Collector
attr_accessor :format
- def initialize(mimes)
+ def initialize(mimes, variant = nil)
@responses = {}
- mimes.each { |mime| send(mime) }
+ @variant = variant
+
+ mimes.each { |mime| @responses["Mime::#{mime.upcase}".constantize] = nil }
end
def any(*args, &block)
@@ -444,15 +446,19 @@ 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
+ VariantFilter.new(@variant)
+ end
end
- def response(variant)
+ def response
response = @responses.fetch(format, @responses[Mime::ALL])
- if response.nil? || response.arity == 0
+ if response.is_a?(VariantFilter) || response.nil? || response.arity == 0
response
else
- lambda { response.call VariantFilter.new(variant) }
+ lambda { response.call VariantFilter.new(@variant) }
end
end
diff --git a/actionpack/test/controller/mime/respond_to_test.rb b/actionpack/test/controller/mime/respond_to_test.rb
index c258bbec06..0bac86977a 100644
--- a/actionpack/test/controller/mime/respond_to_test.rb
+++ b/actionpack/test/controller/mime/respond_to_test.rb
@@ -175,6 +175,12 @@ class RespondToController < ActionController::Base
end
end
+ def variant_inline_syntax
+ respond_to do |format|
+ format.html.phone { render text: "phone" }
+ end
+ end
+
protected
def set_layout
case action_name
@@ -554,10 +560,16 @@ 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
+ @request.variant = :phone
+ get :variant_inline_syntax
+ assert_equal "text/html", @response.content_type
+ assert_equal "phone", @response.body
+ end
end