Skip to content

Commit

Permalink
Merge branch 'mongoid_embedded_docs' of https://github.com/yuki24/kam…
Browse files Browse the repository at this point in the history
…inari into yuki24-mongoid_embedded_docs
  • Loading branch information
amatsuda committed Aug 29, 2011
2 parents 6629f5b + 4276a2c commit a62388f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
10 changes: 9 additions & 1 deletion lib/kaminari/models/mongoid_criteria_methods.rb
Expand Up @@ -11,7 +11,15 @@ def offset_value #:nodoc:
end

def total_count #:nodoc:
count
embedded? ? unpage.count : count
end

private
def unpage
clone.tap do |crit|
crit.options.delete :limit
crit.options.delete :skip
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kaminari/models/mongoid_extension.rb
Expand Up @@ -7,7 +7,7 @@ module Criteria

included do
def page(*args)
self.klass.page(*args).criteria.merge(self)
super(*args).criteria.merge(self)
end
end
end
Expand Down
65 changes: 61 additions & 4 deletions spec/models/mongoid_spec.rb
Expand Up @@ -9,11 +9,12 @@ class Developer
field :salary, :type => Integer
end
end
before do
stub(subject).count { 300 } # in order to avoid DB access...
end

describe '#page' do
before do
stub(subject).count { 300 } # in order to avoid DB access...
end

context 'page 1' do
subject { Developer.page 1 }
it { should be_a Mongoid::Criteria }
Expand Down Expand Up @@ -58,15 +59,71 @@ class Developer
its(:num_pages) { should == 12 }
it { should skip 25 }
end

end

describe '#per' do
before do
stub(subject).count { 300 } # in order to avoid DB access...
end

subject { Developer.page(2).per(10) }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 2 }
its(:limit_value) { should == 10 }
its(:num_pages) { should == 30 }
it { should skip 10 }
end

describe '#page in embedded documents' do
before :all do
class MongoDeveloper
include ::Mongoid::Document
field :salary, :type => Integer
embeds_many :frameworks
end

class Framework
include ::Mongoid::Document
field :name, :type => String
field :language, :type => String
embedded_in :mongo_developer
end
end

before :all do
@mongo_developer = MongoDeveloper.new
@mongo_developer.frameworks.new(:name => "rails", :language => "ruby")
@mongo_developer.frameworks.new(:name => "merb", :language => "ruby")
@mongo_developer.frameworks.new(:name => "sinatra", :language => "ruby")
@mongo_developer.frameworks.new(:name => "cakephp", :language => "php")
@mongo_developer.frameworks.new(:name => "tornado", :language => "python")
end

context 'page 1' do
subject { @mongo_developer.frameworks.page(1).per(1) }
it { should be_a Mongoid::Criteria }
its(:total_count) { should == 5 }
its(:limit_value) { should == 1 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 5 }
end

context 'with criteria after' do
subject { @mongo_developer.frameworks.page(1).per(2).where(:language => "ruby") }
it { should be_a Mongoid::Criteria }
its(:total_count) { should == 3 }
its(:limit_value) { should == 2 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 2 }
end

context 'with criteria before' do
subject { @mongo_developer.frameworks.where(:language => "ruby").page(1).per(2) }
it { should be_a Mongoid::Criteria }
its(:total_count) { should == 3 }
its(:limit_value) { should == 2 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 2 }
end
end
end

0 comments on commit a62388f

Please sign in to comment.