kaminari-rails4 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.gemtest +0 -0
  4. data/.gitignore +9 -0
  5. data/.rspec +2 -0
  6. data/.travis.yml +16 -0
  7. data/CHANGELOG +351 -0
  8. data/Gemfile +4 -0
  9. data/MIT-LICENSE +20 -0
  10. data/README.rdoc +300 -0
  11. data/Rakefile +42 -0
  12. data/app/helpers/kaminari_helper.rb +5 -0
  13. data/app/views/kaminari/_first_page.html.erb +11 -0
  14. data/app/views/kaminari/_first_page.html.haml +9 -0
  15. data/app/views/kaminari/_first_page.html.slim +10 -0
  16. data/app/views/kaminari/_gap.html.erb +8 -0
  17. data/app/views/kaminari/_gap.html.haml +8 -0
  18. data/app/views/kaminari/_gap.html.slim +9 -0
  19. data/app/views/kaminari/_last_page.html.erb +11 -0
  20. data/app/views/kaminari/_last_page.html.haml +9 -0
  21. data/app/views/kaminari/_last_page.html.slim +10 -0
  22. data/app/views/kaminari/_next_page.html.erb +11 -0
  23. data/app/views/kaminari/_next_page.html.haml +9 -0
  24. data/app/views/kaminari/_next_page.html.slim +10 -0
  25. data/app/views/kaminari/_page.html.erb +12 -0
  26. data/app/views/kaminari/_page.html.haml +10 -0
  27. data/app/views/kaminari/_page.html.slim +11 -0
  28. data/app/views/kaminari/_paginator.html.erb +23 -0
  29. data/app/views/kaminari/_paginator.html.haml +18 -0
  30. data/app/views/kaminari/_paginator.html.slim +19 -0
  31. data/app/views/kaminari/_prev_page.html.erb +11 -0
  32. data/app/views/kaminari/_prev_page.html.haml +9 -0
  33. data/app/views/kaminari/_prev_page.html.slim +10 -0
  34. data/config/locales/kaminari.yml +19 -0
  35. data/gemfiles/active_record_30.gemfile +7 -0
  36. data/gemfiles/active_record_31.gemfile +7 -0
  37. data/gemfiles/active_record_32.gemfile +10 -0
  38. data/gemfiles/active_record_40.gemfile +8 -0
  39. data/gemfiles/data_mapper_12.gemfile +15 -0
  40. data/gemfiles/mongo_mapper.gemfile +10 -0
  41. data/gemfiles/mongoid_24.gemfile +7 -0
  42. data/gemfiles/mongoid_30.gemfile +12 -0
  43. data/gemfiles/sinatra.gemfile +13 -0
  44. data/kaminari.gemspec +36 -0
  45. data/lib/generators/kaminari/config_generator.rb +16 -0
  46. data/lib/generators/kaminari/templates/kaminari_config.rb +10 -0
  47. data/lib/generators/kaminari/views_generator.rb +118 -0
  48. data/lib/kaminari.rb +38 -0
  49. data/lib/kaminari/config.rb +51 -0
  50. data/lib/kaminari/engine.rb +4 -0
  51. data/lib/kaminari/grape.rb +4 -0
  52. data/lib/kaminari/helpers/action_view_extension.rb +151 -0
  53. data/lib/kaminari/helpers/paginator.rb +186 -0
  54. data/lib/kaminari/helpers/sinatra_helpers.rb +144 -0
  55. data/lib/kaminari/helpers/tags.rb +95 -0
  56. data/lib/kaminari/hooks.rb +33 -0
  57. data/lib/kaminari/models/active_record_extension.rb +22 -0
  58. data/lib/kaminari/models/active_record_model_extension.rb +20 -0
  59. data/lib/kaminari/models/active_record_relation_methods.rb +29 -0
  60. data/lib/kaminari/models/array_extension.rb +58 -0
  61. data/lib/kaminari/models/configuration_methods.rb +48 -0
  62. data/lib/kaminari/models/data_mapper_collection_methods.rb +15 -0
  63. data/lib/kaminari/models/data_mapper_extension.rb +48 -0
  64. data/lib/kaminari/models/mongo_mapper_extension.rb +18 -0
  65. data/lib/kaminari/models/mongoid_criteria_methods.rb +23 -0
  66. data/lib/kaminari/models/mongoid_extension.rb +33 -0
  67. data/lib/kaminari/models/page_scope_methods.rb +70 -0
  68. data/lib/kaminari/models/plucky_criteria_methods.rb +18 -0
  69. data/lib/kaminari/railtie.rb +7 -0
  70. data/lib/kaminari/sinatra.rb +5 -0
  71. data/lib/kaminari/version.rb +3 -0
  72. data/spec/config/config_spec.rb +91 -0
  73. data/spec/fake_app/active_record/config.rb +3 -0
  74. data/spec/fake_app/active_record/models.rb +57 -0
  75. data/spec/fake_app/data_mapper/config.rb +1 -0
  76. data/spec/fake_app/data_mapper/models.rb +27 -0
  77. data/spec/fake_app/mongo_mapper/config.rb +2 -0
  78. data/spec/fake_app/mongo_mapper/models.rb +9 -0
  79. data/spec/fake_app/mongoid/config.rb +16 -0
  80. data/spec/fake_app/mongoid/models.rb +22 -0
  81. data/spec/fake_app/rails_app.rb +56 -0
  82. data/spec/fake_app/sinatra_app.rb +22 -0
  83. data/spec/fake_gem.rb +4 -0
  84. data/spec/helpers/action_view_extension_spec.rb +262 -0
  85. data/spec/helpers/helpers_spec.rb +135 -0
  86. data/spec/helpers/sinatra_helpers_spec.rb +170 -0
  87. data/spec/helpers/tags_spec.rb +140 -0
  88. data/spec/models/active_record/active_record_relation_methods_spec.rb +47 -0
  89. data/spec/models/active_record/default_per_page_spec.rb +32 -0
  90. data/spec/models/active_record/max_pages_spec.rb +23 -0
  91. data/spec/models/active_record/max_per_page_spec.rb +32 -0
  92. data/spec/models/active_record/scopes_spec.rb +242 -0
  93. data/spec/models/array_spec.rb +150 -0
  94. data/spec/models/data_mapper/data_mapper_spec.rb +179 -0
  95. data/spec/models/mongo_mapper/mongo_mapper_spec.rb +84 -0
  96. data/spec/models/mongoid/mongoid_spec.rb +126 -0
  97. data/spec/requests/users_spec.rb +53 -0
  98. data/spec/spec_helper.rb +33 -0
  99. data/spec/spec_helper_for_sinatra.rb +33 -0
  100. data/spec/support/database_cleaner.rb +16 -0
  101. data/spec/support/matchers.rb +52 -0
  102. metadata +300 -0
@@ -0,0 +1,170 @@
1
+ require 'spec_helper'
2
+
3
+ if defined? Sinatra
4
+ ERB_TEMPLATE_FOR_PAGINATE = <<EOT
5
+ <div>
6
+ <ul>
7
+ <% @users.each do |user| %>
8
+ <li class="user_info"><%= user.id %></li>
9
+ <% end %>
10
+ </ul>
11
+ <%= paginate @users, @options %>
12
+ </div>
13
+ EOT
14
+
15
+ ERB_TEMPLATE_FOR_NEXT_PAGE = <<EOT
16
+ <div>
17
+ <ul>
18
+ <% @users.each do |user| %>
19
+ <li class="user_info"><%= user.id %></li>
20
+ <% end %>
21
+ </ul>
22
+ <%= link_to_next_page(@users, "Next!", {:id => 'next_page_link'}.merge(@options || {})) %>
23
+ </div>
24
+ EOT
25
+
26
+ describe 'Kaminari::Helpers::SinatraHelper' do
27
+ before do
28
+ 50.times {|i| User.create! :name => "user#{i}"}
29
+ end
30
+
31
+ describe '#paginate' do
32
+ before do
33
+ mock_app do
34
+ register Kaminari::Helpers::SinatraHelpers
35
+ get '/users' do
36
+ @page = params[:page] || 1
37
+ @users = User.page(@page)
38
+ @options = {}
39
+ erb ERB_TEMPLATE_FOR_PAGINATE
40
+ end
41
+ end
42
+ end
43
+
44
+ context 'normal paginations with Sinatra' do
45
+ before { get '/users' }
46
+
47
+ it 'should have a navigation tag' do
48
+ last_document.search('nav.pagination').should_not be_empty
49
+ end
50
+
51
+ it 'should have pagination links' do
52
+ last_document.search('.page a').should have_at_least(1).items
53
+ last_document.search('.next a').should have_at_least(1).items
54
+ last_document.search('.last a').should have_at_least(1).items
55
+ end
56
+
57
+ it 'should point to current page' do
58
+ last_document.search('.current').text.should match(/1/)
59
+
60
+ get '/users?page=2'
61
+ last_document.search('.current').text.should match(/2/)
62
+ end
63
+
64
+ it 'should load 25 users' do
65
+ last_document.search('li.user_info').should have(25).items
66
+ end
67
+
68
+ it 'should preserve params' do
69
+ get '/users?foo=bar'
70
+ last_document.search('.page a').should(be_all do |elm|
71
+ elm.attribute('href').value =~ /foo=bar/
72
+ end)
73
+ end
74
+ end
75
+
76
+ context 'optional paginations with Sinatra' do
77
+ it 'should have 5 windows with 1 gap' do
78
+ mock_app do
79
+ register Kaminari::Helpers::SinatraHelpers
80
+ get '/users' do
81
+ @page = params[:page] || 1
82
+ @users = User.page(@page).per(5)
83
+ @options = {}
84
+ erb ERB_TEMPLATE_FOR_PAGINATE
85
+ end
86
+ end
87
+
88
+ get '/users'
89
+ last_document.search('.page').should have(6).items
90
+ last_document.search('.gap').should have(1).item
91
+ end
92
+
93
+ it 'should controll the inner window size' do
94
+ mock_app do
95
+ register Kaminari::Helpers::SinatraHelpers
96
+ get '/users' do
97
+ @page = params[:page] || 1
98
+ @users = User.page(@page).per(3)
99
+ @options = {:window => 10}
100
+ erb ERB_TEMPLATE_FOR_PAGINATE
101
+ end
102
+ end
103
+
104
+ get '/users'
105
+ last_document.search('.page').should have(12).items
106
+ last_document.search('.gap').should have(1).item
107
+ end
108
+
109
+ it 'should specify a page param name' do
110
+ mock_app do
111
+ register Kaminari::Helpers::SinatraHelpers
112
+ get '/users' do
113
+ @page = params[:page] || 1
114
+ @users = User.page(@page).per(3)
115
+ @options = {:param_name => :user_page}
116
+ erb ERB_TEMPLATE_FOR_PAGINATE
117
+ end
118
+ end
119
+
120
+ get '/users'
121
+ last_document.search('.page a').should(be_all do |elm|
122
+ elm.attribute('href').value =~ /user_page=\d+/
123
+ end)
124
+ end
125
+ end
126
+ end
127
+
128
+ describe '#link_to_next_page' do
129
+ before do
130
+ mock_app do
131
+ register Kaminari::Helpers::SinatraHelpers
132
+ get '/users' do
133
+ @page = params[:page] || 1
134
+ @users = User.page(@page)
135
+ erb ERB_TEMPLATE_FOR_NEXT_PAGE
136
+ end
137
+
138
+ get '/users_placeholder' do
139
+ @page = params[:page] || 1
140
+ @options = {:placeholder => %{<span id='no_next_page'>No Next Page</span>}}
141
+ @users = User.page(@page)
142
+ erb ERB_TEMPLATE_FOR_NEXT_PAGE
143
+ end
144
+ end
145
+ end
146
+
147
+ context 'having more page' do
148
+ it 'should have a more page link' do
149
+ get '/users'
150
+ last_document.search('a#next_page_link').should be_present
151
+ last_document.search('a#next_page_link').text.should match(/Next!/)
152
+ end
153
+ end
154
+
155
+ context 'the last page' do
156
+ it 'should not have a more page link' do
157
+ get '/users?page=2'
158
+ last_document.search('a#next_page_link').should be_empty
159
+ end
160
+
161
+ it 'should have a no more page notation using placeholder' do
162
+ get '/users_placeholder?page=2'
163
+ last_document.search('a#next_page_link').should be_empty
164
+ last_document.search('span#no_next_page').should be_present
165
+ last_document.search('span#no_next_page').text.should match(/No Next Page/)
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,140 @@
1
+ require 'spec_helper'
2
+ include Kaminari::Helpers
3
+
4
+ describe 'Kaminari::Helpers' do
5
+ describe 'Paginator' do
6
+ describe 'Paginator::PageProxy' do
7
+ describe '#current?' do
8
+ context 'current_page == page' do
9
+ subject { Paginator::PageProxy.new({:current_page => 26}, 26, nil) }
10
+ its(:current?) { should be_true }
11
+ end
12
+ context 'current_page != page' do
13
+ subject { Paginator::PageProxy.new({:current_page => 13}, 26, nil) }
14
+ its(:current?) { should_not be_true }
15
+ end
16
+ end
17
+
18
+ describe '#first?' do
19
+ context 'page == 1' do
20
+ subject { Paginator::PageProxy.new({:current_page => 26}, 1, nil) }
21
+ its(:first?) { should be_true }
22
+ end
23
+ context 'page != 1' do
24
+ subject { Paginator::PageProxy.new({:current_page => 13}, 2, nil) }
25
+ its(:first?) { should_not be_true }
26
+ end
27
+ end
28
+
29
+ describe '#last?' do
30
+ context 'current_page == page' do
31
+ subject { Paginator::PageProxy.new({:total_pages => 39}, 39, nil) }
32
+ its(:last?) { should be_true }
33
+ end
34
+ context 'current_page != page' do
35
+ subject { Paginator::PageProxy.new({:total_pages => 39}, 38, nil) }
36
+ its(:last?) { should_not be_true }
37
+ end
38
+ end
39
+
40
+ describe '#next?' do
41
+ context 'page == current_page + 1' do
42
+ subject { Paginator::PageProxy.new({:current_page => 52}, 53, nil) }
43
+ its(:next?) { should be_true }
44
+ end
45
+ context 'page != current_page + 1' do
46
+ subject { Paginator::PageProxy.new({:current_page => 52}, 77, nil) }
47
+ its(:next?) { should_not be_true }
48
+ end
49
+ end
50
+
51
+ describe '#prev?' do
52
+ context 'page == current_page - 1' do
53
+ subject { Paginator::PageProxy.new({:current_page => 77}, 76, nil) }
54
+ its(:prev?) { should be_true }
55
+ end
56
+ context 'page != current_page + 1' do
57
+ subject { Paginator::PageProxy.new({:current_page => 77}, 80, nil) }
58
+ its(:prev?) { should_not be_true }
59
+ end
60
+ end
61
+
62
+ describe '#left_outer?' do
63
+ context 'current_page == left' do
64
+ subject { Paginator::PageProxy.new({:left => 3}, 3, nil) }
65
+ its(:left_outer?) { should be_true }
66
+ end
67
+ context 'current_page == left + 1' do
68
+ subject { Paginator::PageProxy.new({:left => 3}, 4, nil) }
69
+ its(:left_outer?) { should_not be_true }
70
+ end
71
+ context 'current_page == left + 2' do
72
+ subject { Paginator::PageProxy.new({:left => 3}, 5, nil) }
73
+ its(:left_outer?) { should_not be_true }
74
+ end
75
+ end
76
+
77
+ describe '#right_outer?' do
78
+ context 'total_pages - page > right' do
79
+ subject { Paginator::PageProxy.new({:total_pages => 10, :right => 3}, 6, nil) }
80
+ its(:right_outer?) { should_not be_true }
81
+ end
82
+ context 'total_pages - page == right' do
83
+ subject { Paginator::PageProxy.new({:total_pages => 10, :right => 3}, 7, nil) }
84
+ its(:right_outer?) { should_not be_true }
85
+ end
86
+ context 'total_pages - page < right' do
87
+ subject { Paginator::PageProxy.new({:total_pages => 10, :right => 3}, 8, nil) }
88
+ its(:right_outer?) { should be_true }
89
+ end
90
+ end
91
+
92
+ describe '#inside_window?' do
93
+ context 'page > current_page' do
94
+ context 'page - current_page > window' do
95
+ subject { Paginator::PageProxy.new({:current_page => 4, :window => 5}, 10, nil) }
96
+ its(:inside_window?) { should_not be_true }
97
+ end
98
+ context 'page - current_page == window' do
99
+ subject { Paginator::PageProxy.new({:current_page => 4, :window => 6}, 10, nil) }
100
+ its(:inside_window?) { should be_true }
101
+ end
102
+ context 'page - current_page < window' do
103
+ subject { Paginator::PageProxy.new({:current_page => 4, :window => 7}, 10, nil) }
104
+ its(:inside_window?) { should be_true }
105
+ end
106
+ end
107
+ context 'current_page > page' do
108
+ context 'current_page - page > window' do
109
+ subject { Paginator::PageProxy.new({:current_page => 15, :window => 4}, 10, nil) }
110
+ its(:inside_window?) { should_not be_true }
111
+ end
112
+ context 'current_page - page == window' do
113
+ subject { Paginator::PageProxy.new({:current_page => 15, :window => 5}, 10, nil) }
114
+ its(:inside_window?) { should be_true }
115
+ end
116
+ context 'current_page - page < window' do
117
+ subject { Paginator::PageProxy.new({:current_page => 15, :window => 6}, 10, nil) }
118
+ its(:inside_window?) { should be_true }
119
+ end
120
+ end
121
+ end
122
+ describe '#was_truncated?' do
123
+ before do
124
+ stub(@template = Object.new) do
125
+ options { {} }
126
+ params { {} }
127
+ end
128
+ end
129
+ context 'last.is_a? Gap' do
130
+ subject { Paginator::PageProxy.new({}, 10, Gap.new(@template)) }
131
+ its(:was_truncated?) { should be_true }
132
+ end
133
+ context 'last.is not a Gap' do
134
+ subject { Paginator::PageProxy.new({}, 10, Page.new(@template)) }
135
+ its(:was_truncated?) { should_not be_true }
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ if defined? ActiveRecord
4
+ describe Kaminari::ActiveRecordRelationMethods do
5
+ describe '#total_count' do
6
+ before do
7
+ @author = User.create! :name => 'author'
8
+ @author2 = User.create! :name => 'author2'
9
+ @author3 = User.create! :name => 'author3'
10
+ @books = 2.times.map {|i| @author.books_authored.create!(:title => "title%03d" % i) }
11
+ @books2 = 3.times.map {|i| @author2.books_authored.create!(:title => "title%03d" % i) }
12
+ @books3 = 4.times.map {|i| @author3.books_authored.create!(:title => "subject%03d" % i) }
13
+ @readers = 4.times.map { User.create! :name => 'reader' }
14
+ @books.each {|book| book.readers << @readers }
15
+ end
16
+
17
+ context "when the scope includes an order which references a generated column" do
18
+ it "should successfully count the results" do
19
+ @author.readers.by_read_count.page(1).total_count.should == @readers.size
20
+ end
21
+ end
22
+ context "when the scope use conditions on includes" do
23
+ it "should keep includes and successfully count the results" do
24
+ # Only @author and @author2 have books titled with the title00x partern
25
+ User.includes(:books_authored).where("books.title LIKE 'title00%'").page(1).total_count.should == 2
26
+ end
27
+ end
28
+ context "when total_count receives options" do
29
+ it "should return a distinct total count" do
30
+ User.page(1).total_count(:name, :distinct => true).should == 4
31
+ end
32
+ end
33
+ context "when count receives options" do
34
+ it "should return a distinct set by column" do
35
+ User.page(1).count(:name, :distinct => true).should == 4
36
+ end
37
+ end
38
+ context "when the scope returns an ActiveSupport::OrderedHash" do
39
+ it "should not throw exception by passing options to count" do
40
+ lambda {
41
+ @author.readers.by_read_count.page(1).total_count(:name, :distinct => true)
42
+ }.should_not raise_exception
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ if defined? ActiveRecord
4
+
5
+ describe 'default per_page' do
6
+ describe 'AR::Base' do
7
+ subject { ActiveRecord::Base }
8
+ it { should_not respond_to :paginates_per }
9
+ end
10
+
11
+ subject { User.page 0 }
12
+
13
+ context 'by default' do
14
+ its(:limit_value) { should == 25 }
15
+ end
16
+
17
+ context 'when explicitly set via paginates_per' do
18
+ before { User.paginates_per 1326 }
19
+ its(:limit_value) { should == 1326 }
20
+ after { User.paginates_per nil }
21
+ end
22
+
23
+ describe "default per_page value's independency per model" do
24
+ context "when User's default per_page was changed" do
25
+ before { User.paginates_per 1326 }
26
+ subject { Book.page 0 }
27
+ its(:limit_value) { should == 25 }
28
+ after { User.paginates_per nil }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ if defined? ActiveRecord
4
+
5
+ describe 'max pages' do
6
+ describe 'AR::Base' do
7
+ subject { ActiveRecord::Base }
8
+ it { should_not respond_to :max_pages_per }
9
+ end
10
+
11
+ subject { User.page 0 }
12
+
13
+ context 'by default' do
14
+ its(:max_pages) { should == nil }
15
+ end
16
+
17
+ context 'when explicitly set via max_pages_per' do
18
+ before { User.max_pages_per 3 }
19
+ its(:max_pages) { should == 3 }
20
+ after { User.max_pages_per nil }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ if defined? ActiveRecord
4
+
5
+ describe 'max per_page' do
6
+ describe 'AR::Base' do
7
+ subject { ActiveRecord::Base }
8
+ it { should_not respond_to :max_paginates_per }
9
+ end
10
+
11
+ subject { User.page(0).per(100) }
12
+
13
+ context 'by default' do
14
+ its(:limit_value) { should == 100 }
15
+ end
16
+
17
+ context 'when explicitly set via max_paginates_per' do
18
+ before { User.max_paginates_per 10 }
19
+ its(:limit_value) { should == 10 }
20
+ after { User.max_paginates_per nil }
21
+ end
22
+
23
+ describe "max per_page value's independency per model" do
24
+ context "when User's max per_page was changed" do
25
+ before { User.max_paginates_per 10 }
26
+ subject { Book.page(0).per(100) }
27
+ its(:limit_value) { should == 100 }
28
+ after { User.max_paginates_per nil }
29
+ end
30
+ end
31
+ end
32
+ end