aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_controller/metal/mime_responds.rb26
1 files changed, 24 insertions, 2 deletions
diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb
index 3cb2ebeb94..dedff9f476 100644
--- a/actionpack/lib/action_controller/metal/mime_responds.rb
+++ b/actionpack/lib/action_controller/metal/mime_responds.rb
@@ -445,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) }
@@ -461,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