Skip to content

Commit

Permalink
add previous attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbirke0927 committed Dec 5, 2024
1 parent aab0b43 commit 49d7536
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
19 changes: 19 additions & 0 deletions gem/lib/pagy/keyset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ def next
end
end

# Return the previous page
def previous
records

@previous ||= begin
hash = keyset_attributes_from(@records.first)
json = @vars[:jsonify_keyset_attributes]&.(hash) || hash.to_json
B64.urlsafe_encode(json)
end

Check failure on line 71 in gem/lib/pagy/keyset.rb

View workflow job for this annotation

GitHub Actions / Ruby 3.2 Test

Layout/BeginEndAlignment: `end` at 71, 16 is not aligned with `begin` at 67, 20.

Check failure on line 71 in gem/lib/pagy/keyset.rb

View workflow job for this annotation

GitHub Actions / Ruby 3.3 Test

Layout/BeginEndAlignment: `end` at 71, 16 is not aligned with `begin` at 67, 20.

return @previous if previous?

nil
end

def previous?
filter_oldest.limit(1).exists?
end

# Fetch the array of records for the current page
def records
@records ||= begin
Expand Down
3 changes: 3 additions & 0 deletions gem/lib/pagy/keyset/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def extract_keyset
# Filter the newest records
def filter_newest = @set.where(filter_newest_query, **@latest)

# Filter the oldest records
def filter_oldest = @set.reverse_order.where(filter_newest_query, **@previous)

# Append the missing keyset keys if the set is restricted by select
def apply_select
@set.select(*@keyset.keys)
Expand Down

0 comments on commit 49d7536

Please sign in to comment.