From bb6d2824946efa365c7e38db0c467c5b55a98829 Mon Sep 17 00:00:00 2001 From: Jared White Date: Mon, 29 Nov 2021 10:44:47 -0800 Subject: [PATCH 1/2] Normalize the Ruby / Liquid API for previous/next resource --- .../lib/bridgetown-core/drops/resource_drop.rb | 16 ++++++++++++---- .../lib/bridgetown-core/resource/base.rb | 2 ++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb b/bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb index 2cc2cd82b..bbd3e2490 100644 --- a/bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb +++ b/bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb @@ -44,13 +44,15 @@ def <=>(other) cmp end - def previous - @previous ||= @obj.previous_resource.to_liquid + def next_resource + @next ||= @obj.next_resource.to_liquid end + alias_method :next, :next_resource - def next - @next ||= @obj.next_resource.to_liquid + def previous_resource + @previous ||= @obj.previous_resource.to_liquid end + alias_method :previous, :previous_resource # Generate a Hash for use in generating JSON. # This is useful if fields need to be cleared before the JSON can generate. @@ -79,6 +81,12 @@ def collapse_document(doc) result[key] = doc[key] unless NESTED_OBJECT_FIELD_BLACKLIST.include?(key) end end + + # Inspect the drop's keys and values through a JSON representation + # of its keys and values. + def inspect + JSON.pretty_generate hash_for_json + end end end end diff --git a/bridgetown-core/lib/bridgetown-core/resource/base.rb b/bridgetown-core/lib/bridgetown-core/resource/base.rb index a4b395d56..3a1067385 100644 --- a/bridgetown-core/lib/bridgetown-core/resource/base.rb +++ b/bridgetown-core/lib/bridgetown-core/resource/base.rb @@ -284,12 +284,14 @@ def next_resource collection.resources[pos + 1] if pos && pos < collection.resources.length - 1 end alias_method :next_doc, :next_resource + alias_method :next, :next_resource def previous_resource pos = collection.resources.index { |item| item.equal?(self) } collection.resources[pos - 1] if pos&.positive? end alias_method :previous_doc, :previous_resource + alias_method :previous, :previous_resource private From 47fc821850fbfbeff180d0b247c17ac4a352dac3 Mon Sep 17 00:00:00 2001 From: Jared White Date: Mon, 29 Nov 2021 10:54:42 -0800 Subject: [PATCH 2/2] Make sure there's no infinite loop --- .../lib/bridgetown-core/drops/drop.rb | 2 +- .../lib/bridgetown-core/drops/resource_drop.rb | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/bridgetown-core/lib/bridgetown-core/drops/drop.rb b/bridgetown-core/lib/bridgetown-core/drops/drop.rb index 0e97c4ef1..c057026c0 100644 --- a/bridgetown-core/lib/bridgetown-core/drops/drop.rb +++ b/bridgetown-core/lib/bridgetown-core/drops/drop.rb @@ -110,7 +110,7 @@ def key?(key) # underlying data hashes and performs a set union to ensure a list # of unique keys for the Drop. # - # Returns an Array of unique keys for content for the Drop. + # @return [Array] def keys (content_methods | mutations.keys | diff --git a/bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb b/bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb index bbd3e2490..a9b6d8009 100644 --- a/bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb +++ b/bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb @@ -6,7 +6,7 @@ class ResourceDrop < Drop extend Forwardable NESTED_OBJECT_FIELD_BLACKLIST = %w( - content output excerpt next previous + content output excerpt next previous next_resource previous_resource ).freeze mutable false @@ -82,6 +82,21 @@ def collapse_document(doc) end end + # Generates a list of keys with user content as their values. + # This gathers up the Drop methods and keys of the mutations and + # underlying data hashes and performs a set union to ensure a list + # of unique keys for the Drop. + # + # @return [Array] + def keys + keys_to_remove = %w[next_resource previous_resource] + (content_methods | + mutations.keys | + fallback_data.keys).flatten.reject do |key| + keys_to_remove.include?(key) + end + end + # Inspect the drop's keys and values through a JSON representation # of its keys and values. def inspect