Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple binary operators in where_exp filter #8047

Merged
merged 1 commit into from Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/jekyll/filters.rb
Expand Up @@ -432,10 +432,14 @@ def parse_condition(exp)
#
# Returns an instance of Liquid::Condition
def parse_binary_comparison(parser)
parse_comparison(parser).tap do |condition|
binary_operator = parser.id?("and") || parser.id?("or")
condition.send(binary_operator, parse_comparison(parser)) if binary_operator
condition = parse_comparison(parser)
first_condition = condition
while (binary_operator = parser.id?("and") || parser.id?("or"))
child_condition = parse_comparison(parser)
condition.send(binary_operator, child_condition)
condition = child_condition
end
first_condition
end

# Generates a Liquid::Condition object from a Liquid::Parser object based on whether the parsed
Expand Down
17 changes: 17 additions & 0 deletions test/test_filters.rb
Expand Up @@ -997,6 +997,23 @@ def to_liquid
)
end

should "filter objects across multiple conditions" do
sample = [
{ "color" => "teal", "size" => "large", "type" => "variable" },
{ "color" => "red", "size" => "large", "type" => "fixed" },
{ "color" => "red", "size" => "medium", "type" => "variable" },
{ "color" => "blue", "size" => "medium", "type" => "fixed" },
]
assert_equal(
[
{ "color" => "red", "size" => "large", "type" => "fixed" },
],
@filter.where_exp(
sample, "item", "item.type == 'fixed' and item.color == 'red' or item.color == 'teal'"
)
)
end

should "stringify during comparison for compatibility with liquid parsing" do
hash = {
"The Words" => { "rating" => 1.2, "featured" => false },
Expand Down