From 64ad293b899a66d6357cb320846b0804ae0d4518 Mon Sep 17 00:00:00 2001 From: Tim Wisniewski Date: Sat, 20 Feb 2016 19:46:48 -0500 Subject: [PATCH 1/4] add array support to where filter --- lib/jekyll/filters.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 986bfdee..55e91ee8 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -222,7 +222,7 @@ module Jekyll def where(input, property, value) return input unless input.is_a?(Enumerable) input = input.values if input.is_a?(Hash) - input.select { |object| item_property(object, property).to_s == value.to_s } + input.select { |object| item_property(object, property).to_s == value.to_s or item_property(object, property).include? value.to_s } end # Sort an array of objects From 6245ddb14d0291ff7b581a937e804f871f792c30 Mon Sep 17 00:00:00 2001 From: timwis Date: Thu, 25 Feb 2016 10:18:03 -0500 Subject: [PATCH 2/4] where filter uses array for everything --- lib/jekyll/filters.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 55e91ee8..b7c8e6bb 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -222,7 +222,7 @@ module Jekyll def where(input, property, value) return input unless input.is_a?(Enumerable) input = input.values if input.is_a?(Hash) - input.select { |object| item_property(object, property).to_s == value.to_s or item_property(object, property).include? value.to_s } + input.select { |object| Array(item_property(object, property)).map(&:to_s).include?(value.to_s) } end # Sort an array of objects From f5f8548eb8f36b279838c77cc890b1ccf2eb3692 Mon Sep 17 00:00:00 2001 From: timwis Date: Thu, 25 Feb 2016 10:24:47 -0500 Subject: [PATCH 3/4] add tests for where arrays --- test/test_filters.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/test_filters.rb b/test/test_filters.rb index e9035b96..6eaff8f4 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -306,6 +306,16 @@ class TestFilters < JekyllUnitTest assert_equal 2, @filter.where(@array_of_objects, "color", "red").length end + should "filter array properties appropriately" do + hash = {"a"=>{"tags"=>["x","y"]}, "b"=>{"tags"=>["x"]}, "c"=>{"tags"=>["y","z"]}} + assert_equal 2, @filter.where(hash, "tags", "x").length + end + + should "filter array properties alongside string properties" do + hash = {"a"=>{"tags"=>["x","y"]}, "b"=>{"tags"=>"x"}, "c"=>{"tags"=>["y","z"]}} + assert_equal 2, @filter.where(hash, "tags", "x").length + end + should "stringify during comparison for compatibility with liquid parsing" do hash = { "The Words" => {"rating" => 1.2, "featured" => false}, From e130a0841f2c0d074a0e43359a3a114f454e8b5c Mon Sep 17 00:00:00 2001 From: timwis Date: Thu, 3 Mar 2016 15:56:54 -0500 Subject: [PATCH 4/4] add test to ensure where doesn't match substrings --- test/test_filters.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test_filters.rb b/test/test_filters.rb index 6eaff8f4..cb22ed87 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -316,6 +316,11 @@ class TestFilters < JekyllUnitTest assert_equal 2, @filter.where(hash, "tags", "x").length end + should "not match substrings" do + hash = {"a"=>{"category"=>"bear"}, "b"=>{"category"=>"wolf"}, "c"=>{"category"=>["bear","lion"]}} + assert_equal 0, @filter.where(hash, "category", "ear").length + end + should "stringify during comparison for compatibility with liquid parsing" do hash = { "The Words" => {"rating" => 1.2, "featured" => false},