wulffeld_kaminari 0.12.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/.document +5 -0
  2. data/.gemtest +0 -0
  3. data/.gitignore +7 -0
  4. data/.rspec +2 -0
  5. data/CHANGELOG +245 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +20 -0
  8. data/README.rdoc +231 -0
  9. data/Rakefile +22 -0
  10. data/app/views/kaminari/_first_page.html.erb +11 -0
  11. data/app/views/kaminari/_first_page.html.haml +9 -0
  12. data/app/views/kaminari/_first_page.html.slim +10 -0
  13. data/app/views/kaminari/_gap.html.erb +8 -0
  14. data/app/views/kaminari/_gap.html.haml +8 -0
  15. data/app/views/kaminari/_gap.html.slim +9 -0
  16. data/app/views/kaminari/_last_page.html.erb +11 -0
  17. data/app/views/kaminari/_last_page.html.haml +9 -0
  18. data/app/views/kaminari/_last_page.html.slim +10 -0
  19. data/app/views/kaminari/_next_page.html.erb +11 -0
  20. data/app/views/kaminari/_next_page.html.haml +9 -0
  21. data/app/views/kaminari/_next_page.html.slim +10 -0
  22. data/app/views/kaminari/_page.html.erb +12 -0
  23. data/app/views/kaminari/_page.html.haml +10 -0
  24. data/app/views/kaminari/_page.html.slim +11 -0
  25. data/app/views/kaminari/_paginator.html.erb +23 -0
  26. data/app/views/kaminari/_paginator.html.haml +18 -0
  27. data/app/views/kaminari/_paginator.html.slim +19 -0
  28. data/app/views/kaminari/_prev_page.html.erb +11 -0
  29. data/app/views/kaminari/_prev_page.html.haml +9 -0
  30. data/app/views/kaminari/_prev_page.html.slim +10 -0
  31. data/config/locales/kaminari.yml +10 -0
  32. data/kaminari.gemspec +35 -0
  33. data/lib/generators/kaminari/config_generator.rb +16 -0
  34. data/lib/generators/kaminari/templates/kaminari_config.rb +8 -0
  35. data/lib/generators/kaminari/views_generator.rb +103 -0
  36. data/lib/kaminari/config.rb +41 -0
  37. data/lib/kaminari/engine.rb +4 -0
  38. data/lib/kaminari/helpers/action_view_extension.rb +50 -0
  39. data/lib/kaminari/helpers/paginator.rb +154 -0
  40. data/lib/kaminari/helpers/tags.rb +95 -0
  41. data/lib/kaminari/models/active_record_extension.rb +25 -0
  42. data/lib/kaminari/models/active_record_relation_methods.rb +26 -0
  43. data/lib/kaminari/models/array_extension.rb +42 -0
  44. data/lib/kaminari/models/configuration_methods.rb +20 -0
  45. data/lib/kaminari/models/mongo_mapper_extension.rb +18 -0
  46. data/lib/kaminari/models/mongoid_criteria_methods.rb +18 -0
  47. data/lib/kaminari/models/mongoid_extension.rb +31 -0
  48. data/lib/kaminari/models/page_scope_methods.rb +36 -0
  49. data/lib/kaminari/models/plucky_criteria_methods.rb +18 -0
  50. data/lib/kaminari/railtie.rb +36 -0
  51. data/lib/kaminari/version.rb +3 -0
  52. data/lib/kaminari.rb +2 -0
  53. data/spec/acceptance/acceptance_helper.rb +5 -0
  54. data/spec/acceptance/support/helpers.rb +5 -0
  55. data/spec/acceptance/support/paths.rb +9 -0
  56. data/spec/acceptance/users_spec.rb +53 -0
  57. data/spec/config/config_spec.rb +61 -0
  58. data/spec/fake_app.rb +80 -0
  59. data/spec/helpers/action_view_extension_spec.rb +37 -0
  60. data/spec/helpers/helpers_spec.rb +135 -0
  61. data/spec/helpers/tags_spec.rb +140 -0
  62. data/spec/models/active_record_relation_methods_spec.rb +28 -0
  63. data/spec/models/array_spec.rb +105 -0
  64. data/spec/models/default_per_page_spec.rb +29 -0
  65. data/spec/models/mongo_mapper_spec.rb +79 -0
  66. data/spec/models/mongoid_spec.rb +72 -0
  67. data/spec/models/scopes_spec.rb +149 -0
  68. data/spec/spec_helper.rb +28 -0
  69. data/spec/support/database_cleaner.rb +13 -0
  70. data/spec/support/matchers.rb +46 -0
  71. metadata +311 -0
@@ -0,0 +1,140 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
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({:num_pages => 39}, 39, nil) }
32
+ its(:last?) { should be_true }
33
+ end
34
+ context 'current_page != page' do
35
+ subject { Paginator::PageProxy.new({:num_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 'num_pages - page > right' do
79
+ subject { Paginator::PageProxy.new({:num_pages => 10, :right => 3}, 6, nil) }
80
+ its(:right_outer?) { should_not be_true }
81
+ end
82
+ context 'num_pages - page == right' do
83
+ subject { Paginator::PageProxy.new({:num_pages => 10, :right => 3}, 7, nil) }
84
+ its(:right_outer?) { should_not be_true }
85
+ end
86
+ context 'num_pages - page < right' do
87
+ subject { Paginator::PageProxy.new({:num_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,28 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+
3
+ describe Kaminari::ActiveRecordRelationMethods do
4
+ describe '#total_count' do
5
+ before do
6
+ @author = User.create! :name => 'author'
7
+ @author2 = User.create! :name => 'author2'
8
+ @author3 = User.create! :name => 'author3'
9
+ @books = 2.times.map {|i| @author.books_authored.create!(:title => "title%03d" % i) }
10
+ @books2 = 3.times.map {|i| @author2.books_authored.create!(:title => "title%03d" % i) }
11
+ @books3 = 4.times.map {|i| @author3.books_authored.create!(:title => "subject%03d" % i) }
12
+ @readers = 4.times.map { User.create! :name => 'reader' }
13
+ @books.each {|book| book.readers << @readers }
14
+ end
15
+
16
+ context "when the scope includes an order which references a generated column" do
17
+ it "should successfully count the results" do
18
+ @author.readers.by_read_count.page(1).total_count.should == @readers.size
19
+ end
20
+ end
21
+ context "when the scope use conditions on includes" do
22
+ it "should keep includes and successfully count the results" do
23
+ # Only @author and @author2 have books titled with the title00x partern
24
+ User.includes(:books_authored).where("books.title LIKE 'title00%'").page(1).total_count.should == 2
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,105 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+
3
+ describe Kaminari::PaginatableArray do
4
+ let(:array) { Kaminari::PaginatableArray.new((1..100).to_a) }
5
+ describe '#page' do
6
+ shared_examples_for 'the first page of array' do
7
+ it { should have(25).users }
8
+ its(:first) { should == 1 }
9
+ end
10
+
11
+ shared_examples_for 'blank array page' do
12
+ it { should have(0).items }
13
+ end
14
+
15
+ context 'page 1' do
16
+ subject { array.page 1 }
17
+ it_should_behave_like 'the first page of array'
18
+ end
19
+
20
+ context 'page 2' do
21
+ subject { array.page 2 }
22
+ it { should have(25).users }
23
+ its(:first) { should == 26 }
24
+ end
25
+
26
+ context 'page without an argument' do
27
+ subject { array.page }
28
+ it_should_behave_like 'the first page of array'
29
+ end
30
+
31
+ context 'page < 1' do
32
+ subject { array.page 0 }
33
+ it_should_behave_like 'the first page of array'
34
+ end
35
+
36
+ context 'page > max page' do
37
+ subject { array.page 5 }
38
+ it_should_behave_like 'blank array page'
39
+ end
40
+ end
41
+
42
+ describe '#per' do
43
+ context 'page 1 per 5' do
44
+ subject { array.page(1).per(5) }
45
+ it { should have(5).users }
46
+ its(:first) { should == 1 }
47
+ end
48
+ end
49
+
50
+ describe '#num_pages' do
51
+ context 'per 25 (default)' do
52
+ subject { array.page }
53
+ its(:num_pages) { should == 4 }
54
+ end
55
+
56
+ context 'per 7' do
57
+ subject { array.page(2).per(7) }
58
+ its(:num_pages) { should == 15 }
59
+ end
60
+
61
+ context 'per 65536' do
62
+ subject { array.page(50).per(65536) }
63
+ its(:num_pages) { should == 1 }
64
+ end
65
+
66
+ context 'per 0 (using default)' do
67
+ subject { array.page(50).per(0) }
68
+ its(:num_pages) { should == 4 }
69
+ end
70
+
71
+ context 'per -1 (using default)' do
72
+ subject { array.page(5).per(-1) }
73
+ its(:num_pages) { should == 4 }
74
+ end
75
+
76
+ context 'per "String value that can not be converted into Number" (using default)' do
77
+ subject { array.page(5).per('aho') }
78
+ its(:num_pages) { should == 4 }
79
+ end
80
+ end
81
+
82
+ describe '#current_page' do
83
+ context 'page 1' do
84
+ subject { array.page }
85
+ its(:current_page) { should == 1 }
86
+ end
87
+
88
+ context 'page 2' do
89
+ subject { array.page(2).per 3 }
90
+ its(:current_page) { should == 2 }
91
+ end
92
+ end
93
+
94
+ describe '#count' do
95
+ context 'page 1' do
96
+ subject { array.page }
97
+ its(:count) { should == 25 }
98
+ end
99
+
100
+ context 'page 2' do
101
+ subject { array.page 2 }
102
+ its(:count) { should == 25 }
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+
3
+ describe 'default per_page' do
4
+ describe 'AR::Base' do
5
+ subject { ActiveRecord::Base }
6
+ it { should_not respond_to :paginates_per }
7
+ end
8
+
9
+ subject { User.page 0 }
10
+
11
+ context 'by default' do
12
+ its(:limit_value) { should == 25 }
13
+ end
14
+
15
+ context 'when explicitly set via paginates_per' do
16
+ before { User.paginates_per 1326 }
17
+ its(:limit_value) { should == 1326 }
18
+ after { User.paginates_per nil }
19
+ end
20
+
21
+ describe "default per_page value's independency per model" do
22
+ context "when User's default per_page was changed" do
23
+ before { User.paginates_per 1326 }
24
+ subject { Book.page 0 }
25
+ its(:limit_value) { should == 25 }
26
+ after { User.paginates_per nil }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,79 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+ require 'mongo_mapper'
3
+ require 'kaminari/models/mongo_mapper_extension'
4
+
5
+ describe Kaminari::MongoMapperExtension do
6
+ before :all do
7
+ MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
8
+ MongoMapper.database = "kaminari_test"
9
+ class Developer
10
+ include ::MongoMapper::Document
11
+ key :salary, Integer
12
+ end
13
+ end
14
+ before do
15
+ stub(subject).count { 300 } # in order to avoid DB access...
16
+ end
17
+
18
+ describe '#page' do
19
+ context 'page 1' do
20
+ subject { Developer.page(1) }
21
+ it { should be_a Plucky::Query }
22
+ its(:current_page) { should == 1 }
23
+ its(:limit_value) { should == 25 }
24
+ its(:num_pages) { should == 12 }
25
+ it { should skip(0) }
26
+ end
27
+
28
+ context 'page 2' do
29
+ subject { Developer.page 2 }
30
+ it { should be_a Plucky::Query }
31
+ its(:current_page) { should == 2 }
32
+ its(:limit_value) { should == 25 }
33
+ its(:num_pages) { should == 12 }
34
+ it { should skip 25 }
35
+ end
36
+
37
+ context 'page "foobar"' do
38
+ subject { Developer.page 'foobar' }
39
+ it { should be_a Plucky::Query }
40
+ its(:current_page) { should == 1 }
41
+ its(:limit_value) { should == 25 }
42
+ its(:num_pages) { should == 12 }
43
+ it { should skip 0 }
44
+ end
45
+
46
+ context 'with criteria before' do
47
+ it "should have the proper criteria source" do
48
+ Developer.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
49
+ end
50
+
51
+ subject { Developer.where(:salary => 1).page 2 }
52
+ its(:current_page) { should == 2 }
53
+ its(:limit_value) { should == 25 }
54
+ its(:num_pages) { should == 12 }
55
+ it { should skip 25 }
56
+ end
57
+
58
+ context 'with criteria after' do
59
+ it "should have the proper criteria source" do
60
+ Developer.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
61
+ end
62
+
63
+ subject { Developer.page(2).where(:salary => 1) }
64
+ its(:current_page) { should == 2 }
65
+ its(:limit_value) { should == 25 }
66
+ its(:num_pages) { should == 12 }
67
+ it { should skip 25 }
68
+ end
69
+ end
70
+
71
+ describe '#per' do
72
+ subject { Developer.page(2).per(10) }
73
+ it { should be_a Plucky::Query }
74
+ its(:current_page) { should == 2 }
75
+ its(:limit_value) { should == 10 }
76
+ its(:num_pages) { should == 30 }
77
+ it { should skip 10 }
78
+ end
79
+ end
@@ -0,0 +1,72 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+ require 'mongoid'
3
+ require 'kaminari/models/mongoid_extension'
4
+
5
+ describe Kaminari::MongoidExtension do
6
+ before :all do
7
+ class Developer
8
+ include ::Mongoid::Document
9
+ field :salary, :type => Integer
10
+ end
11
+ end
12
+ before do
13
+ stub(subject).count { 300 } # in order to avoid DB access...
14
+ end
15
+
16
+ describe '#page' do
17
+ context 'page 1' do
18
+ subject { Developer.page 1 }
19
+ it { should be_a Mongoid::Criteria }
20
+ its(:current_page) { should == 1 }
21
+ its(:limit_value) { should == 25 }
22
+ its(:num_pages) { should == 12 }
23
+ it { should skip(0) }
24
+ end
25
+
26
+ context 'page 2' do
27
+ subject { Developer.page 2 }
28
+ it { should be_a Mongoid::Criteria }
29
+ its(:current_page) { should == 2 }
30
+ its(:limit_value) { should == 25 }
31
+ its(:num_pages) { should == 12 }
32
+ it { should skip 25 }
33
+ end
34
+
35
+ context 'page "foobar"' do
36
+ subject { Developer.page 'foobar' }
37
+ it { should be_a Mongoid::Criteria }
38
+ its(:current_page) { should == 1 }
39
+ its(:limit_value) { should == 25 }
40
+ its(:num_pages) { should == 12 }
41
+ it { should skip 0 }
42
+ end
43
+
44
+ context 'with criteria before' do
45
+ subject { Developer.where(:salary => 1).page 2 }
46
+ its(:selector) { should == {:salary => 1} }
47
+ its(:current_page) { should == 2 }
48
+ its(:limit_value) { should == 25 }
49
+ its(:num_pages) { should == 12 }
50
+ it { should skip 25 }
51
+ end
52
+
53
+ context 'with criteria after' do
54
+ subject { Developer.page(2).where(:salary => 1) }
55
+ its(:selector) { should == {:salary => 1} }
56
+ its(:current_page) { should == 2 }
57
+ its(:limit_value) { should == 25 }
58
+ its(:num_pages) { should == 12 }
59
+ it { should skip 25 }
60
+ end
61
+
62
+ end
63
+
64
+ describe '#per' do
65
+ subject { Developer.page(2).per(10) }
66
+ it { should be_a Mongoid::Criteria }
67
+ its(:current_page) { should == 2 }
68
+ its(:limit_value) { should == 10 }
69
+ its(:num_pages) { should == 30 }
70
+ it { should skip 10 }
71
+ end
72
+ end
@@ -0,0 +1,149 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+
3
+ describe Kaminari::ActiveRecordExtension do
4
+ before :all do
5
+ 1.upto(100) {|i| User.create! :name => "user#{'%03d' % i}", :age => (i / 10)}
6
+ end
7
+
8
+ describe '#page' do
9
+ shared_examples_for 'the first page' do
10
+ it { should have(25).users }
11
+ its('first.name') { should == 'user001' }
12
+ end
13
+
14
+ shared_examples_for 'blank page' do
15
+ it { should have(0).users }
16
+ end
17
+
18
+ context 'page 1' do
19
+ subject { User.page 1 }
20
+ it_should_behave_like 'the first page'
21
+ end
22
+
23
+ context 'page 2' do
24
+ subject { User.page 2 }
25
+ it { should have(25).users }
26
+ its('first.name') { should == 'user026' }
27
+ end
28
+
29
+ context 'page without an argument' do
30
+ subject { User.page }
31
+ it_should_behave_like 'the first page'
32
+ end
33
+
34
+ context 'page < 1' do
35
+ subject { User.page 0 }
36
+ it_should_behave_like 'the first page'
37
+ end
38
+
39
+ context 'page > max page' do
40
+ subject { User.page 5 }
41
+ it_should_behave_like 'blank page'
42
+ end
43
+
44
+ describe 'ensure #order_values is preserved' do
45
+ subject { User.order('id').page 1 }
46
+ its(:order_values) { should == ['id'] }
47
+ end
48
+ end
49
+
50
+ describe '#per' do
51
+ context 'page 1 per 5' do
52
+ subject { User.page(1).per(5) }
53
+ it { should have(5).users }
54
+ its('first.name') { should == 'user001' }
55
+ end
56
+ end
57
+
58
+ describe '#num_pages' do
59
+ context 'per 25 (default)' do
60
+ subject { User.page }
61
+ its(:num_pages) { should == 4 }
62
+ end
63
+
64
+ context 'per 7' do
65
+ subject { User.page(2).per(7) }
66
+ its(:num_pages) { should == 15 }
67
+ end
68
+
69
+ context 'per 65536' do
70
+ subject { User.page(50).per(65536) }
71
+ its(:num_pages) { should == 1 }
72
+ end
73
+
74
+ context 'per 0 (using default)' do
75
+ subject { User.page(50).per(0) }
76
+ its(:num_pages) { should == 4 }
77
+ end
78
+
79
+ context 'per -1 (using default)' do
80
+ subject { User.page(5).per(-1) }
81
+ its(:num_pages) { should == 4 }
82
+ end
83
+
84
+ context 'per "String value that can not be converted into Number" (using default)' do
85
+ subject { User.page(5).per('aho') }
86
+ its(:num_pages) { should == 4 }
87
+ end
88
+ end
89
+
90
+ describe '#current_page' do
91
+ context 'page 1' do
92
+ subject { User.page }
93
+ its(:current_page) { should == 1 }
94
+ end
95
+
96
+ context 'page 2' do
97
+ subject { User.page(2).per 3 }
98
+ its(:current_page) { should == 2 }
99
+ end
100
+ end
101
+
102
+ describe '#first_page?' do
103
+ context 'on first page' do
104
+ subject { User.page(1).per(10) }
105
+ its(:first_page?) { should == true }
106
+ end
107
+
108
+ context 'not on first page' do
109
+ subject { User.page(5).per(10) }
110
+ its(:first_page?) { should == false }
111
+ end
112
+ end
113
+
114
+ describe '#last_page?' do
115
+ context 'on last page' do
116
+ subject { User.page(10).per(10) }
117
+ its(:last_page?) { should == true }
118
+ end
119
+
120
+ context 'not on last page' do
121
+ subject { User.page(1).per(10) }
122
+ its(:last_page?) { should == false }
123
+ end
124
+ end
125
+
126
+ describe '#count' do
127
+ context 'page 1' do
128
+ subject { User.page }
129
+ its(:count) { should == 25 }
130
+ end
131
+
132
+ context 'page 2' do
133
+ subject { User.page 2 }
134
+ its(:count) { should == 25 }
135
+ end
136
+ end
137
+
138
+ context 'chained with .group' do
139
+ subject { User.group('age').page(2).per 5 }
140
+ # 0..10
141
+ its(:total_count) { should == 11 }
142
+ its(:num_pages) { should == 3 }
143
+ end
144
+
145
+ context 'activerecord descendants' do
146
+ subject { ActiveRecord::Base.descendants }
147
+ its(:length) { should_not == 0 }
148
+ end
149
+ end
@@ -0,0 +1,28 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rails'
4
+ require 'mongoid'
5
+ require 'kaminari'
6
+ require 'database_cleaner'
7
+ # Ensure we use 'syck' instead of 'psych' in 1.9.2
8
+ # RubyGems >= 1.5.0 uses 'psych' on 1.9.2, but
9
+ # Psych does not yet support YAML 1.1 merge keys.
10
+ # Merge keys is often used in mongoid.yml
11
+ # See: http://redmine.ruby-lang.org/issues/show/4300
12
+ if RUBY_VERSION >= '1.9.2'
13
+ YAML::ENGINE.yamler = 'syck'
14
+ end
15
+ require File.join(File.dirname(__FILE__), 'fake_app')
16
+
17
+ require 'rspec/rails'
18
+ # Requires supporting files with custom matchers and macros, etc,
19
+ # in ./support/ and its subdirectories.
20
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
21
+
22
+ RSpec.configure do |config|
23
+ config.mock_with :rr
24
+ config.before :all do
25
+ # ActiveRecord::Base.connection.execute 'CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255))' unless ActiveRecord::Base.connection.table_exists? 'users'
26
+ CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'users'
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ DatabaseCleaner.strategy = :transaction
2
+
3
+ RSpec.configure do |config|
4
+ config.before :suite do
5
+ DatabaseCleaner.clean_with :truncation
6
+ end
7
+ config.before :each do
8
+ DatabaseCleaner.start
9
+ end
10
+ config.after :each do
11
+ DatabaseCleaner.clean
12
+ end
13
+ end
@@ -0,0 +1,46 @@
1
+ RSpec::Matchers.define :contain_tag do |klass|
2
+ match do |collection|
3
+ if @num.blank?
4
+ collection.any? {|tag| tag.is_a? klass}
5
+ else
6
+ (@count = collection.count {|tag| tag.is_a? klass}) == @num
7
+ end
8
+ end
9
+
10
+ def count(num)
11
+ @num = num
12
+ self
13
+ end
14
+
15
+ description do
16
+ "contain #{@num || 'any'} instance(s) of #{klass.name}"
17
+ end
18
+ failure_message_for_should do |collection|
19
+ "expected #{@num || 'any'} instance(s) of #{klass.name} but was #{@count}"
20
+ end
21
+ end
22
+
23
+ RSpec::Matchers.define :contain_tag_old do |count|
24
+ match do |collection|
25
+ (@count = collection.count {|tag| tag.is_a? @klass}) == count
26
+ end
27
+
28
+ def instance_of(klass)
29
+ @klass = klass
30
+ self
31
+ end
32
+ alias :instances_of :instance_of
33
+
34
+ description do
35
+ "contain #{count || 'any'} instance(s) of #{@klass.name}"
36
+ end
37
+ failure_message_for_should do |collection|
38
+ "expected #{count || 'any'} instance(s) of #{@klass.name} but was #{@count}"
39
+ end
40
+ end
41
+
42
+ RSpec::Matchers.define :skip do |num|
43
+ match do |criteria|
44
+ criteria.instance_variable_get('@options')[:skip] == num
45
+ end
46
+ end