Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Fix for Collection#slice incorrect offset adjustments for chained negative offsets #202

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ measurements
.bundle
Gemfile.*

## RVM
.rvmrc

## PROJECT::SPECIFIC
spec/db/
20 changes: 16 additions & 4 deletions lib/dm-core/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1412,12 +1412,24 @@ def sliced_query(offset, limit)
query = self.query

if offset >= 0
query.slice(offset, limit)
if query.add_reversed?
query.slice(query.limit - limit - offset, limit)
else
query.slice(offset, limit)
end
else
query = query.slice((limit + offset).abs, limit).reverse!
if query.limit
if query.add_reversed?
query.slice(-offset - limit, limit)
else
query.slice(query.limit + offset, limit)
end
else
query = query.slice((limit + offset).abs, limit).reverse!

# tell the Query to prepend each result from the adapter
query.update(:add_reversed => !query.add_reversed?)
# tell the Query to prepend each result from the adapter
query.update(:add_reversed => !query.add_reversed?)
end
end
end

Expand Down
72 changes: 72 additions & 0 deletions spec/public/shared/collection_shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,78 @@
end
end

describe 'with chained negative offset after positive' do
before :all do
unless @skip
@return = @resources = @articles.slice!(1, 6).slice!(-4, 3)
end
end

it 'should return a Collection' do
@return.should be_kind_of(DataMapper::Collection)
end

it 'should return the expected Resources' do
@return.should == @copy.entries.slice!(1, 6).slice!(-4, 3)
end

it 'should remove the Resources from the Collection' do
@resources.each { |resource| @articles.should_not be_include(resource) }
end

it 'should scope the Collection' do
@resources.reload.should == @copy.entries.slice!(1, 6).slice!(-4, 3)
end
end

describe 'with chained positive offset after negative' do
before :all do
unless @skip
@return = @resources = @articles.slice!(-6, 5).slice!(1, 2)
end
end

it 'should return a Collection' do
@return.should be_kind_of(DataMapper::Collection)
end

it 'should return the expected Resources' do
@return.should == @copy.entries.slice!(-6, 5).slice!(1, 2)
end

it 'should remove the Resources from the Collection' do
@resources.each { |resource| @articles.should_not be_include(resource) }
end

it 'should scope the Collection' do
@resources.reload.should == @copy.entries.slice!(-6, 5).slice!(1, 2)
end
end

describe 'with chained negative offset after negative' do
before :all do
unless @skip
@return = @resources = @articles.slice!(-7, 6).slice!(-4, 2)
end
end

it 'should return a Collection' do
@return.should be_kind_of(DataMapper::Collection)
end

it 'should return the expected Resources' do
@return.should == @copy.entries.slice!(-7, 6).slice!(-4, 2)
end

it 'should remove the Resources from the Collection' do
@resources.each { |resource| @articles.should_not be_include(resource) }
end

it 'should scope the Collection' do
@resources.reload.should == @copy.entries.slice!(-7, 6).slice!(-4, 2)
end
end

describe 'with an offset not within the Collection' do
before :all do
unless @skip
Expand Down
8 changes: 6 additions & 2 deletions spec/semipublic/shared/subject_shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
describe 'with a default' do
subject { @subject_with_default.default? }

it { should be(true) }
it "default?" do
should be(true)
end
end

describe 'without a default' do
subject { @subject_without_default.default? }

it { should be(false) }
it "default?" do
should be(false)
end
end
end

Expand Down