From e031ac9b27bb3c76d318107edf8178c60ff67b21 Mon Sep 17 00:00:00 2001 From: Martin Desrumaux Date: Wed, 14 Jun 2017 22:18:07 +0200 Subject: [PATCH] Allow filters to sort & select based on subvalues (#5622) Merge pull request 5622 --- lib/jekyll/filters.rb | 4 +++- test/test_filters.rb | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index c8da0e52..e15cf7c6 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -361,7 +361,9 @@ module Jekyll private def item_property(item, property) if item.respond_to?(:to_liquid) - item.to_liquid[property.to_s] + property.to_s.split(".").reduce(item.to_liquid) do |subvalue, attribute| + subvalue[attribute] + end elsif item.respond_to?(:data) item.data[property.to_s] else diff --git a/test/test_filters.rb b/test/test_filters.rb index 243f97af..93ddb2cd 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -949,6 +949,12 @@ class TestFilters < JekyllUnitTest assert_equal [{ "a" => 1 }, { "a" => 2 }, { "b" => 1 }], @filter.sort([{ "a" => 2 }, { "b" => 1 }, { "a" => 1 }], "a", "last") end + should "return sorted by subproperty array" do + assert_equal [{ "a" => { "b" => 1 } }, { "a" => { "b" => 2 } }, + { "a" => { "b" => 3 } }, ], + @filter.sort([{ "a" => { "b" => 2 } }, { "a" => { "b" => 1 } }, + { "a" => { "b" => 3 } }, ], "a.b") + end end context "to_integer filter" do