mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
Extract Mongoid integration to a separate gem
This commit is contained in:
parent
8695dfaaf8
commit
0bf21c37a9
14 changed files with 0 additions and 482 deletions
|
@ -16,10 +16,6 @@ gemfile:
|
|||
- gemfiles/active_record_41.gemfile
|
||||
- gemfiles/active_record_42.gemfile
|
||||
- gemfiles/active_record_edge.gemfile
|
||||
- gemfiles/mongoid_30.gemfile
|
||||
- gemfiles/mongoid_31.gemfile
|
||||
- gemfiles/mongoid_40.gemfile
|
||||
- gemfiles/mongoid_50.gemfile
|
||||
|
||||
sudo: false
|
||||
|
||||
|
|
4
Rakefile
4
Rakefile
|
@ -19,10 +19,6 @@ namespace :spec do
|
|||
active_record_41
|
||||
active_record_40
|
||||
active_record_32
|
||||
mongoid_50
|
||||
mongoid_40
|
||||
mongoid_31
|
||||
mongoid_30
|
||||
)
|
||||
|
||||
mappers.each do |gemfile|
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
source 'https://rubygems.org'
|
||||
|
||||
gem 'railties', '>= 3.2.3'
|
||||
gem 'mongoid', '~> 3.0.0'
|
||||
gem 'rspec-rails', '~> 2.14.1'
|
||||
gem 'origin'
|
||||
gem 'moped'
|
||||
gem 'activesupport', '~> 3.2.18'
|
||||
|
||||
if RUBY_VERSION > '1.9.3'
|
||||
gem 'mime-types'
|
||||
else
|
||||
gem 'mime-types', '2.99'
|
||||
end
|
||||
|
||||
platforms :mri do
|
||||
gem 'test-unit' if RUBY_VERSION > "2.1.0"
|
||||
end
|
||||
|
||||
platforms :rbx do
|
||||
gem 'rubysl', '~> 2.0'
|
||||
gem 'racc'
|
||||
gem 'rubysl-test-unit', github: 'rubysl/rubysl-test-unit', branch: '2.0'
|
||||
gem 'rubinius-developer_tools'
|
||||
end
|
||||
|
||||
gemspec :path => '../'
|
|
@ -1,26 +0,0 @@
|
|||
source 'https://rubygems.org'
|
||||
|
||||
gem 'railties', '~> 3.2'
|
||||
gem 'mongoid', '~> 3.1.0'
|
||||
gem 'rspec-rails', '~> 2.14.1'
|
||||
gem 'origin'
|
||||
gem 'moped'
|
||||
|
||||
if RUBY_VERSION > '1.9.3'
|
||||
gem 'mime-types'
|
||||
else
|
||||
gem 'mime-types', '2.99'
|
||||
end
|
||||
|
||||
platforms :mri do
|
||||
gem 'test-unit' if RUBY_VERSION > "2.1.0"
|
||||
end
|
||||
|
||||
platforms :rbx do
|
||||
gem 'rubysl', '~> 2.0'
|
||||
gem 'racc'
|
||||
gem 'rubysl-test-unit', github: 'rubysl/rubysl-test-unit', branch: '2.0'
|
||||
gem 'rubinius-developer_tools'
|
||||
end
|
||||
|
||||
gemspec :path => '../'
|
|
@ -1,24 +0,0 @@
|
|||
source 'https://rubygems.org'
|
||||
|
||||
gem 'railties', '~> 4.0'
|
||||
gem 'mongoid', '~> 4.0.0'
|
||||
gem 'rspec-rails', '~> 2.14.1'
|
||||
|
||||
if RUBY_VERSION > '1.9.3'
|
||||
gem 'mime-types'
|
||||
else
|
||||
gem 'mime-types', '2.99'
|
||||
end
|
||||
|
||||
platforms :mri do
|
||||
gem 'test-unit' if RUBY_VERSION > '2.1.0'
|
||||
end
|
||||
|
||||
platforms :rbx do
|
||||
gem 'rubysl', '~> 2.0'
|
||||
gem 'racc'
|
||||
gem 'minitest'
|
||||
gem 'rubinius-developer_tools'
|
||||
end
|
||||
|
||||
gemspec :path => '../'
|
|
@ -1,28 +0,0 @@
|
|||
source 'https://rubygems.org'
|
||||
|
||||
gem 'railties', '~> 4.0'
|
||||
gem 'mongoid', '~> 5.0.0'
|
||||
gem 'rspec-rails', '~> 2.14.1'
|
||||
|
||||
if RUBY_VERSION > '1.9.3'
|
||||
gem 'mime-types'
|
||||
else
|
||||
gem 'mime-types', '2.99'
|
||||
end
|
||||
|
||||
group :development, :test do
|
||||
gem 'database_cleaner', '~> 1.5.1'
|
||||
end
|
||||
|
||||
platforms :mri do
|
||||
gem 'test-unit' if RUBY_VERSION > '2.1.0'
|
||||
end
|
||||
|
||||
platforms :rbx do
|
||||
gem 'rubysl', '~> 2.0'
|
||||
gem 'racc'
|
||||
gem 'minitest'
|
||||
gem 'rubinius-developer_tools'
|
||||
end
|
||||
|
||||
gemspec :path => '../'
|
|
@ -6,12 +6,6 @@ module Kaminari
|
|||
::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension
|
||||
end
|
||||
|
||||
begin; require 'mongoid'; rescue LoadError; end
|
||||
if defined? ::Mongoid
|
||||
require 'kaminari/models/mongoid_extension'
|
||||
::Mongoid::Document.send :include, Kaminari::MongoidExtension::Document
|
||||
end
|
||||
|
||||
require 'kaminari/models/array_extension'
|
||||
|
||||
ActiveSupport.on_load(:action_view) do
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
module Kaminari
|
||||
module MongoidCriteriaMethods
|
||||
def initialize_copy(other) #:nodoc:
|
||||
@total_count = nil
|
||||
super
|
||||
end
|
||||
|
||||
def entry_name
|
||||
model_name.human.downcase
|
||||
end
|
||||
|
||||
def limit_value #:nodoc:
|
||||
options[:limit]
|
||||
end
|
||||
|
||||
def offset_value #:nodoc:
|
||||
options[:skip]
|
||||
end
|
||||
|
||||
def total_count #:nodoc:
|
||||
@total_count ||= if embedded?
|
||||
unpage.size
|
||||
else
|
||||
if options[:max_scan] && options[:max_scan] < size
|
||||
options[:max_scan]
|
||||
else
|
||||
size
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def unpage
|
||||
clone.tap do |crit|
|
||||
crit.options.delete :limit
|
||||
crit.options.delete :skip
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,26 +0,0 @@
|
|||
require 'kaminari/models/mongoid_criteria_methods'
|
||||
|
||||
module Kaminari
|
||||
module MongoidExtension
|
||||
module Document
|
||||
extend ActiveSupport::Concern
|
||||
include Kaminari::ConfigurationMethods
|
||||
|
||||
included do
|
||||
scope Kaminari.config.page_method_name, Proc.new {|num|
|
||||
limit(default_per_page).offset(default_per_page * ((num = num.to_i - 1) < 0 ? 0 : num))
|
||||
} do
|
||||
include Kaminari::MongoidCriteriaMethods
|
||||
include Kaminari::PageScopeMethods
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def inherited(kls)
|
||||
super
|
||||
kls.send(:include, Kaminari::MongoidExtension::Document.dup)
|
||||
end
|
||||
end if Mongoid::VERSION < '5.0.0'
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,20 +0,0 @@
|
|||
# Ensure we use 'syck' instead of 'psych' in 1.9.2
|
||||
# RubyGems >= 1.5.0 uses 'psych' on 1.9.2, but
|
||||
# Psych does not yet support YAML 1.1 merge keys.
|
||||
# Merge keys is often used in mongoid.yml
|
||||
# See: http://redmine.ruby-lang.org/issues/show/4300
|
||||
require 'mongoid/version'
|
||||
|
||||
if RUBY_VERSION >= '1.9.2' && RUBY_VERSION < '2.2.0'
|
||||
YAML::ENGINE.yamler = 'syck'
|
||||
end
|
||||
|
||||
Mongoid.configure do |config|
|
||||
if Mongoid::VERSION >= '5.0.0'
|
||||
config.connect_to 'kaminari_test'
|
||||
elsif Mongoid::VERSION > '3.0.0'
|
||||
config.sessions = {:default => {:hosts => ['0.0.0.0:27017'], :database => 'kaminari_test'}}
|
||||
else
|
||||
config.master = Mongo::Connection.new.db('kaminari_test')
|
||||
end
|
||||
end
|
|
@ -1,38 +0,0 @@
|
|||
class User
|
||||
include ::Mongoid::Document
|
||||
if Mongoid::VERSION >= '4.0.0'
|
||||
include Mongoid::Attributes::Dynamic
|
||||
end
|
||||
|
||||
field :name, :type => String
|
||||
field :age, :type => Integer
|
||||
end
|
||||
|
||||
class User::Address
|
||||
include ::Mongoid::Document
|
||||
end
|
||||
|
||||
class Product
|
||||
include ::Mongoid::Document
|
||||
end
|
||||
|
||||
class Device < Product
|
||||
paginates_per 100
|
||||
end
|
||||
|
||||
class Android < Device
|
||||
paginates_per 200
|
||||
end
|
||||
|
||||
class MongoMongoidExtensionDeveloper
|
||||
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_mongoid_extension_developer
|
||||
end
|
|
@ -3,7 +3,6 @@ require 'action_controller/railtie'
|
|||
require 'action_view/railtie'
|
||||
|
||||
require 'fake_app/active_record/config' if defined? ActiveRecord
|
||||
require 'fake_app/mongoid/config' if defined? Mongoid
|
||||
# config
|
||||
app = Class.new(Rails::Application)
|
||||
app.config.secret_token = '3b7cd727ee24e8444053437c36cc66c4'
|
||||
|
@ -25,7 +24,6 @@ end
|
|||
|
||||
#models
|
||||
require 'fake_app/active_record/models' if defined? ActiveRecord
|
||||
require 'fake_app/mongoid/models' if defined? Mongoid
|
||||
|
||||
# controllers
|
||||
class ApplicationController < ActionController::Base; end
|
||||
|
|
|
@ -1,230 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
if defined? Mongoid
|
||||
describe Kaminari::MongoidCriteriaMethods do
|
||||
describe "#total_count" do
|
||||
before do
|
||||
2.times {|i| User.create!(:salary => i) }
|
||||
end
|
||||
|
||||
context "when the scope is cloned" do
|
||||
it "should reset total_count memoization" do
|
||||
User.page.tap(&:total_count).where(:salary => 1).total_count.should == 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Kaminari::MongoidExtension do
|
||||
before(:each) do
|
||||
41.times do
|
||||
User.create!({:salary => 1})
|
||||
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
|
||||
subject { User.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 }
|
||||
it { should skip(0) }
|
||||
end
|
||||
|
||||
context 'page 2' do
|
||||
subject { User.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 }
|
||||
it { should skip 25 }
|
||||
end
|
||||
|
||||
context 'page "foobar"' do
|
||||
subject { User.page 'foobar' }
|
||||
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 }
|
||||
it { should skip 0 }
|
||||
end
|
||||
|
||||
shared_examples 'complete valid pagination' do
|
||||
if Mongoid::VERSION > '3.0.0'
|
||||
its(:selector) { should == {'salary' => 1} }
|
||||
else
|
||||
its(:selector) { should == {:salary => 1} }
|
||||
end
|
||||
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 }
|
||||
it { should skip 25 }
|
||||
end
|
||||
|
||||
context 'with criteria before' do
|
||||
subject { User.where(:salary => 1).page 2 }
|
||||
it_should_behave_like 'complete valid pagination'
|
||||
end
|
||||
|
||||
context 'with criteria after' do
|
||||
subject { User.page(2).where(:salary => 1) }
|
||||
it_should_behave_like 'complete valid pagination'
|
||||
end
|
||||
|
||||
context "with database:", :if => Mongoid::VERSION >= '3' do
|
||||
before :all do
|
||||
User.with(:database => "default_db").delete_all
|
||||
User.with(:database => "other_db").delete_all
|
||||
15.times { User.with(:database => "default_db").create!(:salary => 1) }
|
||||
10.times { User.with(:database => "other_db").create!(:salary => 1) }
|
||||
end
|
||||
|
||||
context "default_db" do
|
||||
subject { User.with(:database => "default_db").order_by(:artist.asc).page(1) }
|
||||
its(:total_count) { should == 15 }
|
||||
end
|
||||
|
||||
context "other_db" do
|
||||
subject { User.with(:database => "other_db").order_by(:artist.asc).page(1) }
|
||||
its(:total_count) { should == 10 }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#per' do
|
||||
subject { User.page(2).per(10) }
|
||||
it { should be_a Mongoid::Criteria }
|
||||
its(:current_page) { should == 2 }
|
||||
its(:prev_page) { should == 1 }
|
||||
its(:next_page) { should == 3 }
|
||||
its(:limit_value) { should == 10 }
|
||||
its(:total_pages) { should == 5 }
|
||||
it { should skip 10 }
|
||||
end
|
||||
|
||||
describe '#page in embedded documents' do
|
||||
before do
|
||||
@mongo_developer = MongoMongoidExtensionDeveloper.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(:prev_page) { should be_nil }
|
||||
its(:next_page) { should == 2 }
|
||||
its(:total_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(:prev_page) { should be_nil }
|
||||
its(:next_page) { should == 2 }
|
||||
its(:total_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(:prev_page) { should be_nil }
|
||||
its(:next_page) { should == 2 }
|
||||
its(:total_pages) { should == 2 }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#paginates_per' do
|
||||
context 'when paginates_per is not defined in superclass' do
|
||||
subject { Product.all.page 1 }
|
||||
its(:limit_value) { should == 25 }
|
||||
end
|
||||
|
||||
context 'when paginates_per is defined in subclass' do
|
||||
subject { Device.all.page 1 }
|
||||
its(:limit_value) { should == 100 }
|
||||
end
|
||||
|
||||
context 'when paginates_per is defined in subclass of subclass' do
|
||||
subject { Android.all.page 1 }
|
||||
its(:limit_value) { should == 200 }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,7 +0,0 @@
|
|||
RSpec.configure do |config|
|
||||
config.before :suite do
|
||||
# Mongoid 5 is very noisy at DEBUG level by default
|
||||
Mongoid.logger.level = Logger::INFO
|
||||
Mongo::Logger.logger.level = Logger::INFO if defined?(Mongo)
|
||||
end
|
||||
end if defined? Mongoid
|
Loading…
Reference in a new issue