diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-03-27 16:20:52 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-03-27 16:20:52 -0300 |
commit | 5cb8e0046cf93d1e5468db8abb95a1f7b8a04b42 (patch) | |
tree | c2b3f5d97d469831d671aa60663de419508b4e0f /actionpack/lib/action_dispatch/http | |
parent | 72a02f98614261741d39432b239cded410f46b74 (diff) | |
parent | c64b99ecc98341d504aced72448bee758f3cfdaf (diff) | |
download | rails-5cb8e0046cf93d1e5468db8abb95a1f7b8a04b42.tar.gz rails-5cb8e0046cf93d1e5468db8abb95a1f7b8a04b42.tar.bz2 rails-5cb8e0046cf93d1e5468db8abb95a1f7b8a04b42.zip |
Merge pull request #18939 from georgeclaghorn/variant-inquiry
Provide friendlier access to request variants
Diffstat (limited to 'actionpack/lib/action_dispatch/http')
-rw-r--r-- | actionpack/lib/action_dispatch/http/mime_negotiation.rb | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/actionpack/lib/action_dispatch/http/mime_negotiation.rb b/actionpack/lib/action_dispatch/http/mime_negotiation.rb index 53a98c5d0a..ff336b7354 100644 --- a/actionpack/lib/action_dispatch/http/mime_negotiation.rb +++ b/actionpack/lib/action_dispatch/http/mime_negotiation.rb @@ -10,8 +10,6 @@ module ActionDispatch self.ignore_accept_header = false end - attr_reader :variant - # The MIME type of the HTTP request, such as Mime::XML. # # For backward compatibility, the post \format is extracted from the @@ -75,18 +73,22 @@ module ActionDispatch # Sets the \variant for template. def variant=(variant) - if variant.is_a?(Symbol) - @variant = [variant] - elsif variant.nil? || variant.is_a?(Array) && variant.any? && variant.all?{ |v| v.is_a?(Symbol) } - @variant = variant + variant = Array(variant) + + if variant.all? { |v| v.is_a?(Symbol) } + @variant = ActiveSupport::ArrayInquirer.new(variant) else - raise ArgumentError, "request.variant must be set to a Symbol or an Array of Symbols, not a #{variant.class}. " \ + raise ArgumentError, "request.variant must be set to a Symbol or an Array of Symbols. " \ "For security reasons, never directly set the variant to a user-provided value, " \ "like params[:variant].to_sym. Check user-provided value against a whitelist first, " \ "then set the variant: request.variant = :tablet if params[:variant] == 'tablet'" end end + def variant + @variant ||= ActiveSupport::ArrayInquirer.new + end + # Sets the \format by string extension, which can be used to force custom formats # that are not controlled by the extension. # |