Skip to content

MONGOID-5863 #last overrides #skip #5975

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
147 changes: 12 additions & 135 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,145 +7,22 @@ name: Run Mongoid Tests
- pull_request
jobs:
build:
name: "${{matrix.ruby}} drv:${{matrix.driver}} db:${{matrix.mongodb}}
name: "${{matrix.ruby}} db:${{matrix.mongodb}}
rails:${{matrix.rails}} fle:${{matrix.fle}} ${{matrix.topology}}"
env:
CI: true
TESTOPTS: "-v"
runs-on: ${{matrix.os}}
continue-on-error: "${{matrix.experimental}}"
BUNDLE_GEMFILE: "${{ matrix.rails == '' && 'Gemfile' || format('gemfiles/rails-{0}.gemfile', matrix.rails) }}"
runs-on: ubuntu-22.04
continue-on-error: false
strategy:
fail-fast: false
matrix:
include:
- mongodb: '7.0'
ruby: ruby-3.3
topology: replica_set
os: ubuntu-20.04
task: test
driver: current
gemfile: Gemfile
experimental: false
- mongodb: '6.0'
ruby: ruby-3.2
topology: replica_set
os: ubuntu-20.04
task: test
driver: current
gemfile: Gemfile
experimental: false
- mongodb: '6.0'
ruby: ruby-3.1
topology: replica_set
os: ubuntu-20.04
task: test
driver: current
gemfile: Gemfile
experimental: false
- mongodb: '6.0'
ruby: ruby-3.0
topology: replica_set
os: ubuntu-20.04
task: test
driver: stable
gemfile: gemfiles/driver_stable.gemfile
experimental: false
- mongodb: '7.0'
ruby: ruby-3.3
topology: server
os: ubuntu-22.04
task: test
driver: current
rails: '8.0'
fle: helper
gemfile: gemfiles/rails-8.0.gemfile
experimental: false
- mongodb: '7.0'
ruby: ruby-3.2
topology: server
os: ubuntu-20.04
task: test
driver: current
rails: '7.2'
fle: helper
gemfile: gemfiles/rails-7.2.gemfile
experimental: false
- mongodb: '7.0'
ruby: ruby-3.2
topology: server
os: ubuntu-20.04
task: test
driver: current
rails: '7.1'
fle: helper
gemfile: gemfiles/rails-7.1.gemfile
experimental: false
- mongodb: '7.0'
ruby: ruby-3.1
topology: server
os: ubuntu-20.04
task: test
driver: current
rails: '7.0'
fle: helper
gemfile: gemfiles/rails-7.0.gemfile
experimental: false
- mongodb: '6.0'
ruby: ruby-3.1
topology: server
os: ubuntu-20.04
task: test
driver: current
rails: '6.1'
fle: helper
gemfile: gemfiles/rails-6.1.gemfile
experimental: false
- mongodb: '6.0'
ruby: ruby-3.0
topology: server
os: ubuntu-20.04
task: test
driver: current
rails: '6.1'
fle: helper
gemfile: gemfiles/rails-6.1.gemfile
experimental: false
- mongodb: '6.0'
ruby: ruby-3.0
topology: server
os: ubuntu-20.04
task: test
driver: current
rails: '6.0'
fle: helper
gemfile: gemfiles/rails-6.0.gemfile
experimental: false
- mongodb: '6.0'
ruby: jruby-9.4
topology: server
os: ubuntu-20.04
task: test
driver: current
rails: '6.0'
fle: helper
gemfile: gemfiles/rails-6.0.gemfile
experimental: false
- mongodb: '5.0'
ruby: ruby-3.1
topology: replica_set
os: ubuntu-20.04
task: test
driver: current
gemfile: Gemfile
experimental: false
- mongodb: '4.4'
ruby: ruby-2.7
topology: replica_set
os: ubuntu-20.04
task: test
driver: current
gemfile: Gemfile
experimental: false
ruby: [ "3.3" ]
mongodb: [ "8.0" ]
rails: [ ~, "8.0" ]
fle: [ ~, "helper" ]
topology: [ replica_set, sharded_cluster ]

steps:
- name: repo checkout
Expand All @@ -169,20 +46,20 @@ jobs:
uses: ruby/setup-ruby@v1
env:
FLE: "${{matrix.fle}}"
BUNDLE_GEMFILE: "${{matrix.gemfile}}"
BUNDLE_GEMFILE: "${{env.BUNDLE_GEMFILE}}"
with:
ruby-version: "${{matrix.ruby}}"
bundler: 2
- name: bundle
run: bundle install --jobs 4 --retry 3
env:
FLE: "${{matrix.fle}}"
BUNDLE_GEMFILE: "${{matrix.gemfile}}"
BUNDLE_GEMFILE: "${{env.BUNDLE_GEMFILE}}"
- name: test
timeout-minutes: 60
continue-on-error: "${{matrix.experimental}}"
run: bundle exec rake ci
env:
BUNDLE_GEMFILE: "${{matrix.gemfile}}"
BUNDLE_GEMFILE: "${{env.BUNDLE_GEMFILE}}"
FLE: "${{matrix.fle}}"
MONGODB_URI: "${{ steps.start-mongodb.outputs.cluster-uri }}"
2 changes: 1 addition & 1 deletion lib/mongoid/contextual/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ def retrieve_nth_to_last(n)
end

def retrieve_nth_to_last_with_limit(n, limit)
v = view.sort(inverse_sorting).skip(n).limit(limit || 1)
v = view.sort(inverse_sorting).limit(limit || 1)
v = v.skip(n) if n > 0
raw_docs = v.to_a.reverse
process_raw_docs(raw_docs, limit)
Expand Down
6 changes: 6 additions & 0 deletions spec/mongoid/contextual/mongo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3292,6 +3292,12 @@
it "limits the results" do
expect(context.skip(1).entries).to eq([ new_order ])
end

context "with #last" do
it "returns the nth from last element" do
expect(context.skip(1).last).to eq(depeche_mode)
end
end
Copy link
Contributor

@johnnyshields johnnyshields May 5, 2025

Choose a reason for hiding this comment

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

The method has sort(inverse_sorting) and then later v.to_a.reverse, which ensure the ordering in the case that multiple docs are returned.

Is this covered by tests? i.e. ensuring correct document ordering the result for multiple docs?

  • combining skip(n) with limit(m) (where n, m > 1)
  • last(n) (where n > 1)

end

describe "#sort" do
Expand Down
Loading