wulffeld_kaminari 0.12.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gemtest +0 -0
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/CHANGELOG +245 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +231 -0
- data/Rakefile +22 -0
- data/app/views/kaminari/_first_page.html.erb +11 -0
- data/app/views/kaminari/_first_page.html.haml +9 -0
- data/app/views/kaminari/_first_page.html.slim +10 -0
- data/app/views/kaminari/_gap.html.erb +8 -0
- data/app/views/kaminari/_gap.html.haml +8 -0
- data/app/views/kaminari/_gap.html.slim +9 -0
- data/app/views/kaminari/_last_page.html.erb +11 -0
- data/app/views/kaminari/_last_page.html.haml +9 -0
- data/app/views/kaminari/_last_page.html.slim +10 -0
- data/app/views/kaminari/_next_page.html.erb +11 -0
- data/app/views/kaminari/_next_page.html.haml +9 -0
- data/app/views/kaminari/_next_page.html.slim +10 -0
- data/app/views/kaminari/_page.html.erb +12 -0
- data/app/views/kaminari/_page.html.haml +10 -0
- data/app/views/kaminari/_page.html.slim +11 -0
- data/app/views/kaminari/_paginator.html.erb +23 -0
- data/app/views/kaminari/_paginator.html.haml +18 -0
- data/app/views/kaminari/_paginator.html.slim +19 -0
- data/app/views/kaminari/_prev_page.html.erb +11 -0
- data/app/views/kaminari/_prev_page.html.haml +9 -0
- data/app/views/kaminari/_prev_page.html.slim +10 -0
- data/config/locales/kaminari.yml +10 -0
- data/kaminari.gemspec +35 -0
- data/lib/generators/kaminari/config_generator.rb +16 -0
- data/lib/generators/kaminari/templates/kaminari_config.rb +8 -0
- data/lib/generators/kaminari/views_generator.rb +103 -0
- data/lib/kaminari/config.rb +41 -0
- data/lib/kaminari/engine.rb +4 -0
- data/lib/kaminari/helpers/action_view_extension.rb +50 -0
- data/lib/kaminari/helpers/paginator.rb +154 -0
- data/lib/kaminari/helpers/tags.rb +95 -0
- data/lib/kaminari/models/active_record_extension.rb +25 -0
- data/lib/kaminari/models/active_record_relation_methods.rb +26 -0
- data/lib/kaminari/models/array_extension.rb +42 -0
- data/lib/kaminari/models/configuration_methods.rb +20 -0
- data/lib/kaminari/models/mongo_mapper_extension.rb +18 -0
- data/lib/kaminari/models/mongoid_criteria_methods.rb +18 -0
- data/lib/kaminari/models/mongoid_extension.rb +31 -0
- data/lib/kaminari/models/page_scope_methods.rb +36 -0
- data/lib/kaminari/models/plucky_criteria_methods.rb +18 -0
- data/lib/kaminari/railtie.rb +36 -0
- data/lib/kaminari/version.rb +3 -0
- data/lib/kaminari.rb +2 -0
- data/spec/acceptance/acceptance_helper.rb +5 -0
- data/spec/acceptance/support/helpers.rb +5 -0
- data/spec/acceptance/support/paths.rb +9 -0
- data/spec/acceptance/users_spec.rb +53 -0
- data/spec/config/config_spec.rb +61 -0
- data/spec/fake_app.rb +80 -0
- data/spec/helpers/action_view_extension_spec.rb +37 -0
- data/spec/helpers/helpers_spec.rb +135 -0
- data/spec/helpers/tags_spec.rb +140 -0
- data/spec/models/active_record_relation_methods_spec.rb +28 -0
- data/spec/models/array_spec.rb +105 -0
- data/spec/models/default_per_page_spec.rb +29 -0
- data/spec/models/mongo_mapper_spec.rb +79 -0
- data/spec/models/mongoid_spec.rb +72 -0
- data/spec/models/scopes_spec.rb +149 -0
- data/spec/spec_helper.rb +28 -0
- data/spec/support/database_cleaner.rb +13 -0
- data/spec/support/matchers.rb +46 -0
- metadata +311 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'kaminari/models/active_record_relation_methods'
|
2
|
+
|
3
|
+
module Kaminari
|
4
|
+
module ActiveRecordExtension
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
included do
|
7
|
+
def self.inherited(kls) #:nodoc:
|
8
|
+
super
|
9
|
+
|
10
|
+
kls.class_eval do
|
11
|
+
include Kaminari::ConfigurationMethods
|
12
|
+
|
13
|
+
# Fetch the values at the specified page number
|
14
|
+
# Model.page(5)
|
15
|
+
scope :page, Proc.new {|num|
|
16
|
+
limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1))
|
17
|
+
} do
|
18
|
+
include Kaminari::ActiveRecordRelationMethods
|
19
|
+
include Kaminari::PageScopeMethods
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Kaminari
|
2
|
+
module ActiveRecordRelationMethods
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
module InstanceMethods
|
5
|
+
# a workaround for AR 3.0.x that returns 0 for #count when page > 1
|
6
|
+
# if +limit_value+ is specified, load all the records and count them
|
7
|
+
if ActiveRecord::VERSION::STRING < '3.1'
|
8
|
+
def count #:nodoc:
|
9
|
+
limit_value ? length : super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def total_count #:nodoc:
|
14
|
+
# #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
|
15
|
+
c = except(:offset, :limit, :order)
|
16
|
+
# a workaround for 3.1.beta1 bug. see: https://github.com/rails/rails/issues/406
|
17
|
+
c = c.reorder nil
|
18
|
+
# Remove includes only if they are irrelevant
|
19
|
+
c = c.except(:includes) unless references_eager_loaded_tables?
|
20
|
+
# .group returns an OrderdHash that responds to #count
|
21
|
+
c = c.count
|
22
|
+
c.respond_to?(:count) ? c.count : c
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Kaminari
|
2
|
+
# Kind of Array that can paginate
|
3
|
+
class PaginatableArray < Array
|
4
|
+
include Kaminari::ConfigurationMethods::ClassMethods
|
5
|
+
|
6
|
+
attr_internal_accessor :limit_value, :offset_value
|
7
|
+
|
8
|
+
def initialize(original_array, limit_val = default_per_page, offset_val = 0) #:nodoc:
|
9
|
+
@_original_array, @_limit_value, @_offset_value = original_array, limit_val, offset_val
|
10
|
+
super(original_array[offset_val, limit_val] || [])
|
11
|
+
end
|
12
|
+
|
13
|
+
# items at the specified "page"
|
14
|
+
def page(num = 1)
|
15
|
+
offset(limit_value * ([num.to_i, 1].max - 1))
|
16
|
+
end
|
17
|
+
|
18
|
+
# returns another chunk of the original array
|
19
|
+
def limit(num)
|
20
|
+
self.class.new @_original_array, num, offset_value
|
21
|
+
end
|
22
|
+
|
23
|
+
# total item numbers of the original array
|
24
|
+
def total_count
|
25
|
+
@_original_array.count
|
26
|
+
end
|
27
|
+
|
28
|
+
# returns another chunk of the original array
|
29
|
+
def offset(num)
|
30
|
+
arr = self.class.new @_original_array, limit_value, num
|
31
|
+
class << arr
|
32
|
+
include Kaminari::PageScopeMethods
|
33
|
+
end
|
34
|
+
arr
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Wrap an Array object to make it paginatable
|
39
|
+
def self.paginate_array(array)
|
40
|
+
PaginatableArray.new array
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Kaminari
|
2
|
+
module ConfigurationMethods
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
module ClassMethods
|
5
|
+
# Overrides the default +per_page+ value per model
|
6
|
+
# class Article < ActiveRecord::Base
|
7
|
+
# paginates_per 10
|
8
|
+
# end
|
9
|
+
def paginates_per(val)
|
10
|
+
@_default_per_page = val
|
11
|
+
end
|
12
|
+
|
13
|
+
# This model's default +per_page+ value
|
14
|
+
# returns +default_per_page+ value unless explicitly overridden via <tt>paginates_per</tt>
|
15
|
+
def default_per_page
|
16
|
+
@_default_per_page || Kaminari.config.default_per_page
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'kaminari/models/plucky_criteria_methods'
|
2
|
+
|
3
|
+
module Kaminari
|
4
|
+
module MongoMapperExtension
|
5
|
+
module Document
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
include Kaminari::ConfigurationMethods
|
8
|
+
|
9
|
+
included do
|
10
|
+
# Fetch the values at the specified page number
|
11
|
+
# Model.page(5)
|
12
|
+
scope :page, Proc.new {|num|
|
13
|
+
limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1))
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Kaminari
|
2
|
+
module MongoidCriteriaMethods
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
module InstanceMethods
|
5
|
+
def limit_value #:nodoc:
|
6
|
+
options[:limit]
|
7
|
+
end
|
8
|
+
|
9
|
+
def offset_value #:nodoc:
|
10
|
+
options[:skip]
|
11
|
+
end
|
12
|
+
|
13
|
+
def total_count #:nodoc:
|
14
|
+
count
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'kaminari/models/mongoid_criteria_methods'
|
2
|
+
|
3
|
+
module Kaminari
|
4
|
+
module MongoidExtension
|
5
|
+
module Criteria
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
def page(*args)
|
10
|
+
self.klass.page(*args).criteria.merge(self)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module Document
|
16
|
+
extend ActiveSupport::Concern
|
17
|
+
include Kaminari::ConfigurationMethods
|
18
|
+
|
19
|
+
included do
|
20
|
+
# Fetch the values at the specified page number
|
21
|
+
# Model.page(5)
|
22
|
+
scope :page, Proc.new {|num|
|
23
|
+
limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1))
|
24
|
+
} do
|
25
|
+
include Kaminari::MongoidCriteriaMethods
|
26
|
+
include Kaminari::PageScopeMethods
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Kaminari
|
2
|
+
module PageScopeMethods
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
module InstanceMethods
|
5
|
+
# Specify the <tt>per_page</tt> value for the preceding <tt>page</tt> scope
|
6
|
+
# Model.page(3).per(10)
|
7
|
+
def per(num)
|
8
|
+
if (n = num.to_i) <= 0
|
9
|
+
self
|
10
|
+
else
|
11
|
+
limit(n).offset(offset_value / limit_value * n)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Total number of pages
|
16
|
+
def num_pages
|
17
|
+
(total_count.to_f / limit_value).ceil
|
18
|
+
end
|
19
|
+
|
20
|
+
# Current page number
|
21
|
+
def current_page
|
22
|
+
(offset_value / limit_value) + 1
|
23
|
+
end
|
24
|
+
|
25
|
+
# First page of the collection ?
|
26
|
+
def first_page?
|
27
|
+
current_page == 1
|
28
|
+
end
|
29
|
+
|
30
|
+
# Last page of the collection?
|
31
|
+
def last_page?
|
32
|
+
current_page >= num_pages
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Kaminari
|
2
|
+
module PluckyCriteriaMethods
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
module InstanceMethods
|
5
|
+
def limit_value #:nodoc:
|
6
|
+
options[:limit]
|
7
|
+
end
|
8
|
+
|
9
|
+
def offset_value #:nodoc:
|
10
|
+
options[:skip]
|
11
|
+
end
|
12
|
+
|
13
|
+
def total_count #:nodoc:
|
14
|
+
count
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rails'
|
2
|
+
# ensure ORMs are loaded *before* initializing Kaminari
|
3
|
+
begin; require 'mongoid'; rescue LoadError; end
|
4
|
+
begin; require 'mongo_mapper'; rescue LoadError; end
|
5
|
+
|
6
|
+
require 'kaminari/config'
|
7
|
+
require 'kaminari/helpers/action_view_extension'
|
8
|
+
require 'kaminari/helpers/paginator'
|
9
|
+
require 'kaminari/models/page_scope_methods'
|
10
|
+
require 'kaminari/models/configuration_methods'
|
11
|
+
|
12
|
+
module Kaminari
|
13
|
+
class Railtie < ::Rails::Railtie #:nodoc:
|
14
|
+
initializer 'kaminari' do |app|
|
15
|
+
ActiveSupport.on_load(:active_record) do
|
16
|
+
require 'kaminari/models/active_record_extension'
|
17
|
+
::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension
|
18
|
+
end
|
19
|
+
if defined? ::Mongoid
|
20
|
+
require 'kaminari/models/mongoid_extension'
|
21
|
+
::Mongoid::Document.send :include, Kaminari::MongoidExtension::Document
|
22
|
+
::Mongoid::Criteria.send :include, Kaminari::MongoidExtension::Criteria
|
23
|
+
end
|
24
|
+
if defined? ::MongoMapper
|
25
|
+
require 'kaminari/models/mongo_mapper_extension'
|
26
|
+
::MongoMapper::Document.send :include, Kaminari::MongoMapperExtension::Document
|
27
|
+
::Plucky::Query.send :include, Kaminari::PluckyCriteriaMethods
|
28
|
+
::Plucky::Query.send :include, Kaminari::PageScopeMethods
|
29
|
+
end
|
30
|
+
require 'kaminari/models/array_extension'
|
31
|
+
ActiveSupport.on_load(:action_view) do
|
32
|
+
::ActionView::Base.send :include, Kaminari::ActionViewExtension
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/kaminari.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/acceptance_helper')
|
3
|
+
|
4
|
+
feature 'Users' do
|
5
|
+
background do
|
6
|
+
1.upto(100) {|i| User.create! :name => "user#{'%03d' % i}" }
|
7
|
+
end
|
8
|
+
scenario 'navigating by pagination links' do
|
9
|
+
visit users_path
|
10
|
+
|
11
|
+
within 'nav.pagination' do
|
12
|
+
within 'span.page.current' do
|
13
|
+
page.should have_content '1'
|
14
|
+
end
|
15
|
+
within 'span.next' do
|
16
|
+
click_link 'Next ›'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
within 'nav.pagination' do
|
21
|
+
within 'span.page.current' do
|
22
|
+
page.should have_content '2'
|
23
|
+
end
|
24
|
+
within 'span.last' do
|
25
|
+
click_link 'Last »'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
within 'nav.pagination' do
|
30
|
+
within 'span.page.current' do
|
31
|
+
page.should have_content '4'
|
32
|
+
end
|
33
|
+
within 'span.prev' do
|
34
|
+
click_link '‹ Prev'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
within 'nav.pagination' do
|
39
|
+
within 'span.page.current' do
|
40
|
+
page.should have_content '3'
|
41
|
+
end
|
42
|
+
within 'span.first' do
|
43
|
+
click_link '« First'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
within 'nav.pagination' do
|
48
|
+
within 'span.page.current' do
|
49
|
+
page.should have_content '1'
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
describe Kaminari::Configuration do
|
4
|
+
subject { Kaminari.config }
|
5
|
+
describe 'default_per_page' do
|
6
|
+
context 'by default' do
|
7
|
+
its(:default_per_page) { should == 25 }
|
8
|
+
end
|
9
|
+
context 'configured via config block' do
|
10
|
+
before do
|
11
|
+
Kaminari.configure {|c| c.default_per_page = 17}
|
12
|
+
end
|
13
|
+
its(:default_per_page) { should == 17 }
|
14
|
+
after do
|
15
|
+
Kaminari.configure {|c| c.default_per_page = 25}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'window' do
|
21
|
+
context 'by default' do
|
22
|
+
its(:window) { should == 4 }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'outer_window' do
|
27
|
+
context 'by default' do
|
28
|
+
its(:outer_window) { should == 0 }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'left' do
|
33
|
+
context 'by default' do
|
34
|
+
its(:left) { should == 0 }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'right' do
|
39
|
+
context 'by default' do
|
40
|
+
its(:right) { should == 0 }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe 'param_name' do
|
45
|
+
context 'by default' do
|
46
|
+
its(:param_name) { should == :page }
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'configured via config block' do
|
50
|
+
before do
|
51
|
+
Kaminari.configure {|c| c.param_name = lambda { :test } }
|
52
|
+
end
|
53
|
+
|
54
|
+
its(:param_name) { should == :test }
|
55
|
+
|
56
|
+
after do
|
57
|
+
Kaminari.configure {|c| c.param_name = :page }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/fake_app.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'action_controller/railtie'
|
3
|
+
require 'action_view/railtie'
|
4
|
+
|
5
|
+
# database
|
6
|
+
ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
|
7
|
+
ActiveRecord::Base.establish_connection('test')
|
8
|
+
|
9
|
+
# config
|
10
|
+
app = Class.new(Rails::Application)
|
11
|
+
app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
|
12
|
+
app.config.session_store :cookie_store, :key => "_myapp_session"
|
13
|
+
app.config.active_support.deprecation = :log
|
14
|
+
app.initialize!
|
15
|
+
|
16
|
+
# routes
|
17
|
+
app.routes.draw do
|
18
|
+
resources :users
|
19
|
+
end
|
20
|
+
|
21
|
+
# models
|
22
|
+
class User < ActiveRecord::Base
|
23
|
+
has_many :authorships
|
24
|
+
has_many :readerships
|
25
|
+
has_many :books_authored, :through => :authorships, :source => :book
|
26
|
+
has_many :books_read, :through => :readerships, :source => :book
|
27
|
+
|
28
|
+
def readers
|
29
|
+
User.joins(:books_read => :authors).where(:authors_books => {:id => self})
|
30
|
+
end
|
31
|
+
|
32
|
+
scope :by_name, order(:name)
|
33
|
+
scope :by_read_count, lambda {
|
34
|
+
cols = if connection.adapter_name == "PostgreSQL"
|
35
|
+
column_names.map { |column| %{"users"."#{column}"} }.join(", ")
|
36
|
+
else
|
37
|
+
'"users"."id"'
|
38
|
+
end
|
39
|
+
group(cols).select("count(readerships.id) AS read_count, #{cols}").order('read_count DESC')
|
40
|
+
}
|
41
|
+
end
|
42
|
+
class Authorship < ActiveRecord::Base
|
43
|
+
belongs_to :user
|
44
|
+
belongs_to :book
|
45
|
+
end
|
46
|
+
class Readership < ActiveRecord::Base
|
47
|
+
belongs_to :user
|
48
|
+
belongs_to :book
|
49
|
+
end
|
50
|
+
class Book < ActiveRecord::Base
|
51
|
+
has_many :authorships
|
52
|
+
has_many :readerships
|
53
|
+
has_many :authors, :through => :authorships, :source => :user
|
54
|
+
has_many :readers, :through => :readerships, :source => :user
|
55
|
+
end
|
56
|
+
|
57
|
+
# controllers
|
58
|
+
class ApplicationController < ActionController::Base; end
|
59
|
+
class UsersController < ApplicationController
|
60
|
+
def index
|
61
|
+
@users = User.page params[:page]
|
62
|
+
render :inline => <<-ERB
|
63
|
+
<%= @users.map(&:name).join("\n") %>
|
64
|
+
<%= paginate @users %>
|
65
|
+
ERB
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# helpers
|
70
|
+
Object.const_set(:ApplicationHelper, Module.new)
|
71
|
+
|
72
|
+
#migrations
|
73
|
+
class CreateAllTables < ActiveRecord::Migration
|
74
|
+
def self.up
|
75
|
+
create_table(:users) {|t| t.string :name; t.integer :age}
|
76
|
+
create_table(:books) {|t| t.string :title}
|
77
|
+
create_table(:readerships) {|t| t.integer :user_id; t.integer :book_id }
|
78
|
+
create_table(:authorships) {|t| t.integer :user_id; t.integer :book_id }
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
describe 'Kaminari::ActionViewExtension' do
|
4
|
+
before do
|
5
|
+
50.times {|i| User.create! :name => "user#{i}"}
|
6
|
+
end
|
7
|
+
describe '#paginate' do
|
8
|
+
before do
|
9
|
+
@users = User.page(1)
|
10
|
+
end
|
11
|
+
subject { helper.paginate @users, :params => {:controller => 'users', :action => 'index'} }
|
12
|
+
it { should be_a String }
|
13
|
+
|
14
|
+
context 'escaping the pagination for javascript' do
|
15
|
+
it 'should escape for javascript' do
|
16
|
+
lambda { escape_javascript(helper.paginate @users, :params => {:controller => 'users', :action => 'index'}) }.should_not raise_error
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#link_to_next_page' do
|
22
|
+
context 'having more page' do
|
23
|
+
before do
|
24
|
+
@users = User.page(1)
|
25
|
+
end
|
26
|
+
subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
|
27
|
+
it { should be_a String }
|
28
|
+
end
|
29
|
+
context 'the last page' do
|
30
|
+
before do
|
31
|
+
@users = User.page(2)
|
32
|
+
end
|
33
|
+
subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
|
34
|
+
it { should_not be }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2
|
+
include Kaminari::Helpers
|
3
|
+
|
4
|
+
describe 'Kaminari::Helpers::Paginator' do
|
5
|
+
let :template do
|
6
|
+
stub(r = Object.new) do
|
7
|
+
render.with_any_args
|
8
|
+
params { {} }
|
9
|
+
options { {} }
|
10
|
+
url_for {|h| "/foo?page=#{h[:page]}"}
|
11
|
+
end
|
12
|
+
r
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#params' do
|
16
|
+
before do
|
17
|
+
@paginator = Paginator.new(template, :params => {:controller => 'foo', :action => 'bar'})
|
18
|
+
end
|
19
|
+
subject { @paginator.page_tag(template).instance_variable_get('@params') }
|
20
|
+
it { should == {:controller => 'foo', :action => 'bar'} }
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#param_name' do
|
24
|
+
before do
|
25
|
+
@paginator = Paginator.new(template, :param_name => :pagina)
|
26
|
+
end
|
27
|
+
subject { @paginator.page_tag(template).instance_variable_get('@param_name') }
|
28
|
+
it { should == :pagina }
|
29
|
+
end
|
30
|
+
|
31
|
+
#TODO test somehow...
|
32
|
+
# describe '#tagify_links' do
|
33
|
+
# def tags_with(options)
|
34
|
+
# PaginationRenderer.new(template, options).tagify_links
|
35
|
+
# end
|
36
|
+
|
37
|
+
# context '1 page in total' do
|
38
|
+
# subject { tags_with :num_pages => 1, :current_page => 1 }
|
39
|
+
# it { should have(0).tags }
|
40
|
+
# end
|
41
|
+
|
42
|
+
# context '10 pages in total' do
|
43
|
+
# context 'first page' do
|
44
|
+
# subject { tags_with :num_pages => 10, :current_page => 1 }
|
45
|
+
# it { should_not contain_tag PrevLink }
|
46
|
+
# it { should contain_tag PrevSpan }
|
47
|
+
# it { should contain_tag CurrentPage }
|
48
|
+
# it { should_not contain_tag FirstPageLink }
|
49
|
+
# it { should contain_tag LastPageLink }
|
50
|
+
# it { should contain_tag PageLink }
|
51
|
+
# it { should contain_tag NextLink }
|
52
|
+
# it { should_not contain_tag NextSpan }
|
53
|
+
# it { should contain_tag TruncatedSpan }
|
54
|
+
# end
|
55
|
+
|
56
|
+
# context 'second page' do
|
57
|
+
# subject { tags_with :num_pages => 10, :current_page => 2 }
|
58
|
+
# it { should contain_tag PrevLink }
|
59
|
+
# it { should_not contain_tag PrevSpan }
|
60
|
+
# it { should contain_tag CurrentPage }
|
61
|
+
# it { should contain_tag FirstPageLink }
|
62
|
+
# it { should contain_tag LastPageLink }
|
63
|
+
# it { should contain_tag PageLink }
|
64
|
+
# it { should contain_tag NextLink }
|
65
|
+
# it { should_not contain_tag NextSpan }
|
66
|
+
# it { should contain_tag TruncatedSpan }
|
67
|
+
# end
|
68
|
+
|
69
|
+
# context 'third page' do
|
70
|
+
# subject { tags_with :num_pages => 10, :current_page => 3 }
|
71
|
+
# it { should contain_tag PrevLink }
|
72
|
+
# it { should_not contain_tag PrevSpan }
|
73
|
+
# it { should contain_tag CurrentPage }
|
74
|
+
# it { should contain_tag FirstPageLink }
|
75
|
+
# it { should contain_tag LastPageLink }
|
76
|
+
# it { should contain_tag PageLink }
|
77
|
+
# it { should contain_tag NextLink }
|
78
|
+
# it { should_not contain_tag NextSpan }
|
79
|
+
# it { should contain_tag TruncatedSpan }
|
80
|
+
# end
|
81
|
+
|
82
|
+
# context 'fourth page(no truncation)' do
|
83
|
+
# subject { tags_with :num_pages => 10, :current_page => 4 }
|
84
|
+
# it { should contain_tag PrevLink }
|
85
|
+
# it { should_not contain_tag PrevSpan }
|
86
|
+
# it { should contain_tag CurrentPage }
|
87
|
+
# it { should contain_tag FirstPageLink }
|
88
|
+
# it { should contain_tag LastPageLink }
|
89
|
+
# it { should contain_tag PageLink }
|
90
|
+
# it { should contain_tag NextLink }
|
91
|
+
# it { should_not contain_tag NextSpan }
|
92
|
+
# it { should_not contain_tag TruncatedSpan }
|
93
|
+
# end
|
94
|
+
|
95
|
+
# context 'seventh page(no truncation)' do
|
96
|
+
# subject { tags_with :num_pages => 10, :current_page => 7 }
|
97
|
+
# it { should contain_tag PrevLink }
|
98
|
+
# it { should_not contain_tag PrevSpan }
|
99
|
+
# it { should contain_tag CurrentPage }
|
100
|
+
# it { should contain_tag FirstPageLink }
|
101
|
+
# it { should contain_tag LastPageLink }
|
102
|
+
# it { should contain_tag PageLink }
|
103
|
+
# it { should contain_tag NextLink }
|
104
|
+
# it { should_not contain_tag NextSpan }
|
105
|
+
# it { should_not contain_tag TruncatedSpan }
|
106
|
+
# end
|
107
|
+
|
108
|
+
# context 'eighth page' do
|
109
|
+
# subject { tags_with :num_pages => 10, :current_page => 8 }
|
110
|
+
# it { should contain_tag PrevLink }
|
111
|
+
# it { should_not contain_tag PrevSpan }
|
112
|
+
# it { should contain_tag CurrentPage }
|
113
|
+
# it { should contain_tag FirstPageLink }
|
114
|
+
# it { should contain_tag LastPageLink }
|
115
|
+
# it { should contain_tag PageLink }
|
116
|
+
# it { should contain_tag NextLink }
|
117
|
+
# it { should_not contain_tag NextSpan }
|
118
|
+
# it { should contain_tag TruncatedSpan }
|
119
|
+
# end
|
120
|
+
|
121
|
+
# context 'last page' do
|
122
|
+
# subject { tags_with :num_pages => 10, :current_page => 10 }
|
123
|
+
# it { should contain_tag PrevLink }
|
124
|
+
# it { should_not contain_tag PrevSpan }
|
125
|
+
# it { should contain_tag CurrentPage }
|
126
|
+
# it { should contain_tag FirstPageLink }
|
127
|
+
# it { should_not contain_tag LastPageLink }
|
128
|
+
# it { should contain_tag PageLink }
|
129
|
+
# it { should_not contain_tag NextLink }
|
130
|
+
# it { should contain_tag NextSpan }
|
131
|
+
# it { should contain_tag TruncatedSpan }
|
132
|
+
# end
|
133
|
+
# end
|
134
|
+
# end
|
135
|
+
end
|