diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2014-02-13 17:24:22 +0100 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2014-02-13 17:24:22 +0100 |
commit | f2dfa83fac5e892335d2bceaa6dfe344515fee26 (patch) | |
tree | 8e0f89eefe15f5bff61e111cbbc718114d9a0820 /actionpack/lib/action_controller/metal/mime_responds.rb | |
parent | ea3af7ee14217470b019225a03cccced1120f211 (diff) | |
parent | f9b6b865e60ea770cc34e9946f6df1604f20dd27 (diff) | |
download | rails-f2dfa83fac5e892335d2bceaa6dfe344515fee26.tar.gz rails-f2dfa83fac5e892335d2bceaa6dfe344515fee26.tar.bz2 rails-f2dfa83fac5e892335d2bceaa6dfe344515fee26.zip |
Merge pull request #14043 from strzalek/variants-negotiation
Variant negotiation
Diffstat (limited to 'actionpack/lib/action_controller/metal/mime_responds.rb')
-rw-r--r-- | actionpack/lib/action_controller/metal/mime_responds.rb | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb index d5e08b7034..c8076af0c8 100644 --- a/actionpack/lib/action_controller/metal/mime_responds.rb +++ b/actionpack/lib/action_controller/metal/mime_responds.rb @@ -236,6 +236,18 @@ module ActionController #:nodoc: # end # end # + # You can also set an array of variants: + # + # request.variant = [:tablet, :phone] + # + # which will work similarly to formats and MIME types negotiation. If there will be no + # :tablet variant declared, :phone variant will be picked: + # + # respond_to do |format| + # format.html.none + # format.html.phone # this gets rendered + # end + # # Be sure to check the documentation of +respond_with+ and # <tt>ActionController::MimeResponds.respond_to</tt> for more examples. def respond_to(*mimes, &block) @@ -488,7 +500,7 @@ module ActionController #:nodoc: response else # `format.html{ |variant| variant.phone }` - variant block syntax variant_collector = VariantCollector.new(@variant) - response.call(variant_collector) #call format block with variants collector + response.call(variant_collector) # call format block with variants collector variant_collector.variant end end @@ -519,15 +531,15 @@ module ActionController #:nodoc: end def variant - key = if @variant.nil? - :none - elsif @variants.has_key?(@variant) - @variant + if @variant.nil? + @variants[:none] + elsif (@variants.keys & @variant).any? + @variant.each do |v| + return @variants[v] if @variants.key?(v) + end else - :any + @variants[:any] end - - @variants[key] end end end |