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

add previous attribute #753

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
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 @@
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
Loading