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

Normalize previous/next resource API between Ruby and Liquid #466

Merged
merged 2 commits into from
Nov 29, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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