mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 11:20:41 -07:00
Use to_liquid_value in uniq filter
This commit is contained in:
@@ -1081,7 +1081,10 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def uniq(&block)
|
def uniq(&block)
|
||||||
to_a.uniq(&block)
|
to_a.uniq do |item|
|
||||||
|
item = Utils.to_liquid_value(item)
|
||||||
|
block ? yield(item) : item
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def compact
|
def compact
|
||||||
|
|||||||
@@ -1340,6 +1340,22 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal(0, @filters.sum(input, ""))
|
assert_equal(0, @filters.sum(input, ""))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_uniq_with_to_liquid_value
|
||||||
|
input = [StringDrop.new("foo"), StringDrop.new("bar"), "foo"]
|
||||||
|
expected = [StringDrop.new("foo"), StringDrop.new("bar")]
|
||||||
|
result = @filters.uniq(input)
|
||||||
|
|
||||||
|
assert_equal(expected, result)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_uniq_with_to_liquid_value_pick_correct_classes
|
||||||
|
input = ["foo", StringDrop.new("foo"), StringDrop.new("bar")]
|
||||||
|
expected = [String, StringDrop]
|
||||||
|
result = @filters.uniq(input).map(&:class)
|
||||||
|
|
||||||
|
assert_equal(expected, result)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def with_timezone(tz)
|
def with_timezone(tz)
|
||||||
|
|||||||
@@ -146,6 +146,35 @@ class BooleanDrop < Liquid::Drop
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class StringDrop < Liquid::Drop
|
||||||
|
include Comparable
|
||||||
|
|
||||||
|
def initialize(value)
|
||||||
|
super()
|
||||||
|
@value = value
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_liquid_value
|
||||||
|
@value
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
@value
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_str
|
||||||
|
@value
|
||||||
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
"#<StringDrop @value=#{@value.inspect}>"
|
||||||
|
end
|
||||||
|
|
||||||
|
def <=>(other)
|
||||||
|
to_liquid_value <=> Liquid::Utils.to_liquid_value(other)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class ErrorDrop < Liquid::Drop
|
class ErrorDrop < Liquid::Drop
|
||||||
def standard_error
|
def standard_error
|
||||||
raise Liquid::StandardError, 'standard error'
|
raise Liquid::StandardError, 'standard error'
|
||||||
|
|||||||
Reference in New Issue
Block a user