Skip to content

Add a cursor to go to the first page #42

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

Merged
merged 6 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 2.0.0 (Next)

* [#38](https://github.com/mongoid/mongoid-scroll/pull/38): Allow to reverse the scroll - [@GCorbel](https://github.com/GCorbel).
* [#42](https://github.com/mongoid/mongoid-scroll/pull/42): Add a cursor to go to the first page - [@GCorbel](https://github.com/GCorbel).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* [#42](https://github.com/mongoid/mongoid-scroll/pull/42): Add a cursor to go to the first page - [@GCorbel](https://github.com/GCorbel).
* [#42](https://github.com/mongoid/mongoid-scroll/pull/42): Add a `first` cursor - [@GCorbel](https://github.com/GCorbel).

I think for #38 above we should say "Add a previous cursor" too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed.

* Your contribution here.

### 1.0.1 (2023/03/15)
Expand Down
4 changes: 4 additions & 0 deletions lib/mongoid/criteria/scrollable/iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def initialize(previous_cursor:, next_cursor:)
@previous_cursor = previous_cursor
@next_cursor = next_cursor
end

def first_cursor
next_cursor.class.new(nil, next_cursor.sort_options)
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/mongo/collection_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@
expect(Mongoid.default_client['feed_items'].find.sort(field_name => 1).limit(2).scroll(second_iterator.previous_cursor, field_type: field_type).to_a).to eq(records.limit(2).to_a)
expect(Mongoid.default_client['feed_items'].find.sort(field_name => 1).limit(2).scroll(third_iterator.previous_cursor, field_type: field_type).to_a).to eq(records.skip(2).limit(2).to_a)
end
it 'can loop over the first records with the first cursor' do
first_cursor = nil
records = Mongoid.default_client['feed_items'].find.sort(field_name => 1)
cursor = cursor_type.from_record records.skip(4).first, field_name: field_name, field_type: field_type, include_current: true

records.limit(2).scroll(cursor, field_type: field_type) do |_, iterator|
first_cursor = iterator.first_cursor
end

expect(records.limit(2).scroll(first_cursor, field_type: field_type).to_a).to eq(records.limit(2).to_a)
end
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/mongoid/criteria_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@
expect(Feed::Item.asc(field_name).limit(2).scroll(second_iterator.previous_cursor)).to eq(records.limit(2))
expect(Feed::Item.asc(field_name).limit(2).scroll(third_iterator.previous_cursor)).to eq(records.skip(2).limit(2))
end
it 'can loop over the first records with the first page cursor' do
first_cursor = nil

Feed::Item.asc(field_name).limit(2).scroll(cursor_type) do |_, it|
first_cursor = it.first_cursor
end

expect(Feed::Item.asc(field_name).limit(2).scroll(first_cursor).to_a).to eq(Feed::Item.asc(field_name).limit(2).to_a)
end
end
end
end
Expand Down
Loading