Skip to content

Commit 14e4bbe

Browse files
author
Nicolas Rodriguez
committed
Rework datatable definitions for testing
1 parent da6d07b commit 14e4bbe

12 files changed

+119
-120
lines changed

spec/ajax-datatables-rails/base_spec.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
end
2626

2727
context 'child class implements view_columns' do
28-
it 'expects an array based defining columns' do
29-
datatable = SampleDatatable.new(view)
30-
expect(datatable.view_columns).to be_a(Array)
31-
end
32-
3328
it 'expects a hash based defining columns' do
3429
datatable = ComplexDatatable.new(view)
3530
expect(datatable.view_columns).to be_a(Hash)
@@ -51,7 +46,7 @@
5146
end
5247

5348
context 'when data is defined as a hash' do
54-
let(:datatable) { ComplexDatatableHash.new(view) }
49+
let(:datatable) { ComplexDatatable.new(view) }
5550

5651
it 'should return an array of hashes' do
5752
create_list(:user, 5)
@@ -92,7 +87,7 @@
9287
end
9388

9489
describe '#as_json' do
95-
let(:datatable) { ComplexDatatableHash.new(view) }
90+
let(:datatable) { ComplexDatatable.new(view) }
9691

9792
it 'should return a hash' do
9893
create_list(:user, 5)

spec/ajax-datatables-rails/datatable/column_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
end
8282

8383
describe '#formater' do
84+
let(:datatable) { DatatableWithFormater.new(view) }
8485
let(:column) { datatable.datatable.columns.find { |c| c.data == 'last_name' } }
8586

8687
it 'should be a proc' do
@@ -89,7 +90,7 @@
8990
end
9091

9192
describe '#filter' do
92-
let(:datatable) { ReallyComplexDatatableProc.new(view) }
93+
let(:datatable) { DatatableCondProc.new(view) }
9394
let(:column) { datatable.datatable.columns.find { |c| c.data == 'username' } }
9495

9596
it 'should be a proc' do

spec/ajax-datatables-rails/extended_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
describe AjaxDatatablesRails::Base do
44
let(:view) { double('view', params: sample_params) }
5-
let(:datatable) { ReallyComplexDatatable.new(view) }
5+
let(:datatable) { DatatableWithFormater.new(view) }
66

77
describe 'it can transform search value before asking the database' do
88
before(:each) do

spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@
234234
end
235235

236236
describe 'filter conditions' do
237-
let(:datatable) { ReallyComplexDatatable.new(view) }
238-
239237
unless AjaxDatatablesRails.old_rails?
240238
describe 'it can filter records with condition :date_range' do
239+
let(:datatable) { DatatableCondDate.new(view) }
240+
241241
before(:each) do
242242
create(:user, username: 'johndoe', email: 'johndoe@example.com', last_name: 'Doe', created_at: '01/01/2000')
243243
create(:user, username: 'msmith', email: 'mary.smith@example.com', last_name: 'Smith', created_at: '01/02/2000')
@@ -318,6 +318,8 @@
318318
end
319319

320320
describe 'it can filter records with condition :start_with' do
321+
let(:datatable) { DatatableCondStartWith.new(view) }
322+
321323
before(:each) do
322324
create(:user, first_name: 'John')
323325
create(:user, first_name: 'Mary')
@@ -332,6 +334,8 @@
332334
end
333335

334336
describe 'it can filter records with condition :end_with' do
337+
let(:datatable) { DatatableCondEndWith.new(view) }
338+
335339
before(:each) do
336340
create(:user, last_name: 'JOHN')
337341
create(:user, last_name: 'MARY')
@@ -346,6 +350,8 @@
346350
end
347351

348352
describe 'it can filter records with condition :null_value' do
353+
let(:datatable) { DatatableCondNullValue.new(view) }
354+
349355
before(:each) do
350356
create(:user, first_name: 'john', email: 'foo@bar.com')
351357
create(:user, first_name: 'mary', email: nil)
@@ -371,7 +377,7 @@
371377
end
372378

373379
describe 'it can filter records with condition :eq' do
374-
let(:datatable) { ReallyComplexDatatableEq.new(view) }
380+
let(:datatable) { DatatableCondEq.new(view) }
375381

376382
before(:each) do
377383
create(:user, first_name: 'john', post_id: 1)
@@ -387,7 +393,7 @@
387393
end
388394

389395
describe 'it can filter records with condition :not_eq' do
390-
let(:datatable) { ReallyComplexDatatableNotEq.new(view) }
396+
let(:datatable) { DatatableCondNotEq.new(view) }
391397

392398
before(:each) do
393399
create(:user, first_name: 'john', post_id: 1)
@@ -403,7 +409,7 @@
403409
end
404410

405411
describe 'it can filter records with condition :lt' do
406-
let(:datatable) { ReallyComplexDatatableLt.new(view) }
412+
let(:datatable) { DatatableCondLt.new(view) }
407413

408414
before(:each) do
409415
create(:user, first_name: 'john', post_id: 1)
@@ -419,7 +425,7 @@
419425
end
420426

421427
describe 'it can filter records with condition :gt' do
422-
let(:datatable) { ReallyComplexDatatableGt.new(view) }
428+
let(:datatable) { DatatableCondGt.new(view) }
423429

424430
before(:each) do
425431
create(:user, first_name: 'john', post_id: 1)
@@ -435,7 +441,7 @@
435441
end
436442

437443
describe 'it can filter records with condition :lteq' do
438-
let(:datatable) { ReallyComplexDatatableLteq.new(view) }
444+
let(:datatable) { DatatableCondLteq.new(view) }
439445

440446
before(:each) do
441447
create(:user, first_name: 'john', post_id: 1)
@@ -449,7 +455,7 @@
449455
end
450456

451457
describe 'it can filter records with condition :gteq' do
452-
let(:datatable) { ReallyComplexDatatableGteq.new(view) }
458+
let(:datatable) { DatatableCondGteq.new(view) }
453459

454460
before(:each) do
455461
create(:user, first_name: 'john', post_id: 1)
@@ -463,7 +469,7 @@
463469
end
464470

465471
describe 'it can filter records with condition :in' do
466-
let(:datatable) { ReallyComplexDatatableIn.new(view) }
472+
let(:datatable) { DatatableCondIn.new(view) }
467473

468474
before(:each) do
469475
create(:user, first_name: 'john', post_id: 1)

spec/ajax-datatables-rails/orm/active_record_paginate_records_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
describe AjaxDatatablesRails::ORM::ActiveRecord do
44

55
let(:view) { double('view', params: sample_params) }
6-
let(:datatable) { SampleDatatable.new(view) }
6+
let(:datatable) { ComplexDatatable.new(view) }
77
let(:records) { User.all }
88

99
before(:each) do

spec/ajax-datatables-rails/orm/active_record_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
describe AjaxDatatablesRails::ORM::ActiveRecord do
44
context 'Private API' do
55
let(:view) { double('view', params: sample_params) }
6-
let(:datatable) { SampleDatatable.new(view) }
6+
let(:datatable) { ComplexDatatable.new(view) }
77

88
before(:each) do
99
create(:user, username: 'johndoe', email: 'johndoe@example.com')

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,8 @@
7878

7979
load File.dirname(__FILE__) + '/support/schema.rb'
8080
load File.dirname(__FILE__) + '/support/test_helpers.rb'
81+
load File.dirname(__FILE__) + '/support/datatable_cond_date.rb'
82+
load File.dirname(__FILE__) + '/support/datatable_cond_numeric.rb'
83+
load File.dirname(__FILE__) + '/support/datatable_cond_proc.rb'
84+
load File.dirname(__FILE__) + '/support/datatable_cond_string.rb'
8185
require File.dirname(__FILE__) + '/support/test_models.rb'

spec/support/datatable_cond_date.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class DatatableCondDate < ComplexDatatable
2+
def view_columns
3+
super.deep_merge(created_at: { cond: :date_range })
4+
end
5+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class DatatableCondEq < ComplexDatatable
2+
def view_columns
3+
super.deep_merge(post_id: { cond: :eq })
4+
end
5+
end
6+
7+
class DatatableCondNotEq < ComplexDatatable
8+
def view_columns
9+
super.deep_merge(post_id: { cond: :not_eq })
10+
end
11+
end
12+
13+
class DatatableCondLt < ComplexDatatable
14+
def view_columns
15+
super.deep_merge(post_id: { cond: :lt })
16+
end
17+
end
18+
19+
class DatatableCondGt < ComplexDatatable
20+
def view_columns
21+
super.deep_merge(post_id: { cond: :gt })
22+
end
23+
end
24+
25+
class DatatableCondLteq < ComplexDatatable
26+
def view_columns
27+
super.deep_merge(post_id: { cond: :lteq })
28+
end
29+
end
30+
31+
class DatatableCondGteq < ComplexDatatable
32+
def view_columns
33+
super.deep_merge(post_id: { cond: :gteq })
34+
end
35+
end
36+
37+
class DatatableCondIn < ComplexDatatable
38+
def view_columns
39+
super.deep_merge(post_id: { cond: :in })
40+
end
41+
end

spec/support/datatable_cond_proc.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class DatatableCondProc < ComplexDatatable
2+
def view_columns
3+
super.deep_merge(username: { cond: custom_filter })
4+
end
5+
6+
private
7+
8+
def custom_filter
9+
->(column, value) { ::Arel::Nodes::SqlLiteral.new(column.field.to_s).matches("#{value}%") }
10+
end
11+
end

0 commit comments

Comments
 (0)