Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add support for mongoid max_scan option #500

Merged
merged 1 commit into from

3 participants

@aptx4869

Ignore specs when mongoid version is under 2.4
#495

@aptx4869 aptx4869 Add support for mongoid max_scan option
respect the max_scan option in criteria

cache the total_count in the collection instance
prevent hitting db again in the same instance
88c163e
@yuki24
Collaborator

Looks good, @amatsuda @zzak any thoughts? I think we can still merge this request at this point and work on extracting kaminari-mongoid later.

@amatsuda
Owner

:+1:

@yuki24 yuki24 merged commit cd90846 into amatsuda:master
@aptx4869 aptx4869 deleted the aptx4869:support-mongoid-max-scan branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 26, 2014
  1. @aptx4869

    Add support for mongoid max_scan option

    aptx4869 authored
    respect the max_scan option in criteria
    
    cache the total_count in the collection instance
    prevent hitting db again in the same instance
This page is out of date. Refresh to see the latest.
View
12 lib/kaminari/models/mongoid_criteria_methods.rb
@@ -9,7 +9,17 @@ def offset_value #:nodoc:
end
def total_count #:nodoc:
- embedded? ? unpage.length : length
+ @total_count ||=
+ if embedded?
+ unpage.count
+ else
+ counter_result = count
+ if options[:max_scan] and options[:max_scan] < counter_result
+ options[:max_scan]
+ else
+ counter_result
+ end
+ end
end
private
View
54 spec/models/mongoid/mongoid_spec.rb
@@ -8,6 +8,60 @@
end
end
+ describe 'max_scan', :if => Mongoid::VERSION >= '3' do
+ context 'less than total' do
+ context 'page 1' do
+ subject { User.max_scan(20).page 1 }
+ it { should be_a Mongoid::Criteria }
+ its(:current_page) { should == 1 }
+ its(:prev_page) { should be_nil }
+ its(:next_page) { should be_nil }
+ its(:limit_value) { should == 25 }
+ its(:total_pages) { should == 1 }
+ its(:total_count) { should == 20 }
+ it { should skip(0) }
+ end
+
+ context 'page 2' do
+ subject { User.max_scan(30).page 2 }
+ it { should be_a Mongoid::Criteria }
+ its(:current_page) { should == 2 }
+ its(:prev_page) { should == 1 }
+ its(:next_page) { should be_nil }
+ its(:limit_value) { should == 25 }
+ its(:total_pages) { should == 2 }
+ its(:total_count) { should == 30 }
+ it { should skip 25 }
+ end
+ end
+
+ context 'more than total' do
+ context 'page 1' do
+ subject { User.max_scan(60).page 1 }
+ it { should be_a Mongoid::Criteria }
+ its(:current_page) { should == 1 }
+ its(:prev_page) { should be_nil }
+ its(:next_page) { should == 2 }
+ its(:limit_value) { should == 25 }
+ its(:total_pages) { should == 2 }
+ its(:total_count) { should == 41 }
+ it { should skip(0) }
+ end
+
+ context 'page 2' do
+ subject { User.max_scan(60).page 2 }
+ it { should be_a Mongoid::Criteria }
+ its(:current_page) { should == 2 }
+ its(:prev_page) { should == 1 }
+ its(:next_page) { should be_nil }
+ its(:limit_value) { should == 25 }
+ its(:total_pages) { should == 2 }
+ its(:total_count) { should == 41 }
+ it { should skip 25 }
+ end
+ end
+ end
+
describe '#page' do
context 'page 1' do
Something went wrong with that request. Please try again.