mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
allow reseting of request variants
The current implementation of `variants=` don't allow a resetting to nil, wich is the default value. This results in the following code smell: ```ruby case request.user_agent when /iPhone/ request.variants = :phone when /iPad/ request.variants = :ipad end ``` With the ability to reset variants to nil, it could be: ```ruby request.variants = case request.user_agent when /iPhone/ :phone when /iPad/ :ipad end ```
This commit is contained in:
parent
41dc7fd650
commit
e1fb3483d6
2 changed files with 9 additions and 1 deletions
|
@ -72,11 +72,12 @@ module ActionDispatch
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Sets the \variant for template.
|
||||
def variant=(variant)
|
||||
if variant.is_a?(Symbol)
|
||||
@variant = [variant]
|
||||
elsif variant.is_a?(Array) && variant.any? && variant.all?{ |v| v.is_a?(Symbol) }
|
||||
elsif variant.nil? || variant.is_a?(Array) && variant.any? && variant.all?{ |v| v.is_a?(Symbol) }
|
||||
@variant = variant
|
||||
else
|
||||
raise ArgumentError, "request.variant must be set to a Symbol or an Array of Symbols, not a #{variant.class}. " \
|
||||
|
|
|
@ -1143,6 +1143,13 @@ class RequestVariant < BaseRequestTest
|
|||
end
|
||||
end
|
||||
|
||||
test "reset variant" do
|
||||
request = stub_request
|
||||
|
||||
request.variant = nil
|
||||
assert_equal nil, request.variant
|
||||
end
|
||||
|
||||
test "setting variant with non symbol value" do
|
||||
request = stub_request
|
||||
assert_raise ArgumentError do
|
||||
|
|
Loading…
Reference in a new issue