ajax-datatables-rails 0.4.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/.travis.yml +6 -41
- data/Appraisals +0 -8
- data/CHANGELOG.md +8 -1
- data/README.md +152 -26
- data/ajax-datatables-rails.gemspec +3 -4
- data/doc/migrate.md +53 -0
- data/lib/ajax-datatables-rails/active_record.rb +7 -0
- data/lib/ajax-datatables-rails/base.rb +7 -23
- data/lib/ajax-datatables-rails/config.rb +3 -6
- data/lib/ajax-datatables-rails/datatable/column.rb +18 -13
- data/lib/ajax-datatables-rails/datatable/column/date_filter.rb +10 -20
- data/lib/ajax-datatables-rails/datatable/column/order.rb +1 -1
- data/lib/ajax-datatables-rails/datatable/column/search.rb +29 -20
- data/lib/ajax-datatables-rails/datatable/datatable.rb +1 -7
- data/lib/ajax-datatables-rails/datatable/simple_order.rb +4 -2
- data/lib/ajax-datatables-rails/datatable/simple_search.rb +2 -0
- data/lib/ajax-datatables-rails/version.rb +1 -1
- data/lib/ajax_datatables_rails.rb +2 -1
- data/lib/generators/datatable/config_generator.rb +4 -4
- data/lib/generators/datatable/templates/ajax_datatables_rails_config.rb +0 -3
- data/lib/generators/rails/templates/datatable.rb +1 -1
- data/spec/ajax-datatables-rails/base_spec.rb +81 -41
- data/spec/ajax-datatables-rails/configuration_spec.rb +0 -9
- data/spec/ajax-datatables-rails/datatable/column_spec.rb +58 -12
- data/spec/ajax-datatables-rails/datatable/datatable_spec.rb +1 -2
- data/spec/ajax-datatables-rails/datatable/simple_order_spec.rb +2 -3
- data/spec/ajax-datatables-rails/extended_spec.rb +1 -2
- data/spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb +84 -137
- data/spec/ajax-datatables-rails/orm/active_record_paginate_records_spec.rb +1 -2
- data/spec/ajax-datatables-rails/orm/active_record_sort_records_spec.rb +4 -5
- data/spec/ajax-datatables-rails/orm/active_record_spec.rb +3 -4
- data/spec/spec_helper.rb +0 -2
- data/spec/support/datatable_cond_numeric.rb +1 -1
- data/spec/support/datatable_cond_string.rb +1 -1
- data/spec/support/test_helpers.rb +1 -1
- metadata +8 -8
- data/gemfiles/rails_4.0.13.gemfile +0 -14
- data/gemfiles/rails_4.1.16.gemfile +0 -14
@@ -9,7 +9,8 @@ module AjaxDatatablesRails
|
|
9
9
|
require 'ajax-datatables-rails/datatable/simple_order'
|
10
10
|
require 'ajax-datatables-rails/datatable/column/search'
|
11
11
|
require 'ajax-datatables-rails/datatable/column/order'
|
12
|
-
require 'ajax-datatables-rails/datatable/column/date_filter'
|
12
|
+
require 'ajax-datatables-rails/datatable/column/date_filter'
|
13
13
|
require 'ajax-datatables-rails/datatable/column'
|
14
14
|
require 'ajax-datatables-rails/orm/active_record'
|
15
|
+
require 'ajax-datatables-rails/active_record'
|
15
16
|
end
|
@@ -6,10 +6,10 @@ module Datatable
|
|
6
6
|
module Generators
|
7
7
|
class ConfigGenerator < ::Rails::Generators::Base
|
8
8
|
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
9
|
-
desc
|
10
|
-
Description:
|
11
|
-
|
12
|
-
DESC
|
9
|
+
desc <<~DESC
|
10
|
+
Description:
|
11
|
+
Creates an initializer file for AjaxDatatablesRails configuration at config/initializers.
|
12
|
+
DESC
|
13
13
|
|
14
14
|
def copy_config_file
|
15
15
|
template 'ajax_datatables_rails_config.rb', 'config/initializers/ajax_datatables_rails.rb'
|
@@ -6,7 +6,4 @@ AjaxDatatablesRails.configure do |config|
|
|
6
6
|
|
7
7
|
# Or you can use your rails environment adapter if you want a generic dev and production
|
8
8
|
# config.db_adapter = Rails.configuration.database_configuration[Rails.env]['adapter'].to_sym
|
9
|
-
|
10
|
-
# available options for orm are: :active_record, :mongoid
|
11
|
-
# config.orm = :active_record
|
12
9
|
end
|
@@ -3,30 +3,26 @@ require 'spec_helper'
|
|
3
3
|
describe AjaxDatatablesRails::Base do
|
4
4
|
|
5
5
|
describe 'an instance' do
|
6
|
-
|
7
|
-
|
8
|
-
it 'requires a view_context' do
|
6
|
+
it 'requires a hash of params' do
|
9
7
|
expect { described_class.new }.to raise_error ArgumentError
|
10
8
|
end
|
11
9
|
|
12
10
|
it 'accepts an options hash' do
|
13
|
-
datatable = described_class.new(
|
11
|
+
datatable = described_class.new(sample_params, foo: 'bar')
|
14
12
|
expect(datatable.options).to eq(foo: 'bar')
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
18
16
|
context 'Public API' do
|
19
|
-
let(:view) { double('view', params: sample_params) }
|
20
|
-
|
21
17
|
describe '#view_columns' do
|
22
18
|
it 'raises an error if not defined by the user' do
|
23
|
-
datatable = described_class.new(
|
19
|
+
datatable = described_class.new(sample_params)
|
24
20
|
expect { datatable.view_columns }.to raise_error NotImplementedError
|
25
21
|
end
|
26
22
|
|
27
23
|
context 'child class implements view_columns' do
|
28
24
|
it 'expects a hash based defining columns' do
|
29
|
-
datatable = ComplexDatatable.new(
|
25
|
+
datatable = ComplexDatatable.new(sample_params)
|
30
26
|
expect(datatable.view_columns).to be_a(Hash)
|
31
27
|
end
|
32
28
|
end
|
@@ -34,19 +30,19 @@ describe AjaxDatatablesRails::Base do
|
|
34
30
|
|
35
31
|
describe '#get_raw_records' do
|
36
32
|
it 'raises an error if not defined by the user' do
|
37
|
-
datatable = described_class.new(
|
33
|
+
datatable = described_class.new(sample_params)
|
38
34
|
expect { datatable.get_raw_records }.to raise_error NotImplementedError
|
39
35
|
end
|
40
36
|
end
|
41
37
|
|
42
38
|
describe '#data' do
|
43
39
|
it 'raises an error if not defined by the user' do
|
44
|
-
datatable = described_class.new(
|
40
|
+
datatable = described_class.new(sample_params)
|
45
41
|
expect { datatable.data }.to raise_error NotImplementedError
|
46
42
|
end
|
47
43
|
|
48
44
|
context 'when data is defined as a hash' do
|
49
|
-
let(:datatable) { ComplexDatatable.new(
|
45
|
+
let(:datatable) { ComplexDatatable.new(sample_params) }
|
50
46
|
|
51
47
|
it 'should return an array of hashes' do
|
52
48
|
create_list(:user, 5)
|
@@ -66,7 +62,7 @@ describe AjaxDatatablesRails::Base do
|
|
66
62
|
end
|
67
63
|
|
68
64
|
context 'when data is defined as a array' do
|
69
|
-
let(:datatable) { ComplexDatatableArray.new(
|
65
|
+
let(:datatable) { ComplexDatatableArray.new(sample_params) }
|
70
66
|
|
71
67
|
it 'should return an array of arrays' do
|
72
68
|
create_list(:user, 5)
|
@@ -87,7 +83,7 @@ describe AjaxDatatablesRails::Base do
|
|
87
83
|
end
|
88
84
|
|
89
85
|
describe '#as_json' do
|
90
|
-
let(:datatable) { ComplexDatatable.new(
|
86
|
+
let(:datatable) { ComplexDatatable.new(sample_params) }
|
91
87
|
|
92
88
|
it 'should return a hash' do
|
93
89
|
create_list(:user, 5)
|
@@ -111,73 +107,117 @@ describe AjaxDatatablesRails::Base do
|
|
111
107
|
end
|
112
108
|
end
|
113
109
|
end
|
114
|
-
end
|
115
110
|
|
111
|
+
describe '#filter_records' do
|
112
|
+
let(:records) { User.all }
|
116
113
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
114
|
+
let(:datatable) do
|
115
|
+
datatable = Class.new(ComplexDatatable) do
|
116
|
+
def filter_records(records)
|
117
|
+
raise NotImplementedError
|
118
|
+
end
|
119
|
+
end
|
120
|
+
datatable.new(sample_params)
|
121
|
+
end
|
121
122
|
|
122
|
-
|
123
|
-
|
123
|
+
it 'should allow method override' do
|
124
|
+
expect { datatable.filter_records(records) }.to raise_error(NotImplementedError)
|
125
|
+
end
|
124
126
|
end
|
125
127
|
|
126
|
-
describe '#
|
127
|
-
|
128
|
-
|
128
|
+
describe '#sort_records' do
|
129
|
+
let(:records) { User.all }
|
130
|
+
|
131
|
+
let(:datatable) do
|
132
|
+
datatable = Class.new(ComplexDatatable) do
|
133
|
+
def sort_records(records)
|
134
|
+
raise NotImplementedError
|
135
|
+
end
|
136
|
+
end
|
137
|
+
datatable.new(sample_params)
|
129
138
|
end
|
130
|
-
end
|
131
139
|
|
132
|
-
|
133
|
-
|
134
|
-
expect { datatable.send(:filter_records) }.to raise_error NoMethodError
|
140
|
+
it 'should allow method override' do
|
141
|
+
expect { datatable.sort_records(records) }.to raise_error(NotImplementedError)
|
135
142
|
end
|
136
143
|
end
|
137
144
|
|
138
|
-
describe '#
|
139
|
-
|
140
|
-
|
145
|
+
describe '#paginate_records' do
|
146
|
+
let(:records) { User.all }
|
147
|
+
|
148
|
+
let(:datatable) do
|
149
|
+
datatable = Class.new(ComplexDatatable) do
|
150
|
+
def paginate_records(records)
|
151
|
+
raise NotImplementedError
|
152
|
+
end
|
153
|
+
end
|
154
|
+
datatable.new(sample_params)
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'should allow method override' do
|
158
|
+
expect { datatable.paginate_records(records) }.to raise_error(NotImplementedError)
|
141
159
|
end
|
142
160
|
end
|
161
|
+
end
|
143
162
|
|
144
|
-
|
145
|
-
|
146
|
-
|
163
|
+
|
164
|
+
context 'Private API' do
|
165
|
+
context 'when orm is not implemented' do
|
166
|
+
let(:datatable) { AjaxDatatablesRails::Base.new(sample_params) }
|
167
|
+
|
168
|
+
describe '#fetch_records' do
|
169
|
+
it 'raises an error if it does not include an ORM module' do
|
170
|
+
expect { datatable.fetch_records }.to raise_error NoMethodError
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
describe '#filter_records' do
|
175
|
+
it 'raises an error if it does not include an ORM module' do
|
176
|
+
expect { datatable.filter_records }.to raise_error NoMethodError
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
describe '#sort_records' do
|
181
|
+
it 'raises an error if it does not include an ORM module' do
|
182
|
+
expect { datatable.sort_records }.to raise_error NoMethodError
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe '#paginate_records' do
|
187
|
+
it 'raises an error if it does not include an ORM module' do
|
188
|
+
expect { datatable.paginate_records }.to raise_error NoMethodError
|
189
|
+
end
|
147
190
|
end
|
148
191
|
end
|
149
192
|
|
150
193
|
describe 'helper methods' do
|
151
194
|
describe '#offset' do
|
152
195
|
it 'defaults to 0' do
|
153
|
-
|
154
|
-
datatable = described_class.new(default_view)
|
196
|
+
datatable = described_class.new({})
|
155
197
|
expect(datatable.datatable.send(:offset)).to eq(0)
|
156
198
|
end
|
157
199
|
|
158
200
|
it 'matches the value on view params[:start]' do
|
159
|
-
|
160
|
-
datatable = described_class.new(paginated_view)
|
201
|
+
datatable = described_class.new({ start: '11' })
|
161
202
|
expect(datatable.datatable.send(:offset)).to eq(11)
|
162
203
|
end
|
163
204
|
end
|
164
205
|
|
165
206
|
describe '#page' do
|
166
207
|
it 'calculates page number from params[:start] and #per_page' do
|
167
|
-
|
168
|
-
datatable = described_class.new(paginated_view)
|
208
|
+
datatable = described_class.new({ start: '11' })
|
169
209
|
expect(datatable.datatable.send(:page)).to eq(2)
|
170
210
|
end
|
171
211
|
end
|
172
212
|
|
173
213
|
describe '#per_page' do
|
174
214
|
it 'defaults to 10' do
|
175
|
-
datatable = described_class.new(
|
215
|
+
datatable = described_class.new(sample_params)
|
176
216
|
expect(datatable.datatable.send(:per_page)).to eq(10)
|
177
217
|
end
|
178
218
|
|
179
219
|
it 'matches the value on view params[:length]' do
|
180
|
-
other_view =
|
220
|
+
other_view = { length: 20 }
|
181
221
|
datatable = described_class.new(other_view)
|
182
222
|
expect(datatable.datatable.send(:per_page)).to eq(20)
|
183
223
|
end
|
@@ -20,10 +20,6 @@ describe AjaxDatatablesRails::Configuration do
|
|
20
20
|
let(:config) { AjaxDatatablesRails::Configuration.new }
|
21
21
|
|
22
22
|
describe 'default config' do
|
23
|
-
it 'default orm should :active_record' do
|
24
|
-
expect(config.orm).to eq(:active_record)
|
25
|
-
end
|
26
|
-
|
27
23
|
it 'default db_adapter should :postgresql' do
|
28
24
|
expect(config.db_adapter).to eq(:postgresql)
|
29
25
|
end
|
@@ -34,10 +30,5 @@ describe AjaxDatatablesRails::Configuration do
|
|
34
30
|
config.db_adapter = :mysql
|
35
31
|
expect(config.db_adapter).to eq(:mysql)
|
36
32
|
end
|
37
|
-
|
38
|
-
it 'accepts a custom orm value' do
|
39
|
-
config.orm = :mongoid
|
40
|
-
expect(config.orm).to eq(:mongoid)
|
41
|
-
end
|
42
33
|
end
|
43
34
|
end
|
@@ -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,81 @@ 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
|
+
allow_any_instance_of(AjaxDatatablesRails::Configuration).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
|
+
allow_any_instance_of(AjaxDatatablesRails::Configuration).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
|
+
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :postgresql }
|
167
|
+
expect(column.send(:type_cast)).to eq('VARCHAR')
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'returns VARCHAR if :db_adapter is :oracle' do
|
171
|
+
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :oracle }
|
172
|
+
expect(column.send(:type_cast)).to eq('VARCHAR2(4000)')
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'returns VARCHAR if :db_adapter is :oracleenhanced' do
|
176
|
+
allow_any_instance_of(AjaxDatatablesRails::Configuration).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
|
+
allow_any_instance_of(AjaxDatatablesRails::Configuration).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
|
+
allow_any_instance_of(AjaxDatatablesRails::Configuration).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
|
+
allow_any_instance_of(AjaxDatatablesRails::Configuration).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
|
+
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :sqlite3 }
|
197
|
+
expect(column.send(:type_cast)).to eq('TEXT')
|
198
|
+
end
|
199
|
+
end
|
154
200
|
end
|
@@ -2,8 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe AjaxDatatablesRails::Datatable::Datatable do
|
4
4
|
|
5
|
-
let(:
|
6
|
-
let(:datatable) { ComplexDatatable.new(view).datatable }
|
5
|
+
let(:datatable) { ComplexDatatable.new(sample_params).datatable }
|
7
6
|
let(:order_option) { {'0'=>{'column'=>'0', 'dir'=>'asc'}, '1'=>{'column'=>'1', 'dir'=>'desc'}} }
|
8
7
|
|
9
8
|
describe 'order methods' do
|
@@ -2,8 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe AjaxDatatablesRails::Datatable::SimpleOrder do
|
4
4
|
|
5
|
-
let(:
|
6
|
-
let(:datatable) { ComplexDatatable.new(view).datatable }
|
5
|
+
let(:datatable) { ComplexDatatable.new(sample_params).datatable }
|
7
6
|
let(:options) { ActiveSupport::HashWithIndifferentAccess.new({'column' => '1', 'dir' => 'desc'}) }
|
8
7
|
let(:simple_order) { AjaxDatatablesRails::Datatable::SimpleOrder.new(datatable, options) }
|
9
8
|
|
@@ -26,7 +25,7 @@ describe AjaxDatatablesRails::Datatable::SimpleOrder do
|
|
26
25
|
end
|
27
26
|
|
28
27
|
describe 'using column option' do
|
29
|
-
let(:sorted_datatable) { DatatableOrderNullsLast.new(
|
28
|
+
let(:sorted_datatable) { DatatableOrderNullsLast.new(sample_params).datatable }
|
30
29
|
let(:nulls_last_order) { AjaxDatatablesRails::Datatable::SimpleOrder.new(sorted_datatable, options) }
|
31
30
|
|
32
31
|
it 'sql query' do
|
@@ -2,8 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe AjaxDatatablesRails::Base do
|
4
4
|
describe 'it can transform search value before asking the database' do
|
5
|
-
let(:
|
6
|
-
let(:datatable) { DatatableWithFormater.new(view) }
|
5
|
+
let(:datatable) { DatatableWithFormater.new(sample_params) }
|
7
6
|
|
8
7
|
before(:each) do
|
9
8
|
create(:user, username: 'johndoe', email: 'johndoe@example.com', last_name: 'DOE')
|
@@ -2,25 +2,24 @@ 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)
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
@@ -32,14 +31,14 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
32
31
|
|
33
32
|
it 'returns an Arel object' do
|
34
33
|
datatable.params[:search] = { value: 'msmith' }
|
35
|
-
result = datatable.
|
34
|
+
result = datatable.build_conditions_for_datatable
|
36
35
|
expect(result).to be_a(Arel::Nodes::Grouping)
|
37
36
|
end
|
38
37
|
|
39
38
|
context 'no search query' do
|
40
39
|
it 'returns empty query' do
|
41
40
|
datatable.params[:search] = { value: '' }
|
42
|
-
expect(datatable.
|
41
|
+
expect(datatable.build_conditions_for_datatable).to be_blank
|
43
42
|
end
|
44
43
|
end
|
45
44
|
|
@@ -54,11 +53,11 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
54
53
|
end
|
55
54
|
|
56
55
|
it 'returns empty query result' do
|
57
|
-
expect(datatable.
|
56
|
+
expect(datatable.build_conditions_for_datatable).to be_blank
|
58
57
|
end
|
59
58
|
|
60
59
|
it 'returns filtered results' do
|
61
|
-
query = datatable.
|
60
|
+
query = datatable.build_conditions_for_datatable
|
62
61
|
results = records.where(query).map(&:username)
|
63
62
|
expect(results).to eq ['johndoe', 'msmith']
|
64
63
|
end
|
@@ -70,11 +69,11 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
70
69
|
end
|
71
70
|
|
72
71
|
it 'returns empty query result' do
|
73
|
-
expect(datatable.
|
72
|
+
expect(datatable.build_conditions_for_datatable).to be_blank
|
74
73
|
end
|
75
74
|
|
76
75
|
it 'returns filtered results' do
|
77
|
-
query = datatable.
|
76
|
+
query = datatable.build_conditions_for_datatable
|
78
77
|
results = records.where(query).map(&:username)
|
79
78
|
expect(results).to eq ['johndoe', 'msmith']
|
80
79
|
end
|
@@ -88,7 +87,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
88
87
|
end
|
89
88
|
|
90
89
|
it 'returns a filtering query' do
|
91
|
-
query = datatable.
|
90
|
+
query = datatable.build_conditions_for_datatable
|
92
91
|
results = records.where(query).map(&:username)
|
93
92
|
expect(results).to include('johndoe')
|
94
93
|
expect(results).not_to include('msmith')
|
@@ -101,7 +100,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
101
100
|
end
|
102
101
|
|
103
102
|
it 'returns a filtering query' do
|
104
|
-
query = datatable.
|
103
|
+
query = datatable.build_conditions_for_datatable
|
105
104
|
results = records.where(query).map(&:username)
|
106
105
|
expect(results).to eq ['johndoe']
|
107
106
|
expect(results).not_to include('msmith')
|
@@ -123,14 +122,14 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
123
122
|
end
|
124
123
|
|
125
124
|
it 'returns an Arel object' do
|
126
|
-
result = datatable.
|
125
|
+
result = datatable.build_conditions_for_selected_columns
|
127
126
|
expect(result).to be_a(Arel::Nodes::And)
|
128
127
|
end
|
129
128
|
|
130
129
|
if AjaxDatatablesRails.config.db_adapter == :postgresql
|
131
130
|
context 'when db_adapter is postgresql' do
|
132
131
|
it 'can call #to_sql on returned object' do
|
133
|
-
result = datatable.
|
132
|
+
result = datatable.build_conditions_for_selected_columns
|
134
133
|
expect(result).to respond_to(:to_sql)
|
135
134
|
expect(result.to_sql).to eq(
|
136
135
|
"CAST(\"users\".\"username\" AS VARCHAR) ILIKE '%doe%' AND CAST(\"users\".\"email\" AS VARCHAR) ILIKE '%example%'"
|
@@ -142,7 +141,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
142
141
|
if AjaxDatatablesRails.config.db_adapter.in? %i[oracle oracleenhanced]
|
143
142
|
context 'when db_adapter is oracle' do
|
144
143
|
it 'can call #to_sql on returned object' do
|
145
|
-
result = datatable.
|
144
|
+
result = datatable.build_conditions_for_selected_columns
|
146
145
|
expect(result).to respond_to(:to_sql)
|
147
146
|
expect(result.to_sql).to eq(
|
148
147
|
"CAST(\"USERS\".\"USERNAME\" AS VARCHAR2(4000)) LIKE '%doe%' AND CAST(\"USERS\".\"EMAIL\" AS VARCHAR2(4000)) LIKE '%example%'"
|
@@ -154,7 +153,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
154
153
|
if AjaxDatatablesRails.config.db_adapter.in? %i[mysql2 sqlite3]
|
155
154
|
context 'when db_adapter is mysql2' do
|
156
155
|
it 'can call #to_sql on returned object' do
|
157
|
-
result = datatable.
|
156
|
+
result = datatable.build_conditions_for_selected_columns
|
158
157
|
expect(result).to respond_to(:to_sql)
|
159
158
|
expect(result.to_sql).to eq(
|
160
159
|
"CAST(`users`.`username` AS CHAR) LIKE '%doe%' AND CAST(`users`.`email` AS CHAR) LIKE '%example%'"
|
@@ -166,7 +165,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
166
165
|
|
167
166
|
it 'calls #build_conditions_for_selected_columns' do
|
168
167
|
expect(datatable).to receive(:build_conditions_for_selected_columns)
|
169
|
-
datatable.
|
168
|
+
datatable.build_conditions
|
170
169
|
end
|
171
170
|
|
172
171
|
context 'with search values in columns' do
|
@@ -175,7 +174,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
175
174
|
end
|
176
175
|
|
177
176
|
it 'returns a filtered set of records' do
|
178
|
-
query = datatable.
|
177
|
+
query = datatable.build_conditions_for_selected_columns
|
179
178
|
results = records.where(query).map(&:username)
|
180
179
|
expect(results).to include('johndoe')
|
181
180
|
expect(results).not_to include('msmith')
|
@@ -183,142 +182,90 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
183
182
|
end
|
184
183
|
end
|
185
184
|
|
186
|
-
describe '
|
187
|
-
|
188
|
-
|
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
|
185
|
+
describe 'filter conditions' do
|
186
|
+
describe 'it can filter records with condition :date_range' do
|
187
|
+
let(:datatable) { DatatableCondDate.new(sample_params) }
|
219
188
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
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
|
224
193
|
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
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
|
229
202
|
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
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
|
235
209
|
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
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
|
240
216
|
|
241
|
-
|
242
|
-
|
243
|
-
|
217
|
+
context 'when both date are filled' do
|
218
|
+
it 'should filter records created between the range' do
|
219
|
+
datatable.params[:columns]['5'][:search][:value] = '01/12/1999-15/01/2000'
|
220
|
+
expect(datatable.data.size).to eq 1
|
244
221
|
end
|
222
|
+
end
|
245
223
|
|
224
|
+
context 'when another filter is active' do
|
246
225
|
context 'when range is empty' do
|
247
|
-
it 'should
|
226
|
+
it 'should filter records' do
|
227
|
+
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
248
228
|
datatable.params[:columns]['5'][:search][:value] = '-'
|
249
|
-
expect(datatable.data.size).to eq
|
229
|
+
expect(datatable.data.size).to eq 1
|
250
230
|
item = datatable.data.first
|
251
231
|
expect(item[:last_name]).to eq 'Doe'
|
252
232
|
end
|
253
233
|
end
|
254
234
|
|
255
235
|
context 'when start date is filled' do
|
256
|
-
it 'should filter records
|
257
|
-
datatable.params[:columns]['
|
258
|
-
|
236
|
+
it 'should filter records' do
|
237
|
+
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
238
|
+
datatable.params[:columns]['5'][:search][:value] = '01/12/1999-'
|
239
|
+
expect(datatable.data.size).to eq 1
|
240
|
+
item = datatable.data.first
|
241
|
+
expect(item[:last_name]).to eq 'Doe'
|
259
242
|
end
|
260
243
|
end
|
261
244
|
|
262
245
|
context 'when end date is filled' do
|
263
|
-
it 'should filter records
|
264
|
-
datatable.params[:columns]['
|
265
|
-
|
246
|
+
it 'should filter records' do
|
247
|
+
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
248
|
+
datatable.params[:columns]['5'][:search][:value] = '-15/01/2000'
|
249
|
+
expect(datatable.data.size).to eq 1
|
250
|
+
item = datatable.data.first
|
251
|
+
expect(item[:last_name]).to eq 'Doe'
|
266
252
|
end
|
267
253
|
end
|
268
254
|
|
269
255
|
context 'when both date are filled' do
|
270
|
-
it 'should filter records
|
256
|
+
it 'should filter records' do
|
257
|
+
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
271
258
|
datatable.params[:columns]['5'][:search][:value] = '01/12/1999-15/01/2000'
|
272
259
|
expect(datatable.data.size).to eq 1
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
context 'when another filter is active' do
|
277
|
-
context 'when range is empty' do
|
278
|
-
it 'should filter records' do
|
279
|
-
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
280
|
-
datatable.params[:columns]['5'][:search][:value] = '-'
|
281
|
-
expect(datatable.data.size).to eq 1
|
282
|
-
item = datatable.data.first
|
283
|
-
expect(item[:last_name]).to eq 'Doe'
|
284
|
-
end
|
285
|
-
end
|
286
|
-
|
287
|
-
context 'when start date is filled' do
|
288
|
-
it 'should filter records' do
|
289
|
-
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
290
|
-
datatable.params[:columns]['5'][:search][:value] = '01/12/1999-'
|
291
|
-
expect(datatable.data.size).to eq 1
|
292
|
-
item = datatable.data.first
|
293
|
-
expect(item[:last_name]).to eq 'Doe'
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
context 'when end date is filled' do
|
298
|
-
it 'should filter records' do
|
299
|
-
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
300
|
-
datatable.params[:columns]['5'][:search][:value] = '-15/01/2000'
|
301
|
-
expect(datatable.data.size).to eq 1
|
302
|
-
item = datatable.data.first
|
303
|
-
expect(item[:last_name]).to eq 'Doe'
|
304
|
-
end
|
305
|
-
end
|
306
|
-
|
307
|
-
context 'when both date are filled' do
|
308
|
-
it 'should filter records' do
|
309
|
-
datatable.params[:columns]['0'][:search][:value] = 'doe'
|
310
|
-
datatable.params[:columns]['5'][:search][:value] = '01/12/1999-15/01/2000'
|
311
|
-
expect(datatable.data.size).to eq 1
|
312
|
-
item = datatable.data.first
|
313
|
-
expect(item[:last_name]).to eq 'Doe'
|
314
|
-
end
|
260
|
+
item = datatable.data.first
|
261
|
+
expect(item[:last_name]).to eq 'Doe'
|
315
262
|
end
|
316
263
|
end
|
317
264
|
end
|
318
265
|
end
|
319
266
|
|
320
267
|
describe 'it can filter records with condition :start_with' do
|
321
|
-
let(:datatable) { DatatableCondStartWith.new(
|
268
|
+
let(:datatable) { DatatableCondStartWith.new(sample_params) }
|
322
269
|
|
323
270
|
before(:each) do
|
324
271
|
create(:user, first_name: 'John')
|
@@ -334,7 +281,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
334
281
|
end
|
335
282
|
|
336
283
|
describe 'it can filter records with condition :end_with' do
|
337
|
-
let(:datatable) { DatatableCondEndWith.new(
|
284
|
+
let(:datatable) { DatatableCondEndWith.new(sample_params) }
|
338
285
|
|
339
286
|
before(:each) do
|
340
287
|
create(:user, last_name: 'JOHN')
|
@@ -361,7 +308,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
361
308
|
end
|
362
309
|
|
363
310
|
describe 'it can filter records with condition :like' do
|
364
|
-
let(:datatable) { DatatableCondLike.new(
|
311
|
+
let(:datatable) { DatatableCondLike.new(sample_params) }
|
365
312
|
|
366
313
|
before(:each) do
|
367
314
|
create(:user, email: 'john@foo.com')
|
@@ -377,7 +324,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
377
324
|
end
|
378
325
|
|
379
326
|
describe 'it can filter records with condition :string_eq' do
|
380
|
-
let(:datatable) { DatatableCondStringEq.new(
|
327
|
+
let(:datatable) { DatatableCondStringEq.new(sample_params) }
|
381
328
|
|
382
329
|
before(:each) do
|
383
330
|
create(:user, email: 'john@foo.com')
|
@@ -393,7 +340,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
393
340
|
end
|
394
341
|
|
395
342
|
describe 'it can filter records with condition :null_value' do
|
396
|
-
let(:datatable) { DatatableCondNullValue.new(
|
343
|
+
let(:datatable) { DatatableCondNullValue.new(sample_params) }
|
397
344
|
|
398
345
|
before(:each) do
|
399
346
|
create(:user, first_name: 'john', email: 'foo@bar.com')
|
@@ -420,7 +367,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
420
367
|
end
|
421
368
|
|
422
369
|
describe 'it can filter records with condition :eq' do
|
423
|
-
let(:datatable) { DatatableCondEq.new(
|
370
|
+
let(:datatable) { DatatableCondEq.new(sample_params) }
|
424
371
|
|
425
372
|
before(:each) do
|
426
373
|
create(:user, first_name: 'john', post_id: 1)
|
@@ -436,7 +383,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
436
383
|
end
|
437
384
|
|
438
385
|
describe 'it can filter records with condition :not_eq' do
|
439
|
-
let(:datatable) { DatatableCondNotEq.new(
|
386
|
+
let(:datatable) { DatatableCondNotEq.new(sample_params) }
|
440
387
|
|
441
388
|
before(:each) do
|
442
389
|
create(:user, first_name: 'john', post_id: 1)
|
@@ -452,7 +399,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
452
399
|
end
|
453
400
|
|
454
401
|
describe 'it can filter records with condition :lt' do
|
455
|
-
let(:datatable) { DatatableCondLt.new(
|
402
|
+
let(:datatable) { DatatableCondLt.new(sample_params) }
|
456
403
|
|
457
404
|
before(:each) do
|
458
405
|
create(:user, first_name: 'john', post_id: 1)
|
@@ -468,7 +415,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
468
415
|
end
|
469
416
|
|
470
417
|
describe 'it can filter records with condition :gt' do
|
471
|
-
let(:datatable) { DatatableCondGt.new(
|
418
|
+
let(:datatable) { DatatableCondGt.new(sample_params) }
|
472
419
|
|
473
420
|
before(:each) do
|
474
421
|
create(:user, first_name: 'john', post_id: 1)
|
@@ -484,7 +431,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
484
431
|
end
|
485
432
|
|
486
433
|
describe 'it can filter records with condition :lteq' do
|
487
|
-
let(:datatable) { DatatableCondLteq.new(
|
434
|
+
let(:datatable) { DatatableCondLteq.new(sample_params) }
|
488
435
|
|
489
436
|
before(:each) do
|
490
437
|
create(:user, first_name: 'john', post_id: 1)
|
@@ -498,7 +445,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
498
445
|
end
|
499
446
|
|
500
447
|
describe 'it can filter records with condition :gteq' do
|
501
|
-
let(:datatable) { DatatableCondGteq.new(
|
448
|
+
let(:datatable) { DatatableCondGteq.new(sample_params) }
|
502
449
|
|
503
450
|
before(:each) do
|
504
451
|
create(:user, first_name: 'john', post_id: 1)
|
@@ -512,7 +459,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
512
459
|
end
|
513
460
|
|
514
461
|
describe 'it can filter records with condition :in' do
|
515
|
-
let(:datatable) { DatatableCondIn.new(
|
462
|
+
let(:datatable) { DatatableCondIn.new(sample_params) }
|
516
463
|
|
517
464
|
before(:each) do
|
518
465
|
create(:user, first_name: 'john', post_id: 1)
|
@@ -528,7 +475,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
528
475
|
end
|
529
476
|
|
530
477
|
describe 'it can filter records with condition :in with regex' do
|
531
|
-
let(:datatable) { DatatableCondInWithRegex.new(
|
478
|
+
let(:datatable) { DatatableCondInWithRegex.new(sample_params) }
|
532
479
|
|
533
480
|
before(:each) do
|
534
481
|
create(:user, first_name: 'john', post_id: 1)
|
@@ -545,7 +492,7 @@ describe AjaxDatatablesRails::ORM::ActiveRecord do
|
|
545
492
|
end
|
546
493
|
|
547
494
|
describe 'Integer overflows' do
|
548
|
-
let(:datatable) { DatatableCondEq.new(
|
495
|
+
let(:datatable) { DatatableCondEq.new(sample_params) }
|
549
496
|
let(:largest_postgresql_integer_value) { 2147483647 }
|
550
497
|
let(:smallest_postgresql_integer_value) { -2147483648 }
|
551
498
|
|