Skip to content

Commit

Permalink
Normalize previous/next resource API between Ruby and Liquid (#466)
Browse files Browse the repository at this point in the history
* Normalize the Ruby / Liquid API for previous/next resource

* Make sure there's no infinite loop
  • Loading branch information
jaredcwhite authored Nov 29, 2021
1 parent e854cd1 commit 7e7e236
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/drops/drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>]
def keys
(content_methods |
mutations.keys |
Expand Down
33 changes: 28 additions & 5 deletions bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -79,6 +81,27 @@ def collapse_document(doc)
result[key] = doc[key] unless NESTED_OBJECT_FIELD_BLACKLIST.include?(key)
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<String>]
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
JSON.pretty_generate hash_for_json
end
end
end
end
2 changes: 2 additions & 0 deletions bridgetown-core/lib/bridgetown-core/resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7e7e236

Please sign in to comment.