ajax-datatables-rails 0.4.2 → 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 +17 -7
- data/Appraisals +15 -20
- data/CHANGELOG.md +56 -4
- data/Gemfile +0 -5
- data/Guardfile +16 -0
- data/README.md +223 -97
- data/Rakefile +1 -0
- data/ajax-datatables-rails.gemspec +24 -20
- data/bin/_guard-core +29 -0
- data/bin/appraisal +29 -0
- data/bin/guard +29 -0
- data/bin/rake +29 -0
- data/bin/rspec +29 -0
- data/bin/rubocop +29 -0
- data/doc/migrate.md +97 -0
- data/doc/webpack.md +7 -2
- data/gemfiles/{rails_5.2.0.gemfile → rails_5.2.4.gemfile} +3 -5
- data/gemfiles/{rails_5.0.7.gemfile → rails_6.0.3.gemfile} +4 -6
- data/gemfiles/{rails_5.1.6.gemfile → rails_6.1.0.gemfile} +4 -6
- data/lib/ajax-datatables-rails.rb +12 -1
- data/lib/ajax-datatables-rails/active_record.rb +7 -0
- data/lib/ajax-datatables-rails/base.rb +47 -46
- data/lib/ajax-datatables-rails/datatable.rb +6 -0
- data/lib/ajax-datatables-rails/datatable/column.rb +65 -20
- data/lib/ajax-datatables-rails/datatable/column/date_filter.rb +12 -21
- data/lib/ajax-datatables-rails/datatable/column/order.rb +1 -1
- data/lib/ajax-datatables-rails/datatable/column/search.rb +36 -23
- data/lib/ajax-datatables-rails/datatable/datatable.rb +16 -6
- data/lib/ajax-datatables-rails/datatable/simple_order.rb +23 -10
- data/lib/ajax-datatables-rails/datatable/simple_search.rb +2 -0
- 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 +11 -12
- data/lib/ajax-datatables-rails/version.rb +13 -1
- data/lib/generators/rails/templates/datatable.rb +1 -1
- data/spec/ajax-datatables-rails/base_spec.rb +95 -79
- data/spec/ajax-datatables-rails/datatable/column_spec.rb +83 -12
- data/spec/ajax-datatables-rails/datatable/datatable_spec.rb +62 -24
- data/spec/ajax-datatables-rails/datatable/simple_order_spec.rb +32 -12
- data/spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb +329 -221
- data/spec/ajax-datatables-rails/orm/active_record_paginate_records_spec.rb +5 -6
- data/spec/ajax-datatables-rails/orm/active_record_sort_records_spec.rb +12 -11
- data/spec/ajax-datatables-rails/orm/active_record_spec.rb +3 -4
- data/spec/install_oracle.sh +9 -3
- data/spec/spec_helper.rb +10 -21
- 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} +1 -1
- data/spec/support/{datatable_cond_proc.rb → datatables/datatable_cond_proc.rb} +0 -0
- data/spec/support/datatables/datatable_cond_string.rb +41 -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} +14 -41
- data/spec/support/{test_models.rb → models/user.rb} +0 -0
- metadata +75 -74
- data/.travis.yml +0 -80
- data/gemfiles/rails_4.0.13.gemfile +0 -14
- data/gemfiles/rails_4.1.16.gemfile +0 -14
- data/gemfiles/rails_4.2.10.gemfile +0 -14
- data/lib/ajax-datatables-rails/config.rb +0 -31
- data/lib/ajax_datatables_rails.rb +0 -15
- data/lib/generators/datatable/config_generator.rb +0 -19
- data/lib/generators/datatable/templates/ajax_datatables_rails_config.rb +0 -12
- data/spec/ajax-datatables-rails/configuration_spec.rb +0 -43
- data/spec/ajax-datatables-rails/extended_spec.rb +0 -20
- data/spec/support/datatable_cond_string.rb +0 -23
@@ -2,8 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe AjaxDatatablesRails::Datatable::Column do
|
4
4
|
|
5
|
-
let(:
|
6
|
-
let(:datatable) { ComplexDatatable.new(view) }
|
5
|
+
let(:datatable) { ComplexDatatable.new(sample_params) }
|
7
6
|
|
8
7
|
describe 'username column' do
|
9
8
|
|
@@ -121,34 +120,106 @@ describe AjaxDatatablesRails::Datatable::Column do
|
|
121
120
|
end
|
122
121
|
end
|
123
122
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
expect(column.delimiter).to eq('-')
|
128
|
-
end
|
123
|
+
describe '#delimiter' do
|
124
|
+
it 'should be - by default' do
|
125
|
+
expect(column.delimiter).to eq('-')
|
129
126
|
end
|
130
127
|
end
|
131
128
|
end
|
132
129
|
|
133
|
-
describe '#
|
134
|
-
let(:datatable) { DatatableWithFormater.new(
|
130
|
+
describe '#formatter' do
|
131
|
+
let(:datatable) { DatatableWithFormater.new(sample_params) }
|
135
132
|
let(:column) { datatable.datatable.columns.find { |c| c.data == 'last_name' } }
|
136
133
|
|
137
134
|
it 'should be a proc' do
|
138
|
-
expect(column.
|
135
|
+
expect(column.formatter).to be_a(Proc)
|
139
136
|
end
|
140
137
|
end
|
141
138
|
|
142
139
|
describe '#filter' do
|
143
|
-
let(:datatable) { DatatableCondProc.new(
|
140
|
+
let(:datatable) { DatatableCondProc.new(sample_params) }
|
144
141
|
let(:column) { datatable.datatable.columns.find { |c| c.data == 'username' } }
|
145
142
|
|
146
143
|
it 'should be a proc' do
|
147
144
|
config = column.instance_variable_get('@view_column')
|
148
145
|
filter = config[:cond]
|
149
146
|
expect(filter).to be_a(Proc)
|
150
|
-
expect(filter).to receive(:call).with(column, column.
|
147
|
+
expect(filter).to receive(:call).with(column, column.formatted_value)
|
151
148
|
column.filter
|
152
149
|
end
|
153
150
|
end
|
151
|
+
|
152
|
+
describe '#type_cast' do
|
153
|
+
let(:column) { datatable.datatable.columns.first }
|
154
|
+
|
155
|
+
it 'returns VARCHAR if :db_adapter is :pg' do
|
156
|
+
expect(datatable).to receive(:db_adapter) { :pg }
|
157
|
+
expect(column.send(:type_cast)).to eq('VARCHAR')
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'returns VARCHAR if :db_adapter is :postgre' do
|
161
|
+
expect(datatable).to receive(:db_adapter) { :postgre }
|
162
|
+
expect(column.send(:type_cast)).to eq('VARCHAR')
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'returns VARCHAR if :db_adapter is :postgresql' do
|
166
|
+
expect(datatable).to receive(:db_adapter) { :postgresql }
|
167
|
+
expect(column.send(:type_cast)).to eq('VARCHAR')
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'returns VARCHAR2(4000) if :db_adapter is :oracle' do
|
171
|
+
expect(datatable).to receive(:db_adapter) { :oracle }
|
172
|
+
expect(column.send(:type_cast)).to eq('VARCHAR2(4000)')
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'returns VARCHAR2(4000) if :db_adapter is :oracleenhanced' do
|
176
|
+
expect(datatable).to receive(:db_adapter) { :oracleenhanced }
|
177
|
+
expect(column.send(:type_cast)).to eq('VARCHAR2(4000)')
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'returns CHAR if :db_adapter is :mysql2' do
|
181
|
+
expect(datatable).to receive(:db_adapter) { :mysql2 }
|
182
|
+
expect(column.send(:type_cast)).to eq('CHAR')
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'returns CHAR if :db_adapter is :mysql' do
|
186
|
+
expect(datatable).to receive(:db_adapter) { :mysql }
|
187
|
+
expect(column.send(:type_cast)).to eq('CHAR')
|
188
|
+
end
|
189
|
+
|
190
|
+
it 'returns TEXT if :db_adapter is :sqlite' do
|
191
|
+
expect(datatable).to receive(:db_adapter) { :sqlite }
|
192
|
+
expect(column.send(:type_cast)).to eq('TEXT')
|
193
|
+
end
|
194
|
+
|
195
|
+
it 'returns TEXT if :db_adapter is :sqlite3' do
|
196
|
+
expect(datatable).to receive(:db_adapter) { :sqlite3 }
|
197
|
+
expect(column.send(:type_cast)).to eq('TEXT')
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'returns VARCHAR(4000) if :db_adapter is :sqlserver' do
|
201
|
+
expect(datatable).to receive(:db_adapter) { :sqlserver }
|
202
|
+
expect(column.send(:type_cast)).to eq('VARCHAR(4000)')
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe 'when empty column' do
|
207
|
+
before do
|
208
|
+
datatable.params[:columns] = {'0'=>{'data'=>'', 'name'=>'', 'searchable'=>'true', 'orderable'=>'true', 'search'=>{'value'=>'searchvalue', 'regex'=>'false'}}}
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'raises error' do
|
212
|
+
expect { datatable.to_json }.to raise_error(AjaxDatatablesRails::Error::InvalidSearchColumn).with_message("Unknown column. Check that `data` field is filled on JS side with the column name")
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe 'when unknown column' do
|
217
|
+
before do
|
218
|
+
datatable.params[:columns] = {'0'=>{'data'=>'foo', 'name'=>'', 'searchable'=>'true', 'orderable'=>'true', 'search'=>{'value'=>'searchvalue', 'regex'=>'false'}}}
|
219
|
+
end
|
220
|
+
|
221
|
+
it 'raises error' do
|
222
|
+
expect { datatable.to_json }.to raise_error(AjaxDatatablesRails::Error::InvalidSearchColumn).with_message("Check that column 'foo' exists in view_columns")
|
223
|
+
end
|
224
|
+
end
|
154
225
|
end
|
@@ -2,11 +2,12 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe AjaxDatatablesRails::Datatable::Datatable do
|
4
4
|
|
5
|
-
let(:
|
6
|
-
let(:
|
5
|
+
let(:datatable) { ComplexDatatable.new(sample_params).datatable }
|
6
|
+
let(:datatable_json) { ComplexDatatable.new(sample_params_json).datatable }
|
7
7
|
let(:order_option) { {'0'=>{'column'=>'0', 'dir'=>'asc'}, '1'=>{'column'=>'1', 'dir'=>'desc'}} }
|
8
|
+
let(:order_option_json) { [{'column'=>'0', 'dir'=>'asc'}, {'column'=>'1', 'dir'=>'desc'}] }
|
8
9
|
|
9
|
-
|
10
|
+
shared_examples 'order methods' do
|
10
11
|
it 'should be orderable' do
|
11
12
|
expect(datatable.orderable?).to eq(true)
|
12
13
|
end
|
@@ -36,6 +37,28 @@ describe AjaxDatatablesRails::Datatable::Datatable do
|
|
36
37
|
end
|
37
38
|
end
|
38
39
|
|
40
|
+
shared_examples 'columns methods' do
|
41
|
+
it 'should have 4 columns' do
|
42
|
+
expect(datatable.columns.count).to eq(6)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'child class' do
|
46
|
+
expect(datatable.columns.first).to be_a(AjaxDatatablesRails::Datatable::Column)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'with query params' do
|
51
|
+
it_behaves_like 'order methods'
|
52
|
+
it_behaves_like 'columns methods'
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'with json params' do
|
56
|
+
let(:order_option) { order_option_json }
|
57
|
+
let(:datatable) { datatable_json }
|
58
|
+
it_behaves_like 'order methods'
|
59
|
+
it_behaves_like 'columns methods'
|
60
|
+
end
|
61
|
+
|
39
62
|
describe 'search methods' do
|
40
63
|
it 'should be searchable' do
|
41
64
|
datatable.options[:search][:value] = 'atom'
|
@@ -52,36 +75,51 @@ describe AjaxDatatablesRails::Datatable::Datatable do
|
|
52
75
|
end
|
53
76
|
end
|
54
77
|
|
55
|
-
describe '
|
56
|
-
|
57
|
-
|
78
|
+
describe 'option methods' do
|
79
|
+
describe '#paginate?' do
|
80
|
+
it {
|
81
|
+
expect(datatable.paginate?).to be(true)
|
82
|
+
}
|
58
83
|
end
|
59
84
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
85
|
+
describe '#per_page' do
|
86
|
+
context 'when params[:length] is missing' do
|
87
|
+
it 'defaults to 10' do
|
88
|
+
expect(datatable.per_page).to eq(10)
|
89
|
+
end
|
90
|
+
end
|
64
91
|
|
65
|
-
|
66
|
-
|
67
|
-
datatable.options[:start] = '50'
|
68
|
-
datatable.options[:length] = '15'
|
69
|
-
end
|
92
|
+
context 'when params[:length] is passed' do
|
93
|
+
let(:datatable) { ComplexDatatable.new({ length: '20' }).datatable }
|
70
94
|
|
71
|
-
|
72
|
-
|
95
|
+
it 'matches the value on view params[:length]' do
|
96
|
+
expect(datatable.per_page).to eq(20)
|
97
|
+
end
|
98
|
+
end
|
73
99
|
end
|
74
100
|
|
75
|
-
|
76
|
-
|
77
|
-
|
101
|
+
describe '#offset' do
|
102
|
+
context 'when params[:start] is missing' do
|
103
|
+
it 'defaults to 0' do
|
104
|
+
expect(datatable.offset).to eq(0)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'when params[:start] is passed' do
|
109
|
+
let(:datatable) { ComplexDatatable.new({ start: '11' }).datatable }
|
78
110
|
|
79
|
-
|
80
|
-
|
111
|
+
it 'matches the value on view params[:start]' do
|
112
|
+
expect(datatable.offset).to eq(11)
|
113
|
+
end
|
114
|
+
end
|
81
115
|
end
|
82
116
|
|
83
|
-
|
84
|
-
|
117
|
+
describe '#page' do
|
118
|
+
let(:datatable) { ComplexDatatable.new({ start: '11' }).datatable }
|
119
|
+
|
120
|
+
it 'calculates page number from params[:start] and #per_page' do
|
121
|
+
expect(datatable.page).to eq(2)
|
122
|
+
end
|
85
123
|
end
|
86
124
|
end
|
87
125
|
end
|
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe AjaxDatatablesRails::Datatable::SimpleOrder do
|
4
4
|
|
5
|
-
let(:
|
6
|
-
let(:datatable) {
|
5
|
+
let(:parent) { ComplexDatatable.new(sample_params) }
|
6
|
+
let(:datatable) { parent.datatable }
|
7
7
|
let(:options) { ActiveSupport::HashWithIndifferentAccess.new({'column' => '1', 'dir' => 'desc'}) }
|
8
8
|
let(:simple_order) { AjaxDatatablesRails::Datatable::SimpleOrder.new(datatable, options) }
|
9
9
|
|
@@ -14,27 +14,47 @@ describe AjaxDatatablesRails::Datatable::SimpleOrder do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
describe 'option methods with nulls last' do
|
17
|
-
describe 'using
|
18
|
-
before {
|
19
|
-
after {
|
17
|
+
describe 'using class option' do
|
18
|
+
before { parent.nulls_last = true }
|
19
|
+
after { parent.nulls_last = false }
|
20
20
|
|
21
21
|
it 'sql query' do
|
22
|
+
skip('unsupported database adapter') if ENV['DB_ADAPTER'] == 'oracle_enhanced'
|
23
|
+
|
22
24
|
expect(simple_order.query('email')).to eq(
|
23
|
-
|
25
|
+
"email DESC #{nulls_last_sql(parent)}"
|
24
26
|
)
|
25
27
|
end
|
26
28
|
end
|
27
29
|
|
28
30
|
describe 'using column option' do
|
29
|
-
let(:
|
31
|
+
let(:parent) { DatatableOrderNullsLast.new(sample_params) }
|
32
|
+
let(:sorted_datatable) { parent.datatable }
|
30
33
|
let(:nulls_last_order) { AjaxDatatablesRails::Datatable::SimpleOrder.new(sorted_datatable, options) }
|
31
34
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
35
|
+
context 'with postgres database adapter' do
|
36
|
+
before { parent.db_adapter = :pg }
|
37
|
+
|
38
|
+
it 'sql query' do
|
39
|
+
expect(nulls_last_order.query('email')).to eq('email DESC NULLS LAST')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'with sqlite database adapter' do
|
44
|
+
before { parent.db_adapter = :sqlite }
|
45
|
+
|
46
|
+
it 'sql query' do
|
47
|
+
expect(nulls_last_order.query('email')).to eq('email DESC IS NULL')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'with mysql database adapter' do
|
52
|
+
before { parent.db_adapter = :mysql }
|
53
|
+
|
54
|
+
it 'sql query' do
|
55
|
+
expect(nulls_last_order.query('email')).to eq('email DESC IS NULL')
|
56
|
+
end
|
36
57
|
end
|
37
58
|
end
|
38
59
|
end
|
39
|
-
|
40
60
|
end
|
@@ -2,25 +2,47 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe AjaxDatatablesRails::ORM::ActiveRecord do
|
4
4
|
|
5
|
-
let(:
|
6
|
-
let(:datatable) { ComplexDatatable.new(view) }
|
5
|
+
let(:datatable) { ComplexDatatable.new(sample_params) }
|
7
6
|
let(:records) { User.all }
|
8
7
|
|
9
8
|
describe '#filter_records' do
|
10
9
|
it 'requires a records collection as argument' do
|
11
|
-
expect { datatable.
|
10
|
+
expect { datatable.filter_records() }.to raise_error(ArgumentError)
|
12
11
|
end
|
13
12
|
|
14
13
|
it 'performs a simple search first' do
|
15
14
|
datatable.params[:search] = { value: 'msmith' }
|
16
15
|
expect(datatable).to receive(:build_conditions_for_datatable)
|
17
|
-
datatable.
|
16
|
+
datatable.filter_records(records)
|
18
17
|
end
|
19
18
|
|
20
19
|
it 'performs a composite search second' do
|
21
20
|
datatable.params[:search] = { value: '' }
|
22
21
|
expect(datatable).to receive(:build_conditions_for_selected_columns)
|
23
|
-
datatable.
|
22
|
+
datatable.filter_records(records)
|
23
|
+
end
|
24
|
+
end
|
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
|
24
46
|
end
|
25
47
|
end
|
26
48
|
|
@@ -32,14 +54,14 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
32
54
|
|
33
55
|
it 'returns an Arel object' do
|
34
56
|
datatable.params[:search] = { value: 'msmith' }
|
35
|
-
result = datatable.
|
57
|
+
result = datatable.build_conditions_for_datatable
|
36
58
|
expect(result).to be_a(Arel::Nodes::Grouping)
|
37
59
|
end
|
38
60
|
|
39
61
|
context 'no search query' do
|
40
62
|
it 'returns empty query' do
|
41
63
|
datatable.params[:search] = { value: '' }
|
42
|
-
expect(datatable.
|
64
|
+
expect(datatable.build_conditions_for_datatable).to be_blank
|
43
65
|
end
|
44
66
|
end
|
45
67
|
|
@@ -54,11 +76,11 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
54
76
|
end
|
55
77
|
|
56
78
|
it 'returns empty query result' do
|
57
|
-
expect(datatable.
|
79
|
+
expect(datatable.build_conditions_for_datatable).to be_blank
|
58
80
|
end
|
59
81
|
|
60
82
|
it 'returns filtered results' do
|
61
|
-
query = datatable.
|
83
|
+
query = datatable.build_conditions_for_datatable
|
62
84
|
results = records.where(query).map(&:username)
|
63
85
|
expect(results).to eq ['johndoe', 'msmith']
|
64
86
|
end
|
@@ -70,11 +92,11 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
70
92
|
end
|
71
93
|
|
72
94
|
it 'returns empty query result' do
|
73
|
-
expect(datatable.
|
95
|
+
expect(datatable.build_conditions_for_datatable).to be_blank
|
74
96
|
end
|
75
97
|
|
76
98
|
it 'returns filtered results' do
|
77
|
-
query = datatable.
|
99
|
+
query = datatable.build_conditions_for_datatable
|
78
100
|
results = records.where(query).map(&:username)
|
79
101
|
expect(results).to eq ['johndoe', 'msmith']
|
80
102
|
end
|
@@ -88,7 +110,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
88
110
|
end
|
89
111
|
|
90
112
|
it 'returns a filtering query' do
|
91
|
-
query = datatable.
|
113
|
+
query = datatable.build_conditions_for_datatable
|
92
114
|
results = records.where(query).map(&:username)
|
93
115
|
expect(results).to include('johndoe')
|
94
116
|
expect(results).not_to include('msmith')
|
@@ -101,12 +123,29 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
101
123
|
end
|
102
124
|
|
103
125
|
it 'returns a filtering query' do
|
104
|
-
query = datatable.
|
126
|
+
query = datatable.build_conditions_for_datatable
|
105
127
|
results = records.where(query).map(&:username)
|
106
128
|
expect(results).to eq ['johndoe']
|
107
129
|
expect(results).not_to include('msmith')
|
108
130
|
end
|
109
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
|
110
149
|
end
|
111
150
|
end
|
112
151
|
|
@@ -123,14 +162,14 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
123
162
|
end
|
124
163
|
|
125
164
|
it 'returns an Arel object' do
|
126
|
-
result = datatable.
|
165
|
+
result = datatable.build_conditions_for_selected_columns
|
127
166
|
expect(result).to be_a(Arel::Nodes::And)
|
128
167
|
end
|
129
168
|
|
130
|
-
if
|
169
|
+
if ENV['DB_ADAPTER'] == 'postgresql'
|
131
170
|
context 'when db_adapter is postgresql' do
|
132
171
|
it 'can call #to_sql on returned object' do
|
133
|
-
result = datatable.
|
172
|
+
result = datatable.build_conditions_for_selected_columns
|
134
173
|
expect(result).to respond_to(:to_sql)
|
135
174
|
expect(result.to_sql).to eq(
|
136
175
|
"CAST(\"users\".\"username\" AS VARCHAR) ILIKE '%doe%' AND CAST(\"users\".\"email\" AS VARCHAR) ILIKE '%example%'"
|
@@ -139,10 +178,10 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
139
178
|
end
|
140
179
|
end
|
141
180
|
|
142
|
-
if
|
181
|
+
if ENV['DB_ADAPTER'] == 'oracle_enhanced'
|
143
182
|
context 'when db_adapter is oracle' do
|
144
183
|
it 'can call #to_sql on returned object' do
|
145
|
-
result = datatable.
|
184
|
+
result = datatable.build_conditions_for_selected_columns
|
146
185
|
expect(result).to respond_to(:to_sql)
|
147
186
|
expect(result.to_sql).to eq(
|
148
187
|
"CAST(\"USERS\".\"USERNAME\" AS VARCHAR2(4000)) LIKE '%doe%' AND CAST(\"USERS\".\"EMAIL\" AS VARCHAR2(4000)) LIKE '%example%'"
|
@@ -151,10 +190,10 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
151
190
|
end
|
152
191
|
end
|
153
192
|
|
154
|
-
if
|
193
|
+
if ENV['DB_ADAPTER'] == 'mysql2'
|
155
194
|
context 'when db_adapter is mysql2' do
|
156
195
|
it 'can call #to_sql on returned object' do
|
157
|
-
result = datatable.
|
196
|
+
result = datatable.build_conditions_for_selected_columns
|
158
197
|
expect(result).to respond_to(:to_sql)
|
159
198
|
expect(result.to_sql).to eq(
|
160
199
|
"CAST(`users`.`username` AS CHAR) LIKE '%doe%' AND CAST(`users`.`email` AS CHAR) LIKE '%example%'"
|
@@ -166,7 +205,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
166
205
|
|
167
206
|
it 'calls #build_conditions_for_selected_columns' do
|
168
207
|
expect(datatable).to receive(:build_conditions_for_selected_columns)
|
169
|
-
datatable.
|
208
|
+
datatable.build_conditions
|
170
209
|
end
|
171
210
|
|
172
211
|
context 'with search values in columns' do
|
@@ -175,7 +214,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
175
214
|
end
|
176
215
|
|
177
216
|
it 'returns a filtered set of records' do
|
178
|
-
query = datatable.
|
217
|
+
query = datatable.build_conditions_for_selected_columns
|
179
218
|
results = records.where(query).map(&:username)
|
180
219
|
expect(results).to include('johndoe')
|
181
220
|
expect(results).not_to include('msmith')
|
@@ -183,60 +222,10 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
183
222
|
end
|
184
223
|
end
|
185
224
|
|
186
|
-
describe '#type_cast helper method' do
|
187
|
-
let(:view) { double('view', params: sample_params) }
|
188
|
-
let(:column) { ComplexDatatable.new(view).datatable.columns.first }
|
189
|
-
|
190
|
-
it 'returns VARCHAR if :db_adapter is :pg' do
|
191
|
-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :pg }
|
192
|
-
expect(column.send(:type_cast)).to eq('VARCHAR')
|
193
|
-
end
|
194
|
-
|
195
|
-
it 'returns VARCHAR if :db_adapter is :postgre' do
|
196
|
-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :postgre }
|
197
|
-
expect(column.send(:type_cast)).to eq('VARCHAR')
|
198
|
-
end
|
199
|
-
|
200
|
-
it 'returns VARCHAR if :db_adapter is :postgresql' do
|
201
|
-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :postgresql }
|
202
|
-
expect(column.send(:type_cast)).to eq('VARCHAR')
|
203
|
-
end
|
204
|
-
|
205
|
-
it 'returns VARCHAR if :db_adapter is :oracle' do
|
206
|
-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :oracle }
|
207
|
-
expect(column.send(:type_cast)).to eq('VARCHAR2(4000)')
|
208
|
-
end
|
209
|
-
|
210
|
-
it 'returns VARCHAR if :db_adapter is :oracleenhanced' do
|
211
|
-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :oracleenhanced }
|
212
|
-
expect(column.send(:type_cast)).to eq('VARCHAR2(4000)')
|
213
|
-
end
|
214
|
-
|
215
|
-
it 'returns CHAR if :db_adapter is :mysql2' do
|
216
|
-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :mysql2 }
|
217
|
-
expect(column.send(:type_cast)).to eq('CHAR')
|
218
|
-
end
|
219
|
-
|
220
|
-
it 'returns CHAR if :db_adapter is :mysql' do
|
221
|
-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :mysql }
|
222
|
-
expect(column.send(:type_cast)).to eq('CHAR')
|
223
|
-
end
|
224
|
-
|
225
|
-
it 'returns TEXT if :db_adapter is :sqlite' do
|
226
|
-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :sqlite }
|
227
|
-
expect(column.send(:type_cast)).to eq('TEXT')
|
228
|
-
end
|
229
|
-
|
230
|
-
it 'returns TEXT if :db_adapter is :sqlite3' do
|
231
|
-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :sqlite3 }
|
232
|
-
expect(column.send(:type_cast)).to eq('TEXT')
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
225
|
describe 'filter conditions' do
|
237
|
-
|
226
|
+
context 'date condition' do
|
238
227
|
describe 'it can filter records with condition :date_range' do
|
239
|
-
let(:datatable) { DatatableCondDate.new(
|
228
|
+
let(:datatable) { DatatableCondDate.new(sample_params) }
|
240
229
|
|
241
230
|
before(:each) do
|
242
231
|
create(:user, username: 'johndoe', email: 'johndoe@example.com', last_name: 'Doe', created_at: '01/01/2000')
|
@@ -317,226 +306,345 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
317
306
|
end
|
318
307
|
end
|
319
308
|
|
320
|
-
|
321
|
-
|
309
|
+
context 'numeric condition' do
|
310
|
+
describe 'it can filter records with condition :eq' do
|
311
|
+
let(:datatable) { DatatableCondEq.new(sample_params) }
|
322
312
|
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
313
|
+
before(:each) do
|
314
|
+
create(:user, first_name: 'john', post_id: 1)
|
315
|
+
create(:user, first_name: 'mary', post_id: 2)
|
316
|
+
end
|
327
317
|
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
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
|
333
324
|
end
|
334
|
-
end
|
335
325
|
|
336
|
-
|
337
|
-
|
326
|
+
describe 'it can filter records with condition :not_eq' do
|
327
|
+
let(:datatable) { DatatableCondNotEq.new(sample_params) }
|
338
328
|
|
339
|
-
|
340
|
-
|
341
|
-
|
329
|
+
before(:each) do
|
330
|
+
create(:user, first_name: 'john', post_id: 1)
|
331
|
+
create(:user, first_name: 'mary', post_id: 2)
|
332
|
+
end
|
333
|
+
|
334
|
+
it 'should filter records matching' do
|
335
|
+
datatable.params[:columns]['4'][:search][:value] = 1
|
336
|
+
expect(datatable.data.size).to eq 1
|
337
|
+
item = datatable.data.first
|
338
|
+
expect(item[:first_name]).to eq 'mary'
|
339
|
+
end
|
342
340
|
end
|
343
341
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
expect(item[:last_name]).to eq 'MARY'
|
351
|
-
end
|
342
|
+
describe 'it can filter records with condition :lt' do
|
343
|
+
let(:datatable) { DatatableCondLt.new(sample_params) }
|
344
|
+
|
345
|
+
before(:each) do
|
346
|
+
create(:user, first_name: 'john', post_id: 1)
|
347
|
+
create(:user, first_name: 'mary', post_id: 2)
|
352
348
|
end
|
353
|
-
|
349
|
+
|
354
350
|
it 'should filter records matching' do
|
355
|
-
datatable.params[:columns]['
|
351
|
+
datatable.params[:columns]['4'][:search][:value] = 2
|
356
352
|
expect(datatable.data.size).to eq 1
|
357
353
|
item = datatable.data.first
|
358
|
-
expect(item[:
|
354
|
+
expect(item[:first_name]).to eq 'john'
|
359
355
|
end
|
360
356
|
end
|
361
|
-
end
|
362
357
|
|
363
|
-
|
364
|
-
|
358
|
+
describe 'it can filter records with condition :gt' do
|
359
|
+
let(:datatable) { DatatableCondGt.new(sample_params) }
|
365
360
|
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
361
|
+
before(:each) do
|
362
|
+
create(:user, first_name: 'john', post_id: 1)
|
363
|
+
create(:user, first_name: 'mary', post_id: 2)
|
364
|
+
end
|
370
365
|
|
371
|
-
context 'when condition is NULL' do
|
372
366
|
it 'should filter records matching' do
|
373
|
-
datatable.params[:columns]['
|
367
|
+
datatable.params[:columns]['4'][:search][:value] = 1
|
374
368
|
expect(datatable.data.size).to eq 1
|
375
369
|
item = datatable.data.first
|
376
370
|
expect(item[:first_name]).to eq 'mary'
|
377
371
|
end
|
378
372
|
end
|
379
373
|
|
380
|
-
|
374
|
+
describe 'it can filter records with condition :lteq' do
|
375
|
+
let(:datatable) { DatatableCondLteq.new(sample_params) }
|
376
|
+
|
377
|
+
before(:each) do
|
378
|
+
create(:user, first_name: 'john', post_id: 1)
|
379
|
+
create(:user, first_name: 'mary', post_id: 2)
|
380
|
+
end
|
381
|
+
|
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
|
386
|
+
end
|
387
|
+
|
388
|
+
describe 'it can filter records with condition :gteq' do
|
389
|
+
let(:datatable) { DatatableCondGteq.new(sample_params) }
|
390
|
+
|
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
|
+
|
381
396
|
it 'should filter records matching' do
|
382
|
-
datatable.params[:columns]['
|
397
|
+
datatable.params[:columns]['4'][:search][:value] = 1
|
398
|
+
expect(datatable.data.size).to eq 2
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
describe 'it can filter records with condition :in' do
|
403
|
+
let(:datatable) { DatatableCondIn.new(sample_params) }
|
404
|
+
|
405
|
+
before(:each) do
|
406
|
+
create(:user, first_name: 'john', post_id: 1)
|
407
|
+
create(:user, first_name: 'mary', post_id: 2)
|
408
|
+
end
|
409
|
+
|
410
|
+
it 'should filter records matching' do
|
411
|
+
datatable.params[:columns]['4'][:search][:value] = [1]
|
383
412
|
expect(datatable.data.size).to eq 1
|
384
413
|
item = datatable.data.first
|
385
414
|
expect(item[:first_name]).to eq 'john'
|
386
415
|
end
|
387
416
|
end
|
388
|
-
end
|
389
417
|
|
390
|
-
|
391
|
-
|
418
|
+
describe 'it can filter records with condition :in with regex' do
|
419
|
+
let(:datatable) { DatatableCondInWithRegex.new(sample_params) }
|
392
420
|
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
421
|
+
before(:each) do
|
422
|
+
create(:user, first_name: 'john', post_id: 1)
|
423
|
+
create(:user, first_name: 'mary', post_id: 2)
|
424
|
+
end
|
397
425
|
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
426
|
+
it 'should filter records matching' do
|
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
|
430
|
+
item = datatable.data.first
|
431
|
+
expect(item[:first_name]).to eq 'john'
|
432
|
+
end
|
403
433
|
end
|
404
|
-
end
|
405
434
|
|
406
|
-
|
407
|
-
|
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 }
|
408
439
|
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
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
|
413
450
|
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
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
|
455
|
+
|
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
|
419
460
|
end
|
420
461
|
end
|
421
462
|
|
422
|
-
|
423
|
-
|
463
|
+
context 'proc condition' do
|
464
|
+
describe 'it can filter records with lambda/proc condition' do
|
465
|
+
let(:datatable) { DatatableCondProc.new(sample_params) }
|
424
466
|
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
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
|
429
472
|
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
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
|
435
479
|
end
|
436
480
|
end
|
437
481
|
|
438
|
-
|
439
|
-
|
482
|
+
context 'string condition' do
|
483
|
+
describe 'it can filter records with condition :start_with' do
|
484
|
+
let(:datatable) { DatatableCondStartWith.new(sample_params) }
|
440
485
|
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
486
|
+
before(:each) do
|
487
|
+
create(:user, first_name: 'John')
|
488
|
+
create(:user, first_name: 'Mary')
|
489
|
+
end
|
445
490
|
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
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
|
451
497
|
end
|
452
|
-
end
|
453
498
|
|
454
|
-
|
455
|
-
|
499
|
+
describe 'it can filter records with condition :end_with' do
|
500
|
+
let(:datatable) { DatatableCondEndWith.new(sample_params) }
|
456
501
|
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
502
|
+
before(:each) do
|
503
|
+
create(:user, last_name: 'JOHN')
|
504
|
+
create(:user, last_name: 'MARY')
|
505
|
+
end
|
461
506
|
|
462
|
-
|
463
|
-
|
464
|
-
|
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
|
465
524
|
end
|
466
|
-
end
|
467
525
|
|
468
|
-
|
469
|
-
|
526
|
+
describe 'it can filter records with condition :like' do
|
527
|
+
let(:datatable) { DatatableCondLike.new(sample_params) }
|
470
528
|
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
529
|
+
before(:each) do
|
530
|
+
create(:user, email: 'john@foo.com')
|
531
|
+
create(:user, email: 'mary@bar.com')
|
532
|
+
end
|
475
533
|
|
476
|
-
|
477
|
-
|
478
|
-
|
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
|
479
540
|
end
|
480
|
-
end
|
481
541
|
|
482
|
-
|
483
|
-
|
542
|
+
describe 'it can filter records with condition :string_eq' do
|
543
|
+
let(:datatable) { DatatableCondStringEq.new(sample_params) }
|
484
544
|
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
545
|
+
before(:each) do
|
546
|
+
create(:user, email: 'john@foo.com')
|
547
|
+
create(:user, email: 'mary@bar.com')
|
548
|
+
end
|
489
549
|
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
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
|
495
556
|
end
|
496
|
-
end
|
497
557
|
|
498
|
-
|
499
|
-
|
558
|
+
describe 'it can filter records with condition :string_in' do
|
559
|
+
let(:datatable) { DatatableCondStringIn.new(sample_params) }
|
500
560
|
|
501
|
-
|
502
|
-
|
503
|
-
|
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
|
566
|
+
|
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
|
573
|
+
|
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
|
583
|
+
|
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
|
504
590
|
end
|
505
591
|
|
506
|
-
|
507
|
-
datatable.
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
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
|
512
617
|
end
|
513
618
|
end
|
514
619
|
|
515
|
-
|
516
|
-
let(:datatable) {
|
517
|
-
let(:largest_postgresql_integer_value) { 2147483647 }
|
518
|
-
let(:smallest_postgresql_integer_value) { -2147483648 }
|
620
|
+
context 'unknown condition' do
|
621
|
+
let(:datatable) { DatatableCondUnknown.new(sample_params) }
|
519
622
|
|
520
623
|
before(:each) do
|
521
|
-
|
522
|
-
create(:user, first_name: 'mary', post_id: 2)
|
523
|
-
create(:user, first_name: 'phil', post_id: largest_postgresql_integer_value)
|
624
|
+
datatable.params[:search] = { value: 'john doe', regex: 'false' }
|
524
625
|
end
|
525
626
|
|
526
|
-
it '
|
527
|
-
|
528
|
-
|
627
|
+
it 'should raise error' do
|
628
|
+
expect {
|
629
|
+
datatable.data.size
|
630
|
+
}.to raise_error(AjaxDatatablesRails::Error::InvalidSearchCondition).with_message('foo')
|
529
631
|
end
|
632
|
+
end
|
633
|
+
end
|
530
634
|
|
531
|
-
|
532
|
-
|
533
|
-
expect(datatable.data.size).to eq 0
|
534
|
-
end
|
635
|
+
describe 'formatter option' do
|
636
|
+
let(:datatable) { DatatableWithFormater.new(sample_params) }
|
535
637
|
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
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'
|
540
648
|
end
|
541
649
|
end
|
542
650
|
end
|