trailblazer-finder 0.1.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop-https---raw-githubusercontent-com-trailblazer-meta-master-rubocop-yml +101 -0
  4. data/.rubocop.yml +3 -43
  5. data/.rubocop_todo.yml +42 -196
  6. data/.travis.yml +5 -8
  7. data/Gemfile +2 -2
  8. data/LICENSE.txt +7 -21
  9. data/README.md +177 -292
  10. data/Rakefile +12 -12
  11. data/lib/trailblazer/finder.rb +48 -23
  12. data/lib/trailblazer/finder/activity/find.rb +57 -0
  13. data/lib/trailblazer/finder/activity/prepare.rb +29 -0
  14. data/lib/trailblazer/finder/activity/prepare/adapters.rb +66 -0
  15. data/lib/trailblazer/finder/activity/prepare/entity.rb +32 -0
  16. data/lib/trailblazer/finder/activity/prepare/filters.rb +36 -0
  17. data/lib/trailblazer/finder/activity/prepare/paging.rb +49 -0
  18. data/lib/trailblazer/finder/activity/prepare/params.rb +33 -0
  19. data/lib/trailblazer/finder/activity/prepare/properties.rb +47 -0
  20. data/lib/trailblazer/finder/activity/prepare/sorting.rb +57 -0
  21. data/lib/trailblazer/finder/activity/process.rb +18 -0
  22. data/lib/trailblazer/finder/activity/process/adapters.rb +48 -0
  23. data/lib/trailblazer/finder/activity/process/filters.rb +28 -0
  24. data/lib/trailblazer/finder/activity/process/paging.rb +26 -0
  25. data/lib/trailblazer/finder/activity/process/predicates.rb +30 -0
  26. data/lib/trailblazer/finder/activity/process/sorting.rb +26 -0
  27. data/lib/trailblazer/finder/adapters/active_record/paging.rb +9 -8
  28. data/lib/trailblazer/finder/adapters/active_record/predicates.rb +78 -17
  29. data/lib/trailblazer/finder/adapters/active_record/sorting.rb +14 -12
  30. data/lib/trailblazer/finder/adapters/basic/paging.rb +21 -0
  31. data/lib/trailblazer/finder/adapters/basic/predicates.rb +115 -0
  32. data/lib/trailblazer/finder/adapters/basic/sorting.rb +40 -0
  33. data/lib/trailblazer/finder/adapters/kaminari/paging.rb +21 -0
  34. data/lib/trailblazer/finder/adapters/sequel/paging.rb +9 -8
  35. data/lib/trailblazer/finder/adapters/sequel/predicates.rb +78 -17
  36. data/lib/trailblazer/finder/adapters/sequel/sorting.rb +17 -15
  37. data/lib/trailblazer/finder/adapters/will_paginate/paging.rb +21 -0
  38. data/lib/trailblazer/finder/base.rb +20 -83
  39. data/lib/trailblazer/finder/dsl.rb +56 -0
  40. data/lib/trailblazer/finder/find.rb +28 -15
  41. data/lib/trailblazer/finder/helpers/basic.rb +44 -0
  42. data/lib/trailblazer/finder/helpers/sorting.rb +54 -0
  43. data/lib/trailblazer/finder/utils/array.rb +20 -0
  44. data/lib/trailblazer/finder/utils/extra.rb +10 -20
  45. data/lib/trailblazer/finder/utils/hash.rb +36 -0
  46. data/lib/trailblazer/finder/utils/splitter.rb +16 -17
  47. data/lib/trailblazer/finder/utils/string.rb +20 -10
  48. data/lib/trailblazer/finder/version.rb +3 -1
  49. data/lib/trailblazer/operation/finder.rb +20 -19
  50. data/spec/spec_helper.rb +5 -11
  51. data/spec/spec_helper_active_record.rb +11 -12
  52. data/spec/spec_helper_sequel.rb +4 -4
  53. data/spec/spec_helper_will_paginate.rb +13 -0
  54. data/spec/trailblazer/finder/adapters/active_record_spec.rb +267 -0
  55. data/spec/trailblazer/finder/adapters/basic_spec.rb +251 -0
  56. data/spec/trailblazer/finder/adapters/kaminari_spec.rb +78 -32
  57. data/spec/trailblazer/finder/adapters/sequel_spec.rb +267 -0
  58. data/spec/trailblazer/finder/adapters/will_paginate_spec.rb +81 -40
  59. data/spec/trailblazer/finder/base_spec.rb +292 -204
  60. data/spec/trailblazer/finder/dsl_spec.rb +213 -0
  61. data/spec/trailblazer/finder/utils/hash_spec.rb +26 -0
  62. data/spec/trailblazer/finder/utils/splitter_spec.rb +39 -0
  63. data/spec/trailblazer/finder/utils/string_spec.rb +52 -9
  64. data/spec/trailblazer/operation/finder_spec.rb +35 -32
  65. data/trailblazer-finder.gemspec +31 -30
  66. metadata +119 -137
  67. data/lib/trailblazer/finder/adapters.rb +0 -26
  68. data/lib/trailblazer/finder/adapters/active_record.rb +0 -32
  69. data/lib/trailblazer/finder/adapters/data_mapper.rb +0 -32
  70. data/lib/trailblazer/finder/adapters/data_mapper/paging.rb +0 -20
  71. data/lib/trailblazer/finder/adapters/data_mapper/predicates.rb +0 -42
  72. data/lib/trailblazer/finder/adapters/data_mapper/sorting.rb +0 -38
  73. data/lib/trailblazer/finder/adapters/friendly_id.rb +0 -31
  74. data/lib/trailblazer/finder/adapters/kaminari.rb +0 -18
  75. data/lib/trailblazer/finder/adapters/sequel.rb +0 -32
  76. data/lib/trailblazer/finder/adapters/will_paginate.rb +0 -18
  77. data/lib/trailblazer/finder/errors/block_ignored.rb +0 -11
  78. data/lib/trailblazer/finder/errors/invalid_defined_by_value.rb +0 -11
  79. data/lib/trailblazer/finder/errors/invalid_number.rb +0 -16
  80. data/lib/trailblazer/finder/errors/missing_entity_type.rb +0 -11
  81. data/lib/trailblazer/finder/errors/with_ignored.rb +0 -11
  82. data/lib/trailblazer/finder/features.rb +0 -23
  83. data/lib/trailblazer/finder/features/paging.rb +0 -55
  84. data/lib/trailblazer/finder/features/predicate.rb +0 -32
  85. data/lib/trailblazer/finder/features/sorting.rb +0 -108
  86. data/lib/trailblazer/finder/filter.rb +0 -66
  87. data/lib/trailblazer/finder/predicates.rb +0 -39
  88. data/lib/trailblazer/finder/utils/deep_locate.rb +0 -30
  89. data/lib/trailblazer/finder/utils/params.rb +0 -28
  90. data/lib/trailblazer/finder/utils/parse.rb +0 -25
  91. data/spec/spec_helper_data_mapper.rb +0 -35
  92. data/spec/support/paging_shared_example.rb +0 -65
  93. data/spec/support/sorting_shared_example.rb +0 -125
  94. data/spec/trailblazer/finder/adapters/active_record/base_spec.rb +0 -112
  95. data/spec/trailblazer/finder/adapters/active_record/paging_spec.rb +0 -64
  96. data/spec/trailblazer/finder/adapters/active_record/predicates_spec.rb +0 -101
  97. data/spec/trailblazer/finder/adapters/active_record/sorting_spec.rb +0 -99
  98. data/spec/trailblazer/finder/adapters/data_mapper/base_spec.rb +0 -112
  99. data/spec/trailblazer/finder/adapters/data_mapper/paging_spec.rb +0 -64
  100. data/spec/trailblazer/finder/adapters/data_mapper/predicates_spec.rb +0 -100
  101. data/spec/trailblazer/finder/adapters/data_mapper/sorting_spec.rb +0 -97
  102. data/spec/trailblazer/finder/adapters/friendly_id_spec.rb +0 -46
  103. data/spec/trailblazer/finder/adapters/sequel/base_spec.rb +0 -125
  104. data/spec/trailblazer/finder/adapters/sequel/paging_spec.rb +0 -64
  105. data/spec/trailblazer/finder/adapters/sequel/predicates_spec.rb +0 -89
  106. data/spec/trailblazer/finder/adapters/sequel/sorting_spec.rb +0 -110
  107. data/spec/trailblazer/finder/adapters_spec.rb +0 -110
  108. data/spec/trailblazer/finder/features/paging_spec.rb +0 -104
  109. data/spec/trailblazer/finder/features/predicates_spec.rb +0 -99
  110. data/spec/trailblazer/finder/features/sorting_spec.rb +0 -96
  111. data/spec/trailblazer/finder/features_spec.rb +0 -55
  112. data/spec/trailblazer/finder/filter_spec.rb +0 -133
  113. data/spec/trailblazer/finder/find_spec.rb +0 -72
  114. data/spec/trailblazer/finder/utils/extra_spec.rb +0 -41
  115. data/spec/trailblazer/finder/utils/params_spec.rb +0 -39
  116. data/spec/trailblazer/finder/utils/parse_spec.rb +0 -33
  117. data/spec/trailblazer/operation/paging_spec.rb +0 -68
  118. data/spec/trailblazer/operation/predicates_spec.rb +0 -127
  119. data/spec/trailblazer/operation/sorting_spec.rb +0 -80
  120. data/spec/trailblazer/test_spec.rb +0 -41
@@ -1,61 +1,107 @@
1
- require 'spec_helper_active_record'
2
- require 'kaminari'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper_active_record"
4
+ require "kaminari"
5
+ require "kaminari/activerecord"
3
6
 
4
7
  module Trailblazer
5
8
  class Finder
6
9
  module Adapters
7
10
  describe Kaminari do
8
- it_behaves_like 'a paging feature'
9
-
10
11
  after do
11
12
  Product.delete_all
13
+ Product.reset_pk_sequence
12
14
  end
13
15
 
14
16
  def define_finder_class(&block)
15
- Class.new do
16
- include Trailblazer::Finder::Base
17
- include Trailblazer::Finder::Features::Paging
18
- include Trailblazer::Finder::Adapters::ActiveRecord
19
- include Trailblazer::Finder::Adapters::Kaminari
17
+ Class.new(Trailblazer::Finder) do
18
+ # adapters ActiveRecord, Kaminari
19
+ entity { Product }
20
20
 
21
- instance_eval(&block) if block_given?
21
+ class_eval(&block)
22
22
  end
23
23
  end
24
24
 
25
- def finder_class
25
+ def finder_class(&block)
26
26
  define_finder_class do
27
- entity_type { Product }
28
-
29
- per_page 2
30
-
31
- min_per_page 2
32
- max_per_page 10
27
+ class_eval(&block) unless block.nil?
33
28
  end
34
29
  end
35
30
 
36
- def finder_with_page(page = nil, per_page = nil)
37
- finder_class.new page: page, per_page: per_page
31
+ def new_finder(filter = {}, &block)
32
+ finder_class(&block).new params: filter
38
33
  end
39
34
 
40
- it 'can be inherited' do
41
- child_class = Class.new(finder_class)
42
- expect(child_class.new.per_page).to eq 2
35
+ describe "#adapters" do
36
+ it "cannot use kaminari without an actual orm" do
37
+ 10.times { |i| Product.create name: "product_#{i}" }
38
+ finder = new_finder do
39
+ adapters Kaminari
40
+ paging per_page: 2, min_per_page: 1, max_per_page: 5
41
+ end
42
+
43
+ expect(finder.errors).to eq [{adapters: "Can't use paging adapters like Kaminari without using an ORM like ActiveRecord or Sequel"}]
44
+ end
43
45
  end
44
46
 
45
- describe '#results' do
46
- it 'paginates results' do
47
- 6.times { |i| Product.create name: "product_#{i}" }
48
- finder = finder_with_page 2, 2
47
+ describe "#paging" do
48
+ it "sets the paging values and shows only the first page results" do
49
+ 10.times { |i| Product.create name: "product_#{i}" }
50
+ finder = new_finder do
51
+ adapters ActiveRecord, Kaminari
52
+ paging per_page: 2, min_per_page: 1, max_per_page: 5
53
+ end
49
54
 
50
- expect(finder.results.map(&:name)).to eq %w[product_2 product_3]
55
+ expect(finder.result.map { |n| n[:name] }).to eq %w[product_0 product_1]
51
56
  end
52
- end
53
57
 
54
- describe '#count' do
55
- it 'gives the real count' do
58
+ it "accepts per_page as a parameter" do
59
+ 10.times { |i| Product.create name: "product_#{i}" }
60
+ finder = new_finder page: 2, per_page: 4 do
61
+ adapters ActiveRecord, Kaminari
62
+ paging per_page: 5, min_per_page: 2, max_per_page: 8
63
+ end
64
+
65
+ expect(finder.result.first.id).to eq 5
66
+ expect(finder.result.map(&:id)).to eq [5, 6, 7, 8]
67
+ end
68
+
69
+ it "uses max_per_page in finder as maximum per_page" do
70
+ 10.times { |i| Product.create name: "product_#{i}" }
71
+ finder = new_finder page: 2, per_page: 9 do
72
+ adapters ActiveRecord, Kaminari
73
+ paging per_page: 5, min_per_page: 2, max_per_page: 8
74
+ end
75
+
76
+ expect(finder.result.first.id).to eq 9
77
+ expect(finder.result.map(&:id)).to eq [9, 10]
78
+ end
79
+
80
+ it "uses min_per_page in finder as minimum per_page" do
56
81
  10.times { |i| Product.create name: "product_#{i}" }
57
- finder = finder_with_page 1
58
- expect(finder.count).to eq 10
82
+ finder = new_finder page: 2, per_page: 1 do
83
+ adapters ActiveRecord, Kaminari
84
+ paging per_page: 5, min_per_page: 2, max_per_page: 8
85
+ end
86
+
87
+ expect(finder.result.first.id).to eq 3
88
+ expect(finder.result.map(&:id)).to eq [3, 4]
89
+ end
90
+ end
91
+
92
+ describe "Predicates, Paging and Sorting together" do
93
+ it "sets the property and works with eq predicate and paging" do
94
+ 5.times { |i| Product.create name: "product_#{i}" }
95
+ 5.times { |_i| Product.create name: "product" }
96
+ finder = new_finder name_eq: "product", sort: "id desc", page: 2 do
97
+ adapters ActiveRecord, Kaminari
98
+ paging per_page: 2, min_per_page: 1, max_per_page: 5
99
+ property :name, type: Types::String
100
+ property :id, type: Types::Integer, sortable: true
101
+ end
102
+
103
+ expect(finder.result.map(&:id)).to eq [8, 7]
104
+ expect(finder.count).to eq 2
59
105
  end
60
106
  end
61
107
  end
@@ -0,0 +1,267 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper_sequel"
4
+
5
+ module Trailblazer
6
+ class Finder
7
+ module Adapters
8
+ describe Sequel do
9
+ after do
10
+ SProduct.order(:id).delete
11
+ DB.run "update sqlite_sequence set seq = 0 where name = 's_products';"
12
+ end
13
+
14
+ def define_finder_class(&block)
15
+ Class.new(Trailblazer::Finder) do
16
+ adapters Sequel
17
+ entity { SProduct }
18
+
19
+ class_eval(&block)
20
+ end
21
+ end
22
+
23
+ def finder_class(&block)
24
+ define_finder_class do
25
+ class_eval(&block) unless block.nil?
26
+ end
27
+ end
28
+
29
+ def new_finder(filter = {}, &block)
30
+ finder_class(&block).new params: filter
31
+ end
32
+
33
+ describe "#property" do
34
+ it "sets the property and works with eq predicate" do
35
+ 10.times { |i| SProduct.create name: "product_#{i}" }
36
+ finder = new_finder name_eq: "product_2" do
37
+ property :name, type: Types::String
38
+ end
39
+
40
+ expect(finder.result.first.id).to eq 3
41
+ expect(finder.result.count).to eq 1
42
+ end
43
+
44
+ it "sets the property and works with not_eq predicate" do
45
+ 10.times { |i| SProduct.create name: "product_#{i}" }
46
+ finder = new_finder name_not_eq: "product_2" do
47
+ property :name, type: Types::String
48
+ end
49
+
50
+ expect(finder.result.count).to eq 9
51
+ end
52
+
53
+ it "sets the property and works with blank predicate" do
54
+ 9.times { |i| SProduct.create name: "product_#{i}" }
55
+ SProduct.create name: "", slug: "Blank Test"
56
+ finder = new_finder name_blank: true do
57
+ property :name, type: Types::String
58
+ end
59
+
60
+ expect(finder.result.count).to eq 1
61
+ expect(finder.result.first.slug).to eq "Blank Test"
62
+ end
63
+
64
+ it "sets the property and works with not blank predicate" do
65
+ 9.times { |i| SProduct.create name: "product_#{i}" }
66
+ SProduct.create name: "", slug: "Blank Test"
67
+ finder = new_finder name_not_blank: true do
68
+ property :name, type: Types::String
69
+ end
70
+
71
+ expect(finder.result.count).to eq 9
72
+ expect(finder.result.first.name).to eq "product_0"
73
+ end
74
+
75
+ it "sets the property and works with gt (greater than) predicate" do
76
+ 10.times { |i| SProduct.create name: "product_#{i}" }
77
+ finder = new_finder id_gt: 3 do
78
+ property :id, type: Types::Integer
79
+ end
80
+
81
+ expect(finder.result.count).to eq 7
82
+ end
83
+
84
+ it "sets the property and works with gte (greater than or equal to) predicate" do
85
+ 10.times { |i| SProduct.create name: "product_#{i}" }
86
+ finder = new_finder id_gte: 3 do
87
+ property :id, type: Types::Integer
88
+ end
89
+
90
+ expect(finder.result.count).to eq 8
91
+ end
92
+
93
+ it "sets the property and works with lt (less than) predicate" do
94
+ 10.times { |i| SProduct.create name: "product_#{i}" }
95
+ finder = new_finder id_lt: 3 do
96
+ property :id, type: Types::Integer
97
+ end
98
+
99
+ expect(finder.result.count).to eq 2
100
+ end
101
+
102
+ it "sets the property and works with lte (less than or equal to) predicate" do
103
+ 10.times { |i| SProduct.create name: "product_#{i}" }
104
+ finder = new_finder id_lte: 3 do
105
+ property :id, type: Types::Integer
106
+ end
107
+
108
+ expect(finder.result.count).to eq 3
109
+ end
110
+
111
+ it "sets the property and works with cont predicate" do
112
+ 5.times { |i| SProduct.create name: "product_#{i}" }
113
+ 5.times { |i| SProduct.create name: "none_#{i}" }
114
+ finder = new_finder name_cont: "product" do
115
+ property :name, type: Types::String
116
+ end
117
+
118
+ expect(finder.result.first.id).to eq 1
119
+ expect(finder.result.count).to eq 5
120
+ end
121
+
122
+ it "sets the property and works with not_cont predicate" do
123
+ 5.times { |i| SProduct.create name: "product_#{i}" }
124
+ 5.times { |i| SProduct.create name: "none_#{i}" }
125
+ finder = new_finder name_not_cont: "product" do
126
+ property :name, type: Types::String
127
+ end
128
+
129
+ expect(finder.result.first.id).to eq 6
130
+ expect(finder.result.count).to eq 5
131
+ end
132
+
133
+ it "sets the property and works with sw predicate" do
134
+ 5.times { |i| SProduct.create name: "product_#{i}" }
135
+ 1.times { |i| SProduct.create name: "predicate_#{i}" }
136
+ 4.times { |i| SProduct.create name: "none_#{i}" }
137
+ finder = new_finder name_sw: "pr" do
138
+ property :name, type: Types::String
139
+ end
140
+
141
+ expect(finder.result.first.id).to eq 1
142
+ expect(finder.result.count).to eq 6
143
+ end
144
+
145
+ it "sets the property and works with not_sw predicate" do
146
+ 5.times { |i| SProduct.create name: "product_#{i}" }
147
+ 1.times { |i| SProduct.create name: "predicate_#{i}" }
148
+ 4.times { |i| SProduct.create name: "none_#{i}" }
149
+ finder = new_finder name_not_sw: "pr" do
150
+ property :name, type: Types::String
151
+ end
152
+
153
+ expect(finder.result.first.id).to eq 7
154
+ expect(finder.result.count).to eq 4
155
+ end
156
+
157
+ it "sets the property and works with ew predicate" do
158
+ 5.times { |i| SProduct.create name: "product_#{i}_end" }
159
+ 1.times { |i| SProduct.create name: "predicate_#{i}_endwow" }
160
+ 4.times { |i| SProduct.create name: "none_#{i}" }
161
+ finder = new_finder name_ew: "end" do
162
+ property :name, type: Types::String
163
+ end
164
+
165
+ expect(finder.result.first.id).to eq 1
166
+ expect(finder.result.count).to eq 5
167
+ end
168
+
169
+ it "sets the property and works with not_ew predicate" do
170
+ 5.times { |i| SProduct.create name: "product_#{i}_end" }
171
+ 1.times { |i| SProduct.create name: "predicate_#{i}_endwow" }
172
+ 4.times { |i| SProduct.create name: "none_#{i}" }
173
+ finder = new_finder name_not_ew: "end" do
174
+ property :name, type: Types::String
175
+ end
176
+
177
+ expect(finder.result.first.id).to eq 6
178
+ expect(finder.result.count).to eq 5
179
+ end
180
+ end
181
+
182
+ describe "#paging" do
183
+ it "sets the paging values and shows only the first page results" do
184
+ 10.times { |i| SProduct.create name: "product_#{i}" }
185
+ finder = new_finder do
186
+ paging per_page: 2, min_per_page: 1, max_per_page: 5
187
+ end
188
+
189
+ expect(finder.result.map { |n| n[:name] }).to eq %w[product_0 product_1]
190
+ end
191
+
192
+ it "accepts per_page as a parameter" do
193
+ 10.times { |i| SProduct.create name: "product_#{i}" }
194
+ finder = new_finder page: 2, per_page: 4 do
195
+ paging per_page: 5, min_per_page: 2, max_per_page: 8
196
+ end
197
+
198
+ expect(finder.result.first.id).to eq 5
199
+ expect(finder.result.map(&:id)).to eq [5, 6, 7, 8]
200
+ end
201
+
202
+ it "uses max_per_page in finder as maximum per_page" do
203
+ 10.times { |i| SProduct.create name: "product_#{i}" }
204
+ finder = new_finder page: 2, per_page: 9 do
205
+ paging per_page: 5, min_per_page: 2, max_per_page: 8
206
+ end
207
+
208
+ expect(finder.result.first.id).to eq 9
209
+ expect(finder.result.map(&:id)).to eq [9, 10]
210
+ end
211
+
212
+ it "uses min_per_page in finder as minimum per_page" do
213
+ 10.times { |i| SProduct.create name: "product_#{i}" }
214
+ finder = new_finder page: 2, per_page: 1 do
215
+ paging per_page: 5, min_per_page: 2, max_per_page: 8
216
+ end
217
+
218
+ expect(finder.result.first.id).to eq 3
219
+ expect(finder.result.map(&:id)).to eq [3, 4]
220
+ end
221
+ end
222
+
223
+ describe "#sorting" do
224
+ it "sets the property and works with eq predicate and sorting" do
225
+ 5.times { |i| SProduct.create name: "product_#{i}" }
226
+ 5.times { |_i| SProduct.create name: "product" }
227
+ finder = new_finder name_eq: "product", sort: "id desc" do
228
+ property :name, type: Types::String
229
+ property :id, type: Types::Integer, sortable: true
230
+ end
231
+
232
+ expect(finder.result.first.id).to eq 10
233
+ expect(finder.result.count).to eq 5
234
+ end
235
+
236
+ it "sets the property and sorts by multiple columns" do
237
+ 5.times { |i| SProduct.create name: "product_#{i}", slug: "slug #{i}" }
238
+ 5.times { |i| SProduct.create name: "product", slug: "slug #{i}" }
239
+ finder = new_finder sort: "name asc, slug desc" do
240
+ property :name, type: Types::String, sortable: true
241
+ property :slug, type: Types::String, sortable: true
242
+ property :id, type: Types::Integer, sortable: true
243
+ end
244
+
245
+ expect(finder.result.map { |n| n[:id] }).to eq [10, 9, 8, 7, 6, 1, 2, 3, 4, 5]
246
+ expect(finder.result.count).to eq 10
247
+ end
248
+ end
249
+
250
+ describe "Predicates, Paging and Sorting together" do
251
+ it "sets the property and works with eq predicate and paging" do
252
+ 5.times { |i| SProduct.create name: "product_#{i}" }
253
+ 5.times { |_i| SProduct.create name: "product" }
254
+ finder = new_finder name_eq: "product", sort: "id desc", page: 2 do
255
+ paging per_page: 2, min_per_page: 1, max_per_page: 5
256
+ property :name, type: Types::String
257
+ property :id, type: Types::Integer, sortable: true
258
+ end
259
+
260
+ expect(finder.result.first.id).to eq 8
261
+ expect(finder.result.map(&:id)).to eq [8, 7]
262
+ end
263
+ end
264
+ end
265
+ end
266
+ end
267
+ end
@@ -1,68 +1,109 @@
1
- require 'spec_helper_active_record'
1
+ # frozen_string_literal: true
2
2
 
3
- require 'will_paginate'
4
- require 'will_paginate/active_record'
3
+ require "spec_helper_active_record"
4
+
5
+ require "will_paginate"
6
+ require "will_paginate/active_record"
7
+
8
+ require "spec_helper_will_paginate"
5
9
 
6
10
  module Trailblazer
7
11
  class Finder
8
12
  module Adapters
9
13
  describe WillPaginate do
10
- it_behaves_like 'a paging feature' do
11
- after do
12
- Product.delete_all
13
- end
14
+ after do
15
+ Product.delete_all
16
+ Product.reset_pk_sequence
17
+ end
14
18
 
15
- def define_finder_class(&block)
16
- Class.new do
17
- include Trailblazer::Finder::Base
18
- include Trailblazer::Finder::Features::Paging
19
- include Trailblazer::Finder::Adapters::ActiveRecord
20
- include Trailblazer::Finder::Adapters::WillPaginate
19
+ def define_finder_class(&block)
20
+ Class.new(Trailblazer::Finder) do
21
+ entity { Product }
21
22
 
22
- instance_eval(&block) if block_given?
23
- end
23
+ class_eval(&block)
24
24
  end
25
+ end
25
26
 
26
- def finder_class
27
- define_finder_class do
28
- entity_type { Product }
27
+ def finder_class(&block)
28
+ define_finder_class do
29
+ class_eval(&block) unless block.nil?
30
+ end
31
+ end
29
32
 
30
- per_page 2
33
+ def new_finder(filter = {}, &block)
34
+ finder_class(&block).new params: filter
35
+ end
31
36
 
32
- min_per_page 2
33
- max_per_page 10
37
+ describe "#adapters" do
38
+ it "cannot use kaminari without an actual orm" do
39
+ 10.times { |i| Product.create name: "product_#{i}" }
40
+ finder = new_finder do
41
+ adapters WillPaginate
42
+ paging per_page: 2, min_per_page: 1, max_per_page: 5
34
43
  end
35
- end
36
44
 
37
- def finder_with_page(page = nil, per_page = nil)
38
- finder_class.new page: page, per_page: per_page
45
+ expect(finder.errors).to eq [{adapters: "Can't use paging adapters like Kaminari without using an ORM like ActiveRecord or Sequel"}]
39
46
  end
47
+ end
40
48
 
41
- it 'can be inherited' do
42
- child_class = Class.new(finder_class)
43
- expect(child_class.new.per_page).to eq 2
49
+ describe "#paging" do
50
+ it "sets the paging values and shows only the first page results" do
51
+ 10.times { |i| Product.create name: "product_#{i}" }
52
+ finder = new_finder do
53
+ adapters ActiveRecord, WillPaginate
54
+ paging per_page: 2, min_per_page: 1, max_per_page: 5
55
+ end
56
+
57
+ expect(finder.result.map { |n| n[:name] }).to eq %w[product_0 product_1]
44
58
  end
45
59
 
46
- describe '#results' do
47
- it 'paginates results' do
48
- 6.times { |i| Product.create name: "product_#{i}" }
49
- finder = finder_with_page 2, 2
60
+ it "accepts per_page as a parameter" do
61
+ 10.times { |i| Product.create name: "product_#{i}" }
62
+ finder = new_finder page: 2, per_page: 4 do
63
+ adapters ActiveRecord, WillPaginate
64
+ paging per_page: 5, min_per_page: 2, max_per_page: 8
65
+ end
66
+
67
+ expect(finder.result.first.id).to eq 5
68
+ expect(finder.result.map(&:id)).to eq [5, 6, 7, 8]
69
+ end
50
70
 
51
- expect(finder.results.map(&:name)).to eq %w[product_2 product_3]
71
+ it "uses max_per_page in finder as maximum per_page" do
72
+ 10.times { |i| Product.create name: "product_#{i}" }
73
+ finder = new_finder page: 2, per_page: 9 do
74
+ adapters ActiveRecord, WillPaginate
75
+ paging per_page: 5, min_per_page: 2, max_per_page: 8
52
76
  end
77
+
78
+ expect(finder.result.first.id).to eq 9
79
+ expect(finder.result.map(&:id)).to eq [9, 10]
53
80
  end
54
81
 
55
- describe '#count' do
56
- it 'gives the real count' do
57
- 10.times { |i| Product.create name: "product_#{i}" }
58
- finder = finder_with_page 1
59
- expect(finder.count).to eq 10
82
+ it "uses min_per_page in finder as minimum per_page" do
83
+ 10.times { |i| Product.create name: "product_#{i}" }
84
+ finder = new_finder page: 2, per_page: 1 do
85
+ adapters ActiveRecord, WillPaginate
86
+ paging per_page: 5, min_per_page: 2, max_per_page: 8
60
87
  end
88
+
89
+ expect(finder.result.first.id).to eq 3
90
+ expect(finder.result.map(&:id)).to eq [3, 4]
61
91
  end
92
+ end
93
+
94
+ describe "Predicates, Paging and Sorting together" do
95
+ it "sets the property and works with eq predicate and paging" do
96
+ 5.times { |i| Product.create name: "product_#{i}" }
97
+ 5.times { |_i| Product.create name: "product" }
98
+ finder = new_finder name_eq: "product", sort: "id desc", page: 2 do
99
+ adapters ActiveRecord, WillPaginate
100
+ paging per_page: 2, min_per_page: 1, max_per_page: 5
101
+ property :name, type: Types::String
102
+ property :id, type: Types::Integer, sortable: true
103
+ end
62
104
 
63
- it 'uses will_paginate gem' do
64
- finder = finder_with_page
65
- expect(finder.results).to be_respond_to :total_entries
105
+ expect(finder.result.map(&:id)).to eq [8, 7]
106
+ expect(finder.count).to eq 2
66
107
  end
67
108
  end
68
109
  end