ajax-datatables-rails 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +120 -0
- data/.rubocop.yml +3 -1
- data/Appraisals +7 -13
- data/CHANGELOG.md +23 -2
- data/README.md +51 -70
- data/ajax-datatables-rails.gemspec +11 -5
- data/doc/migrate.md +44 -0
- data/doc/webpack.md +4 -1
- data/gemfiles/{rails_5.2.3.gemfile → rails_5.2.4.gemfile} +1 -1
- data/gemfiles/{rails_6.0.1.gemfile → rails_6.0.3.gemfile} +1 -1
- data/gemfiles/{rails_5.0.7.gemfile → rails_6.1.0.gemfile} +3 -3
- data/lib/ajax-datatables-rails.rb +0 -16
- data/lib/ajax-datatables-rails/base.rb +38 -13
- data/lib/ajax-datatables-rails/datatable.rb +6 -0
- data/lib/ajax-datatables-rails/datatable/column.rb +59 -21
- data/lib/ajax-datatables-rails/datatable/column/date_filter.rb +1 -1
- data/lib/ajax-datatables-rails/datatable/column/search.rb +2 -2
- data/lib/ajax-datatables-rails/datatable/datatable.rb +17 -3
- data/lib/ajax-datatables-rails/datatable/simple_order.rb +7 -5
- data/lib/ajax-datatables-rails/error.rb +9 -0
- data/lib/ajax-datatables-rails/orm.rb +6 -0
- data/lib/ajax-datatables-rails/orm/active_record.rb +9 -10
- data/lib/ajax-datatables-rails/version.rb +1 -1
- data/spec/ajax-datatables-rails/base_spec.rb +77 -120
- data/spec/ajax-datatables-rails/datatable/column_spec.rb +30 -10
- data/spec/ajax-datatables-rails/datatable/datatable_spec.rb +61 -22
- data/spec/ajax-datatables-rails/datatable/simple_order_spec.rb +11 -9
- data/spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb +352 -257
- data/spec/ajax-datatables-rails/orm/active_record_sort_records_spec.rb +4 -4
- data/spec/install_oracle.sh +4 -4
- data/spec/spec_helper.rb +10 -19
- data/spec/support/datatables/complex_datatable.rb +29 -0
- data/spec/support/datatables/complex_datatable_array.rb +14 -0
- data/spec/support/{datatable_cond_date.rb → datatables/datatable_cond_date.rb} +0 -0
- data/spec/support/{datatable_cond_numeric.rb → datatables/datatable_cond_numeric.rb} +0 -0
- data/spec/support/{datatable_cond_proc.rb → datatables/datatable_cond_proc.rb} +0 -0
- data/spec/support/{datatable_cond_string.rb → datatables/datatable_cond_string.rb} +0 -0
- data/spec/support/datatables/datatable_cond_unknown.rb +5 -0
- data/spec/support/{datatable_order_nulls_last.rb → datatables/datatable_order_nulls_last.rb} +0 -0
- data/spec/support/{test_helpers.rb → helpers/params.rb} +8 -46
- data/spec/support/{test_models.rb → models/user.rb} +0 -0
- metadata +53 -49
- data/.travis.yml +0 -57
- data/gemfiles/rails_5.1.7.gemfile +0 -11
- data/lib/ajax-datatables-rails/configuration.rb +0 -10
- data/lib/generators/datatable/config_generator.rb +0 -19
- data/lib/generators/datatable/templates/ajax_datatables_rails_config.rb +0 -9
- data/spec/ajax-datatables-rails/configuration_spec.rb +0 -34
- data/spec/ajax-datatables-rails/extended_spec.rb +0 -19
@@ -2,7 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe AjaxDatatablesRails::Datatable::SimpleOrder do
|
4
4
|
|
5
|
-
let(:
|
5
|
+
let(:parent) { ComplexDatatable.new(sample_params) }
|
6
|
+
let(:datatable) { parent.datatable }
|
6
7
|
let(:options) { ActiveSupport::HashWithIndifferentAccess.new({'column' => '1', 'dir' => 'desc'}) }
|
7
8
|
let(:simple_order) { AjaxDatatablesRails::Datatable::SimpleOrder.new(datatable, options) }
|
8
9
|
|
@@ -13,25 +14,26 @@ describe AjaxDatatablesRails::Datatable::SimpleOrder do
|
|
13
14
|
end
|
14
15
|
|
15
16
|
describe 'option methods with nulls last' do
|
16
|
-
describe 'using
|
17
|
-
before {
|
18
|
-
after {
|
17
|
+
describe 'using class option' do
|
18
|
+
before { parent.nulls_last = true }
|
19
|
+
after { parent.nulls_last = false }
|
19
20
|
|
20
21
|
it 'sql query' do
|
21
22
|
skip('unsupported database adapter') if ENV['DB_ADAPTER'] == 'oracle_enhanced'
|
22
23
|
|
23
24
|
expect(simple_order.query('email')).to eq(
|
24
|
-
"email DESC #{nulls_last_sql}"
|
25
|
+
"email DESC #{nulls_last_sql(parent)}"
|
25
26
|
)
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
29
30
|
describe 'using column option' do
|
30
|
-
let(:
|
31
|
+
let(:parent) { DatatableOrderNullsLast.new(sample_params) }
|
32
|
+
let(:sorted_datatable) { parent.datatable }
|
31
33
|
let(:nulls_last_order) { AjaxDatatablesRails::Datatable::SimpleOrder.new(sorted_datatable, options) }
|
32
34
|
|
33
35
|
context 'with postgres database adapter' do
|
34
|
-
before {
|
36
|
+
before { parent.db_adapter = :pg }
|
35
37
|
|
36
38
|
it 'sql query' do
|
37
39
|
expect(nulls_last_order.query('email')).to eq('email DESC NULLS LAST')
|
@@ -39,7 +41,7 @@ describe AjaxDatatablesRails::Datatable::SimpleOrder do
|
|
39
41
|
end
|
40
42
|
|
41
43
|
context 'with sqlite database adapter' do
|
42
|
-
before {
|
44
|
+
before { parent.db_adapter = :sqlite }
|
43
45
|
|
44
46
|
it 'sql query' do
|
45
47
|
expect(nulls_last_order.query('email')).to eq('email DESC IS NULL')
|
@@ -47,7 +49,7 @@ describe AjaxDatatablesRails::Datatable::SimpleOrder do
|
|
47
49
|
end
|
48
50
|
|
49
51
|
context 'with mysql database adapter' do
|
50
|
-
before {
|
52
|
+
before { parent.db_adapter = :mysql }
|
51
53
|
|
52
54
|
it 'sql query' do
|
53
55
|
expect(nulls_last_order.query('email')).to eq('email DESC IS NULL')
|
@@ -23,6 +23,29 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
describe '#build_conditions' do
|
27
|
+
before(:each) do
|
28
|
+
create(:user, username: 'johndoe', email: 'johndoe@example.com')
|
29
|
+
create(:user, username: 'msmith', email: 'mary.smith@example.com')
|
30
|
+
create(:user, username: 'hsmith', email: 'henry.smith@example.net')
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with column and global search' do
|
34
|
+
before(:each) do
|
35
|
+
datatable.params[:search] = { value: 'example.com', regex: 'false' }
|
36
|
+
datatable.params[:columns]['0'][:search][:value] = 'smith'
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'return a filtered set of records' do
|
40
|
+
query = datatable.build_conditions
|
41
|
+
results = records.where(query).map(&:username)
|
42
|
+
expect(results).to include('msmith')
|
43
|
+
expect(results).not_to include('johndoe')
|
44
|
+
expect(results).not_to include('hsmith')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
26
49
|
describe '#build_conditions_for_datatable' do
|
27
50
|
before(:each) do
|
28
51
|
create(:user, username: 'johndoe', email: 'johndoe@example.com')
|
@@ -106,6 +129,23 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
106
129
|
expect(results).not_to include('msmith')
|
107
130
|
end
|
108
131
|
end
|
132
|
+
|
133
|
+
# TODO: improve (or delete?) this test
|
134
|
+
context 'when column.search_query returns nil' do
|
135
|
+
let(:datatable) { DatatableCondUnknown.new(sample_params) }
|
136
|
+
|
137
|
+
before(:each) do
|
138
|
+
datatable.params[:search] = { value: 'john doe', regex: 'false' }
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'does not raise error' do
|
142
|
+
allow_any_instance_of(AjaxDatatablesRails::Datatable::Column).to receive(:valid_search_condition?).and_return(true)
|
143
|
+
|
144
|
+
expect {
|
145
|
+
datatable.data.size
|
146
|
+
}.to_not raise_error
|
147
|
+
end
|
148
|
+
end
|
109
149
|
end
|
110
150
|
end
|
111
151
|
|
@@ -183,373 +223,428 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
183
223
|
end
|
184
224
|
|
185
225
|
describe 'filter conditions' do
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
before(:each) do
|
190
|
-
create(:user, username: 'johndoe', email: 'johndoe@example.com', last_name: 'Doe', created_at: '01/01/2000')
|
191
|
-
create(:user, username: 'msmith', email: 'mary.smith@example.com', last_name: 'Smith', created_at: '01/02/2000')
|
192
|
-
end
|
193
|
-
|
194
|
-
context 'when range is empty' do
|
195
|
-
it 'should not filter records' do
|
196
|
-
datatable.params[:columns]['5'][:search][:value] = '-'
|
197
|
-
expect(datatable.data.size).to eq 2
|
198
|
-
item = datatable.data.first
|
199
|
-
expect(item[:last_name]).to eq 'Doe'
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
context 'when start date is filled' do
|
204
|
-
it 'should filter records created after this date' do
|
205
|
-
datatable.params[:columns]['5'][:search][:value] = '31/12/1999-'
|
206
|
-
expect(datatable.data.size).to eq 2
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
210
|
-
context 'when end date is filled' do
|
211
|
-
it 'should filter records created before this date' do
|
212
|
-
datatable.params[:columns]['5'][:search][:value] = '-31/12/1999'
|
213
|
-
expect(datatable.data.size).to eq 0
|
214
|
-
end
|
215
|
-
end
|
226
|
+
context 'date condition' do
|
227
|
+
describe 'it can filter records with condition :date_range' do
|
228
|
+
let(:datatable) { DatatableCondDate.new(sample_params) }
|
216
229
|
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
expect(datatable.data.size).to eq 1
|
230
|
+
before(:each) do
|
231
|
+
create(:user, username: 'johndoe', email: 'johndoe@example.com', last_name: 'Doe', created_at: '01/01/2000')
|
232
|
+
create(:user, username: 'msmith', email: 'mary.smith@example.com', last_name: 'Smith', created_at: '01/02/2000')
|
221
233
|
end
|
222
|
-
end
|
223
234
|
|
224
|
-
context 'when another filter is active' do
|
225
235
|
context 'when range is empty' do
|
226
|
-
it 'should filter records' do
|
227
|
-
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
236
|
+
it 'should not filter records' do
|
228
237
|
datatable.params[:columns]['5'][:search][:value] = '-'
|
229
|
-
expect(datatable.data.size).to eq
|
238
|
+
expect(datatable.data.size).to eq 2
|
230
239
|
item = datatable.data.first
|
231
240
|
expect(item[:last_name]).to eq 'Doe'
|
232
241
|
end
|
233
242
|
end
|
234
243
|
|
235
244
|
context 'when start date is filled' do
|
236
|
-
it 'should filter records' do
|
237
|
-
datatable.params[:columns]['
|
238
|
-
datatable.
|
239
|
-
expect(datatable.data.size).to eq 1
|
240
|
-
item = datatable.data.first
|
241
|
-
expect(item[:last_name]).to eq 'Doe'
|
245
|
+
it 'should filter records created after this date' do
|
246
|
+
datatable.params[:columns]['5'][:search][:value] = '31/12/1999-'
|
247
|
+
expect(datatable.data.size).to eq 2
|
242
248
|
end
|
243
249
|
end
|
244
250
|
|
245
251
|
context 'when end date is filled' do
|
246
|
-
it 'should filter records' do
|
247
|
-
datatable.params[:columns]['
|
248
|
-
datatable.
|
249
|
-
expect(datatable.data.size).to eq 1
|
250
|
-
item = datatable.data.first
|
251
|
-
expect(item[:last_name]).to eq 'Doe'
|
252
|
+
it 'should filter records created before this date' do
|
253
|
+
datatable.params[:columns]['5'][:search][:value] = '-31/12/1999'
|
254
|
+
expect(datatable.data.size).to eq 0
|
252
255
|
end
|
253
256
|
end
|
254
257
|
|
255
258
|
context 'when both date are filled' do
|
256
|
-
it 'should filter records' do
|
257
|
-
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
259
|
+
it 'should filter records created between the range' do
|
258
260
|
datatable.params[:columns]['5'][:search][:value] = '01/12/1999-15/01/2000'
|
259
261
|
expect(datatable.data.size).to eq 1
|
260
|
-
item = datatable.data.first
|
261
|
-
expect(item[:last_name]).to eq 'Doe'
|
262
262
|
end
|
263
263
|
end
|
264
|
-
end
|
265
|
-
end
|
266
264
|
|
267
|
-
|
268
|
-
|
265
|
+
context 'when another filter is active' do
|
266
|
+
context 'when range is empty' do
|
267
|
+
it 'should filter records' do
|
268
|
+
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
269
|
+
datatable.params[:columns]['5'][:search][:value] = '-'
|
270
|
+
expect(datatable.data.size).to eq 1
|
271
|
+
item = datatable.data.first
|
272
|
+
expect(item[:last_name]).to eq 'Doe'
|
273
|
+
end
|
274
|
+
end
|
269
275
|
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
276
|
+
context 'when start date is filled' do
|
277
|
+
it 'should filter records' do
|
278
|
+
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
279
|
+
datatable.params[:columns]['5'][:search][:value] = '01/12/1999-'
|
280
|
+
expect(datatable.data.size).to eq 1
|
281
|
+
item = datatable.data.first
|
282
|
+
expect(item[:last_name]).to eq 'Doe'
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
context 'when end date is filled' do
|
287
|
+
it 'should filter records' do
|
288
|
+
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
289
|
+
datatable.params[:columns]['5'][:search][:value] = '-15/01/2000'
|
290
|
+
expect(datatable.data.size).to eq 1
|
291
|
+
item = datatable.data.first
|
292
|
+
expect(item[:last_name]).to eq 'Doe'
|
293
|
+
end
|
294
|
+
end
|
274
295
|
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
296
|
+
context 'when both date are filled' do
|
297
|
+
it 'should filter records' do
|
298
|
+
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
299
|
+
datatable.params[:columns]['5'][:search][:value] = '01/12/1999-15/01/2000'
|
300
|
+
expect(datatable.data.size).to eq 1
|
301
|
+
item = datatable.data.first
|
302
|
+
expect(item[:last_name]).to eq 'Doe'
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
280
306
|
end
|
281
307
|
end
|
282
308
|
|
283
|
-
|
284
|
-
|
309
|
+
context 'numeric condition' do
|
310
|
+
describe 'it can filter records with condition :eq' do
|
311
|
+
let(:datatable) { DatatableCondEq.new(sample_params) }
|
285
312
|
|
286
|
-
|
287
|
-
|
288
|
-
|
313
|
+
before(:each) do
|
314
|
+
create(:user, first_name: 'john', post_id: 1)
|
315
|
+
create(:user, first_name: 'mary', post_id: 2)
|
316
|
+
end
|
317
|
+
|
318
|
+
it 'should filter records matching' do
|
319
|
+
datatable.params[:columns]['4'][:search][:value] = 1
|
320
|
+
expect(datatable.data.size).to eq 1
|
321
|
+
item = datatable.data.first
|
322
|
+
expect(item[:first_name]).to eq 'john'
|
323
|
+
end
|
289
324
|
end
|
290
325
|
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
expect(item[:last_name]).to eq 'MARY'
|
298
|
-
end
|
326
|
+
describe 'it can filter records with condition :not_eq' do
|
327
|
+
let(:datatable) { DatatableCondNotEq.new(sample_params) }
|
328
|
+
|
329
|
+
before(:each) do
|
330
|
+
create(:user, first_name: 'john', post_id: 1)
|
331
|
+
create(:user, first_name: 'mary', post_id: 2)
|
299
332
|
end
|
300
|
-
|
333
|
+
|
301
334
|
it 'should filter records matching' do
|
302
|
-
datatable.params[:columns]['
|
335
|
+
datatable.params[:columns]['4'][:search][:value] = 1
|
303
336
|
expect(datatable.data.size).to eq 1
|
304
337
|
item = datatable.data.first
|
305
|
-
expect(item[:
|
338
|
+
expect(item[:first_name]).to eq 'mary'
|
306
339
|
end
|
307
340
|
end
|
308
|
-
end
|
309
341
|
|
310
|
-
|
311
|
-
|
342
|
+
describe 'it can filter records with condition :lt' do
|
343
|
+
let(:datatable) { DatatableCondLt.new(sample_params) }
|
312
344
|
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
345
|
+
before(:each) do
|
346
|
+
create(:user, first_name: 'john', post_id: 1)
|
347
|
+
create(:user, first_name: 'mary', post_id: 2)
|
348
|
+
end
|
317
349
|
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
350
|
+
it 'should filter records matching' do
|
351
|
+
datatable.params[:columns]['4'][:search][:value] = 2
|
352
|
+
expect(datatable.data.size).to eq 1
|
353
|
+
item = datatable.data.first
|
354
|
+
expect(item[:first_name]).to eq 'john'
|
355
|
+
end
|
323
356
|
end
|
324
|
-
end
|
325
357
|
|
326
|
-
|
327
|
-
|
358
|
+
describe 'it can filter records with condition :gt' do
|
359
|
+
let(:datatable) { DatatableCondGt.new(sample_params) }
|
328
360
|
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
361
|
+
before(:each) do
|
362
|
+
create(:user, first_name: 'john', post_id: 1)
|
363
|
+
create(:user, first_name: 'mary', post_id: 2)
|
364
|
+
end
|
333
365
|
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
366
|
+
it 'should filter records matching' do
|
367
|
+
datatable.params[:columns]['4'][:search][:value] = 1
|
368
|
+
expect(datatable.data.size).to eq 1
|
369
|
+
item = datatable.data.first
|
370
|
+
expect(item[:first_name]).to eq 'mary'
|
371
|
+
end
|
339
372
|
end
|
340
|
-
end
|
341
373
|
|
342
|
-
|
343
|
-
|
374
|
+
describe 'it can filter records with condition :lteq' do
|
375
|
+
let(:datatable) { DatatableCondLteq.new(sample_params) }
|
344
376
|
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
end
|
377
|
+
before(:each) do
|
378
|
+
create(:user, first_name: 'john', post_id: 1)
|
379
|
+
create(:user, first_name: 'mary', post_id: 2)
|
380
|
+
end
|
350
381
|
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
expect(item[:email]).to eq 'john@foo.com'
|
382
|
+
it 'should filter records matching' do
|
383
|
+
datatable.params[:columns]['4'][:search][:value] = 2
|
384
|
+
expect(datatable.data.size).to eq 2
|
385
|
+
end
|
356
386
|
end
|
357
387
|
|
358
|
-
|
359
|
-
datatable
|
360
|
-
expect(datatable.data.size).to eq 2
|
361
|
-
items = datatable.data.sort_by { |h| h[:email] }
|
362
|
-
item_first = items.first
|
363
|
-
item_last = items.last
|
364
|
-
expect(item_first[:email]).to eq 'henry@baz.com'
|
365
|
-
expect(item_last[:email]).to eq 'john@foo.com'
|
366
|
-
end
|
388
|
+
describe 'it can filter records with condition :gteq' do
|
389
|
+
let(:datatable) { DatatableCondGteq.new(sample_params) }
|
367
390
|
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
391
|
+
before(:each) do
|
392
|
+
create(:user, first_name: 'john', post_id: 1)
|
393
|
+
create(:user, first_name: 'mary', post_id: 2)
|
394
|
+
end
|
395
|
+
|
396
|
+
it 'should filter records matching' do
|
397
|
+
datatable.params[:columns]['4'][:search][:value] = 1
|
398
|
+
expect(datatable.data.size).to eq 2
|
399
|
+
end
|
373
400
|
end
|
374
|
-
end
|
375
401
|
|
376
|
-
|
377
|
-
|
402
|
+
describe 'it can filter records with condition :in' do
|
403
|
+
let(:datatable) { DatatableCondIn.new(sample_params) }
|
378
404
|
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
405
|
+
before(:each) do
|
406
|
+
create(:user, first_name: 'john', post_id: 1)
|
407
|
+
create(:user, first_name: 'mary', post_id: 2)
|
408
|
+
end
|
383
409
|
|
384
|
-
context 'when condition is NULL' do
|
385
410
|
it 'should filter records matching' do
|
386
|
-
datatable.params[:columns]['
|
411
|
+
datatable.params[:columns]['4'][:search][:value] = [1]
|
387
412
|
expect(datatable.data.size).to eq 1
|
388
413
|
item = datatable.data.first
|
389
|
-
expect(item[:first_name]).to eq '
|
414
|
+
expect(item[:first_name]).to eq 'john'
|
390
415
|
end
|
391
416
|
end
|
392
417
|
|
393
|
-
|
418
|
+
describe 'it can filter records with condition :in with regex' do
|
419
|
+
let(:datatable) { DatatableCondInWithRegex.new(sample_params) }
|
420
|
+
|
421
|
+
before(:each) do
|
422
|
+
create(:user, first_name: 'john', post_id: 1)
|
423
|
+
create(:user, first_name: 'mary', post_id: 2)
|
424
|
+
end
|
425
|
+
|
394
426
|
it 'should filter records matching' do
|
395
|
-
datatable.params[:columns]['
|
396
|
-
|
427
|
+
datatable.params[:columns]['4'][:search][:value] = '1|2'
|
428
|
+
datatable.params[:order]['0'] = { column: '4', dir: 'asc' }
|
429
|
+
expect(datatable.data.size).to eq 2
|
397
430
|
item = datatable.data.first
|
398
431
|
expect(item[:first_name]).to eq 'john'
|
399
432
|
end
|
400
433
|
end
|
401
|
-
end
|
402
434
|
|
403
|
-
|
404
|
-
|
435
|
+
describe 'Integer overflows' do
|
436
|
+
let(:datatable) { DatatableCondEq.new(sample_params) }
|
437
|
+
let(:largest_postgresql_integer_value) { 2147483647 }
|
438
|
+
let(:smallest_postgresql_integer_value) { -2147483648 }
|
405
439
|
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
440
|
+
before(:each) do
|
441
|
+
create(:user, first_name: 'john', post_id: 1)
|
442
|
+
create(:user, first_name: 'mary', post_id: 2)
|
443
|
+
create(:user, first_name: 'phil', post_id: largest_postgresql_integer_value)
|
444
|
+
end
|
445
|
+
|
446
|
+
it 'Returns an empty result if input value is too large' do
|
447
|
+
datatable.params[:columns]['4'][:search][:value] = largest_postgresql_integer_value + 1
|
448
|
+
expect(datatable.data.size).to eq 0
|
449
|
+
end
|
450
|
+
|
451
|
+
it 'Returns an empty result if input value is too small' do
|
452
|
+
datatable.params[:columns]['4'][:search][:value] = smallest_postgresql_integer_value - 1
|
453
|
+
expect(datatable.data.size).to eq 0
|
454
|
+
end
|
410
455
|
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
expect(item[:first_name]).to eq 'john'
|
456
|
+
it 'returns the matching user' do
|
457
|
+
datatable.params[:columns]['4'][:search][:value] = largest_postgresql_integer_value
|
458
|
+
expect(datatable.data.size).to eq 1
|
459
|
+
end
|
416
460
|
end
|
417
461
|
end
|
418
462
|
|
419
|
-
|
420
|
-
|
463
|
+
context 'proc condition' do
|
464
|
+
describe 'it can filter records with lambda/proc condition' do
|
465
|
+
let(:datatable) { DatatableCondProc.new(sample_params) }
|
421
466
|
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
467
|
+
before(:each) do
|
468
|
+
create(:user, username: 'johndoe', email: 'johndoe@example.com')
|
469
|
+
create(:user, username: 'johndie', email: 'johndie@example.com')
|
470
|
+
create(:user, username: 'msmith', email: 'mary.smith@example.com')
|
471
|
+
end
|
426
472
|
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
473
|
+
it 'should filter records matching' do
|
474
|
+
datatable.params[:columns]['0'][:search][:value] = 'john'
|
475
|
+
expect(datatable.data.size).to eq 2
|
476
|
+
item = datatable.data.first
|
477
|
+
expect(item[:username]).to eq 'johndie'
|
478
|
+
end
|
432
479
|
end
|
433
480
|
end
|
434
481
|
|
435
|
-
|
436
|
-
|
482
|
+
context 'string condition' do
|
483
|
+
describe 'it can filter records with condition :start_with' do
|
484
|
+
let(:datatable) { DatatableCondStartWith.new(sample_params) }
|
437
485
|
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
486
|
+
before(:each) do
|
487
|
+
create(:user, first_name: 'John')
|
488
|
+
create(:user, first_name: 'Mary')
|
489
|
+
end
|
442
490
|
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
491
|
+
it 'should filter records matching' do
|
492
|
+
datatable.params[:columns]['2'][:search][:value] = 'Jo'
|
493
|
+
expect(datatable.data.size).to eq 1
|
494
|
+
item = datatable.data.first
|
495
|
+
expect(item[:first_name]).to eq 'John'
|
496
|
+
end
|
448
497
|
end
|
449
|
-
end
|
450
498
|
|
451
|
-
|
452
|
-
|
499
|
+
describe 'it can filter records with condition :end_with' do
|
500
|
+
let(:datatable) { DatatableCondEndWith.new(sample_params) }
|
453
501
|
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
502
|
+
before(:each) do
|
503
|
+
create(:user, last_name: 'JOHN')
|
504
|
+
create(:user, last_name: 'MARY')
|
505
|
+
end
|
458
506
|
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
507
|
+
if ENV['DB_ADAPTER'] == 'oracle_enhanced'
|
508
|
+
context 'when db_adapter is oracleenhanced' do
|
509
|
+
it 'should filter records matching' do
|
510
|
+
datatable.params[:columns]['3'][:search][:value] = 'RY'
|
511
|
+
expect(datatable.data.size).to eq 1
|
512
|
+
item = datatable.data.first
|
513
|
+
expect(item[:last_name]).to eq 'MARY'
|
514
|
+
end
|
515
|
+
end
|
516
|
+
else
|
517
|
+
it 'should filter records matching' do
|
518
|
+
datatable.params[:columns]['3'][:search][:value] = 'ry'
|
519
|
+
expect(datatable.data.size).to eq 1
|
520
|
+
item = datatable.data.first
|
521
|
+
expect(item[:last_name]).to eq 'MARY'
|
522
|
+
end
|
523
|
+
end
|
464
524
|
end
|
465
|
-
end
|
466
525
|
|
467
|
-
|
468
|
-
|
526
|
+
describe 'it can filter records with condition :like' do
|
527
|
+
let(:datatable) { DatatableCondLike.new(sample_params) }
|
469
528
|
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
529
|
+
before(:each) do
|
530
|
+
create(:user, email: 'john@foo.com')
|
531
|
+
create(:user, email: 'mary@bar.com')
|
532
|
+
end
|
474
533
|
|
475
|
-
|
476
|
-
|
477
|
-
|
534
|
+
it 'should filter records matching' do
|
535
|
+
datatable.params[:columns]['1'][:search][:value] = 'foo'
|
536
|
+
expect(datatable.data.size).to eq 1
|
537
|
+
item = datatable.data.first
|
538
|
+
expect(item[:email]).to eq 'john@foo.com'
|
539
|
+
end
|
478
540
|
end
|
479
|
-
end
|
480
541
|
|
481
|
-
|
482
|
-
|
542
|
+
describe 'it can filter records with condition :string_eq' do
|
543
|
+
let(:datatable) { DatatableCondStringEq.new(sample_params) }
|
483
544
|
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
545
|
+
before(:each) do
|
546
|
+
create(:user, email: 'john@foo.com')
|
547
|
+
create(:user, email: 'mary@bar.com')
|
548
|
+
end
|
488
549
|
|
489
|
-
|
490
|
-
|
491
|
-
|
550
|
+
it 'should filter records matching' do
|
551
|
+
datatable.params[:columns]['1'][:search][:value] = 'john@foo.com'
|
552
|
+
expect(datatable.data.size).to eq 1
|
553
|
+
item = datatable.data.first
|
554
|
+
expect(item[:email]).to eq 'john@foo.com'
|
555
|
+
end
|
492
556
|
end
|
493
|
-
end
|
494
557
|
|
495
|
-
|
496
|
-
|
558
|
+
describe 'it can filter records with condition :string_in' do
|
559
|
+
let(:datatable) { DatatableCondStringIn.new(sample_params) }
|
497
560
|
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
561
|
+
before(:each) do
|
562
|
+
create(:user, email: 'john@foo.com')
|
563
|
+
create(:user, email: 'mary@bar.com')
|
564
|
+
create(:user, email: 'henry@baz.com')
|
565
|
+
end
|
502
566
|
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
end
|
567
|
+
it 'should filter records matching' do
|
568
|
+
datatable.params[:columns]['1'][:search][:value] = 'john@foo.com'
|
569
|
+
expect(datatable.data.size).to eq 1
|
570
|
+
item = datatable.data.first
|
571
|
+
expect(item[:email]).to eq 'john@foo.com'
|
572
|
+
end
|
510
573
|
|
511
|
-
|
512
|
-
|
574
|
+
it 'should filter records matching with multiple' do
|
575
|
+
datatable.params[:columns]['1'][:search][:value] = 'john@foo.com|henry@baz.com'
|
576
|
+
expect(datatable.data.size).to eq 2
|
577
|
+
items = datatable.data.sort_by { |h| h[:email] }
|
578
|
+
item_first = items.first
|
579
|
+
item_last = items.last
|
580
|
+
expect(item_first[:email]).to eq 'henry@baz.com'
|
581
|
+
expect(item_last[:email]).to eq 'john@foo.com'
|
582
|
+
end
|
513
583
|
|
514
|
-
|
515
|
-
|
516
|
-
|
584
|
+
it 'should filter records matching with multiple contains not found' do
|
585
|
+
datatable.params[:columns]['1'][:search][:value] = 'john@foo.com|henry_not@baz.com'
|
586
|
+
expect(datatable.data.size).to eq 1
|
587
|
+
item = datatable.data.first
|
588
|
+
expect(item[:email]).to eq 'john@foo.com'
|
589
|
+
end
|
517
590
|
end
|
518
591
|
|
519
|
-
|
520
|
-
datatable.
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
592
|
+
describe 'it can filter records with condition :null_value' do
|
593
|
+
let(:datatable) { DatatableCondNullValue.new(sample_params) }
|
594
|
+
|
595
|
+
before(:each) do
|
596
|
+
create(:user, first_name: 'john', email: 'foo@bar.com')
|
597
|
+
create(:user, first_name: 'mary', email: nil)
|
598
|
+
end
|
599
|
+
|
600
|
+
context 'when condition is NULL' do
|
601
|
+
it 'should filter records matching' do
|
602
|
+
datatable.params[:columns]['1'][:search][:value] = 'NULL'
|
603
|
+
expect(datatable.data.size).to eq 1
|
604
|
+
item = datatable.data.first
|
605
|
+
expect(item[:first_name]).to eq 'mary'
|
606
|
+
end
|
607
|
+
end
|
608
|
+
|
609
|
+
context 'when condition is !NULL' do
|
610
|
+
it 'should filter records matching' do
|
611
|
+
datatable.params[:columns]['1'][:search][:value] = '!NULL'
|
612
|
+
expect(datatable.data.size).to eq 1
|
613
|
+
item = datatable.data.first
|
614
|
+
expect(item[:first_name]).to eq 'john'
|
615
|
+
end
|
616
|
+
end
|
525
617
|
end
|
526
618
|
end
|
527
619
|
|
528
|
-
|
529
|
-
let(:datatable) {
|
530
|
-
let(:largest_postgresql_integer_value) { 2147483647 }
|
531
|
-
let(:smallest_postgresql_integer_value) { -2147483648 }
|
620
|
+
context 'unknown condition' do
|
621
|
+
let(:datatable) { DatatableCondUnknown.new(sample_params) }
|
532
622
|
|
533
623
|
before(:each) do
|
534
|
-
|
535
|
-
create(:user, first_name: 'mary', post_id: 2)
|
536
|
-
create(:user, first_name: 'phil', post_id: largest_postgresql_integer_value)
|
624
|
+
datatable.params[:search] = { value: 'john doe', regex: 'false' }
|
537
625
|
end
|
538
626
|
|
539
|
-
it '
|
540
|
-
|
541
|
-
|
627
|
+
it 'should raise error' do
|
628
|
+
expect {
|
629
|
+
datatable.data.size
|
630
|
+
}.to raise_error(AjaxDatatablesRails::Error::InvalidSearchCondition).with_message('foo')
|
542
631
|
end
|
632
|
+
end
|
633
|
+
end
|
543
634
|
|
544
|
-
|
545
|
-
|
546
|
-
expect(datatable.data.size).to eq 0
|
547
|
-
end
|
635
|
+
describe 'formatter option' do
|
636
|
+
let(:datatable) { DatatableWithFormater.new(sample_params) }
|
548
637
|
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
638
|
+
before(:each) do
|
639
|
+
create(:user, username: 'johndoe', email: 'johndoe@example.com', last_name: 'DOE')
|
640
|
+
create(:user, username: 'msmith', email: 'mary.smith@example.com', last_name: 'SMITH')
|
641
|
+
datatable.params[:columns]['3'][:search][:value] = 'doe'
|
642
|
+
end
|
643
|
+
|
644
|
+
it 'can transform search value before asking the database' do
|
645
|
+
expect(datatable.data.size).to eq 1
|
646
|
+
item = datatable.data.first
|
647
|
+
expect(item[:last_name]).to eq 'DOE'
|
553
648
|
end
|
554
649
|
end
|
555
650
|
end
|