ajax-datatables-rails 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +26 -0
- data/.gitignore +20 -0
- data/.rspec +1 -0
- data/.rubocop.yml +1157 -0
- data/.travis.yml +68 -0
- data/Appraisals +34 -0
- data/Gemfile +5 -1
- data/LICENSE +17 -18
- data/README.md +239 -239
- data/Rakefile +1 -1
- data/ajax-datatables-rails.gemspec +31 -24
- data/gemfiles/rails_4.0.13.gemfile +14 -0
- data/gemfiles/rails_4.1.15.gemfile +14 -0
- data/gemfiles/rails_4.2.8.gemfile +13 -0
- data/gemfiles/rails_5.0.3.gemfile +13 -0
- data/gemfiles/rails_5.1.1.gemfile +13 -0
- data/lib/ajax-datatables-rails.rb +9 -8
- data/lib/ajax-datatables-rails/base.rb +80 -156
- data/lib/ajax-datatables-rails/config.rb +8 -5
- data/lib/ajax-datatables-rails/datatable/column.rb +169 -0
- data/lib/ajax-datatables-rails/datatable/column_date_filter.rb +41 -0
- data/lib/ajax-datatables-rails/datatable/datatable.rb +79 -0
- data/lib/ajax-datatables-rails/datatable/simple_order.rb +31 -0
- data/lib/ajax-datatables-rails/datatable/simple_search.rb +18 -0
- data/lib/ajax-datatables-rails/orm/active_record.rb +52 -0
- data/lib/ajax-datatables-rails/version.rb +1 -1
- data/lib/generators/datatable/templates/ajax_datatables_rails_config.rb +3 -3
- data/lib/generators/rails/datatable_generator.rb +7 -19
- data/lib/generators/rails/templates/datatable.rb +26 -14
- data/spec/ajax-datatables-rails/base_spec.rb +190 -0
- data/spec/ajax-datatables-rails/configuration_spec.rb +43 -0
- data/spec/ajax-datatables-rails/datatable/column_spec.rb +109 -0
- data/spec/ajax-datatables-rails/datatable/datatable_spec.rb +87 -0
- data/spec/ajax-datatables-rails/datatable/simple_order_spec.rb +13 -0
- data/spec/ajax-datatables-rails/datatable/simple_search_spec.rb +17 -0
- data/spec/ajax-datatables-rails/extended_spec.rb +20 -0
- data/spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb +439 -0
- data/spec/ajax-datatables-rails/orm/active_record_paginate_records_spec.rb +66 -0
- data/spec/ajax-datatables-rails/orm/active_record_sort_records_spec.rb +34 -0
- data/spec/ajax-datatables-rails/orm/active_record_spec.rb +25 -0
- data/spec/factories/user.rb +9 -0
- data/spec/install_oracle.sh +12 -0
- data/spec/spec_helper.rb +75 -3
- data/spec/support/schema.rb +14 -0
- data/spec/support/test_helpers.rb +174 -0
- data/spec/support/test_models.rb +2 -0
- metadata +169 -37
- data/lib/ajax-datatables-rails/extensions/kaminari.rb +0 -12
- data/lib/ajax-datatables-rails/extensions/simple_paginator.rb +0 -12
- data/lib/ajax-datatables-rails/extensions/will_paginate.rb +0 -12
- data/lib/ajax-datatables-rails/models.rb +0 -6
- data/spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb +0 -351
- data/spec/ajax-datatables-rails/kaminari_spec.rb +0 -35
- data/spec/ajax-datatables-rails/models_spec.rb +0 -10
- data/spec/ajax-datatables-rails/simple_paginator_spec.rb +0 -32
- data/spec/ajax-datatables-rails/will_paginate_spec.rb +0 -28
- data/spec/schema.rb +0 -35
- data/spec/test_models.rb +0 -21
@@ -1,351 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe AjaxDatatablesRails::Base do
|
4
|
-
|
5
|
-
params = {
|
6
|
-
:draw => '5',
|
7
|
-
:columns => {
|
8
|
-
"0" => {
|
9
|
-
:data => '0',
|
10
|
-
:name => '',
|
11
|
-
:searchable => true,
|
12
|
-
:orderable => true,
|
13
|
-
:search => { :value => '', :regex => false }
|
14
|
-
},
|
15
|
-
"1" => {
|
16
|
-
:data => '1',
|
17
|
-
:name => '',
|
18
|
-
:searchable => true,
|
19
|
-
:orderable => true,
|
20
|
-
:search => { :value => '', :regex => false }
|
21
|
-
}
|
22
|
-
},
|
23
|
-
:order => { "0" => { :column => '1', :dir => 'desc' } },
|
24
|
-
:start => '0',
|
25
|
-
:length => '10',
|
26
|
-
:search => { :value => '', :regex => false },
|
27
|
-
'_' => '1403141483098'
|
28
|
-
}
|
29
|
-
let(:view) { double('view', :params => params) }
|
30
|
-
|
31
|
-
describe 'an instance' do
|
32
|
-
it 'requires a view_context' do
|
33
|
-
expect { AjaxDatatablesRails::Base.new }.to raise_error
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'accepts an options hash' do
|
37
|
-
datatable = AjaxDatatablesRails::Base.new(view, :foo => 'bar')
|
38
|
-
expect(datatable.options).to eq(:foo => 'bar')
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe 'helper methods' do
|
43
|
-
describe '#offset' do
|
44
|
-
it 'defaults to 0' do
|
45
|
-
default_view = double('view', :params => {})
|
46
|
-
datatable = AjaxDatatablesRails::Base.new(default_view)
|
47
|
-
expect(datatable.send(:offset)).to eq(0)
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'matches the value on view params[:start] minus 1' do
|
51
|
-
paginated_view = double('view', :params => { :start => '11' })
|
52
|
-
datatable = AjaxDatatablesRails::Base.new(paginated_view)
|
53
|
-
expect(datatable.send(:offset)).to eq(10)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
describe '#page' do
|
58
|
-
it 'calculates page number from params[:start] and #per_page' do
|
59
|
-
paginated_view = double('view', :params => { :start => '11' })
|
60
|
-
datatable = AjaxDatatablesRails::Base.new(paginated_view)
|
61
|
-
expect(datatable.send(:page)).to eq(2)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
describe '#per_page' do
|
66
|
-
it 'defaults to 10' do
|
67
|
-
datatable = AjaxDatatablesRails::Base.new(view)
|
68
|
-
expect(datatable.send(:per_page)).to eq(10)
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'matches the value on view params[:length]' do
|
72
|
-
other_view = double('view', :params => { :length => 20 })
|
73
|
-
datatable = AjaxDatatablesRails::Base.new(other_view)
|
74
|
-
expect(datatable.send(:per_page)).to eq(20)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
describe '#sort_column' do
|
79
|
-
it 'returns a column name from the #sorting_columns array' do
|
80
|
-
sort_view = double(
|
81
|
-
'view', :params => params
|
82
|
-
)
|
83
|
-
datatable = AjaxDatatablesRails::Base.new(sort_view)
|
84
|
-
allow(datatable).to receive(:sortable_displayed_columns) { ["0", "1"] }
|
85
|
-
allow(datatable).to receive(:sortable_columns) { ['User.foo', 'User.bar', 'User.baz'] }
|
86
|
-
|
87
|
-
expect(datatable.send(:sort_column, sort_view.params[:order]["0"])).to eq('users.bar')
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
describe '#sort_direction' do
|
92
|
-
it 'matches value of params[:sSortDir_0]' do
|
93
|
-
sorting_view = double(
|
94
|
-
'view',
|
95
|
-
:params => {
|
96
|
-
:order => {
|
97
|
-
'0' => { :column => '1', :dir => 'desc' }
|
98
|
-
}
|
99
|
-
}
|
100
|
-
)
|
101
|
-
datatable = AjaxDatatablesRails::Base.new(sorting_view)
|
102
|
-
expect(datatable.send(:sort_direction, sorting_view.params[:order]["0"])).to eq('DESC')
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'can only be one option from ASC or DESC' do
|
106
|
-
sorting_view = double(
|
107
|
-
'view',
|
108
|
-
:params => {
|
109
|
-
:order => {
|
110
|
-
'0' => { :column => '1', :dir => 'foo' }
|
111
|
-
}
|
112
|
-
}
|
113
|
-
)
|
114
|
-
datatable = AjaxDatatablesRails::Base.new(sorting_view)
|
115
|
-
expect(datatable.send(:sort_direction, sorting_view.params[:order]["0"])).to eq('ASC')
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
describe "#configure" do
|
120
|
-
let(:datatable) do
|
121
|
-
class FooDatatable < AjaxDatatablesRails::Base
|
122
|
-
end
|
123
|
-
|
124
|
-
FooDatatable.new view
|
125
|
-
end
|
126
|
-
|
127
|
-
context "when model class name is regular" do
|
128
|
-
it "should successfully get right model class" do
|
129
|
-
expect(
|
130
|
-
datatable.send(:search_condition, 'User.bar', 'bar')
|
131
|
-
).to be_a(Arel::Nodes::Matches)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
context "when custom named model class" do
|
136
|
-
it "should successfully get right model class" do
|
137
|
-
expect(
|
138
|
-
datatable.send(:search_condition, 'Statistics::Request.bar', 'bar')
|
139
|
-
).to be_a(Arel::Nodes::Matches)
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
|
144
|
-
context "when model class name camelcased" do
|
145
|
-
it "should successfully get right model class" do
|
146
|
-
expect(
|
147
|
-
datatable.send(:search_condition, 'PurchasedOrder.bar', 'bar')
|
148
|
-
).to be_a(Arel::Nodes::Matches)
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
context "when model class name is namespaced" do
|
153
|
-
it "should successfully get right model class" do
|
154
|
-
expect(
|
155
|
-
datatable.send(:search_condition, 'Statistics::Session.bar', 'bar')
|
156
|
-
).to be_a(Arel::Nodes::Matches)
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
context "when model class defined but not found" do
|
161
|
-
it "raise 'uninitialized constant'" do
|
162
|
-
expect {
|
163
|
-
datatable.send(:search_condition, 'UnexistentModel.bar', 'bar')
|
164
|
-
}.to raise_error(NameError, /uninitialized constant/)
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
context 'when using deprecated notation' do
|
169
|
-
it 'should successfully get right model class if exists' do
|
170
|
-
expect(
|
171
|
-
datatable.send(:search_condition, 'users.bar', 'bar')
|
172
|
-
).to be_a(Arel::Nodes::Matches)
|
173
|
-
end
|
174
|
-
|
175
|
-
it 'should display a deprecated message' do
|
176
|
-
expect(AjaxDatatablesRails::Base).to receive(:deprecated)
|
177
|
-
datatable.send(:search_condition, 'users.bar', 'bar')
|
178
|
-
end
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
describe '#sortable_columns' do
|
183
|
-
it 'returns an array representing database columns' do
|
184
|
-
datatable = AjaxDatatablesRails::Base.new(view)
|
185
|
-
expect(datatable.sortable_columns).to eq([])
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
describe '#searchable_columns' do
|
190
|
-
it 'returns an array representing database columns' do
|
191
|
-
datatable = AjaxDatatablesRails::Base.new(view)
|
192
|
-
expect(datatable.searchable_columns).to eq([])
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
describe 'perform' do
|
198
|
-
let(:results) { double('Collection', :offset => [], :limit => []) }
|
199
|
-
let(:view) { double('view', :params => params) }
|
200
|
-
let(:datatable) { AjaxDatatablesRails::Base.new(view) }
|
201
|
-
let(:records) { double('Array').as_null_object }
|
202
|
-
|
203
|
-
before(:each) do
|
204
|
-
allow(datatable).to receive(:sortable_columns) { ['User.foo', 'User.bar'] }
|
205
|
-
allow(datatable).to receive(:sortable_displayed_columns) { ["0", "1"] }
|
206
|
-
end
|
207
|
-
|
208
|
-
describe '#paginate_records' do
|
209
|
-
it 'defaults to Extensions::SimplePaginator#paginate_records' do
|
210
|
-
allow(records).to receive_message_chain(:offset, :limit)
|
211
|
-
|
212
|
-
expect { datatable.send(:paginate_records, records) }.not_to raise_error
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
describe '#sort_records' do
|
217
|
-
it 'calls #order on a collection' do
|
218
|
-
expect(results).to receive(:order)
|
219
|
-
datatable.send(:sort_records, results)
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
describe '#filter_records' do
|
224
|
-
let(:records) { double('User', :where => []) }
|
225
|
-
let(:search_view) { double('view', :params => params) }
|
226
|
-
|
227
|
-
it 'applies search like functionality on a collection' do
|
228
|
-
datatable = AjaxDatatablesRails::Base.new(search_view)
|
229
|
-
allow(datatable).to receive(:searchable_columns) { ['users.foo'] }
|
230
|
-
|
231
|
-
expect(records).to receive(:where)
|
232
|
-
records.where
|
233
|
-
datatable.send(:filter_records, records)
|
234
|
-
end
|
235
|
-
end
|
236
|
-
|
237
|
-
describe '#filter_records with multi word model' do
|
238
|
-
let(:records) { double('UserData', :where => []) }
|
239
|
-
let(:search_view) { double('view', :params => params) }
|
240
|
-
|
241
|
-
it 'applies search like functionality on a collection' do
|
242
|
-
datatable = AjaxDatatablesRails::Base.new(search_view)
|
243
|
-
allow(datatable).to receive(:searchable_columns) { ['user_datas.bar'] }
|
244
|
-
|
245
|
-
expect(records).to receive(:where)
|
246
|
-
records.where
|
247
|
-
datatable.send(:filter_records, records)
|
248
|
-
end
|
249
|
-
end
|
250
|
-
end
|
251
|
-
|
252
|
-
describe 'hook methods' do
|
253
|
-
let(:datatable) { AjaxDatatablesRails::Base.new(view) }
|
254
|
-
|
255
|
-
describe '#data' do
|
256
|
-
it 'raises a MethodNotImplementedError' do
|
257
|
-
expect { datatable.data }.to raise_error(
|
258
|
-
AjaxDatatablesRails::Base::MethodNotImplementedError,
|
259
|
-
'Please implement this method in your class.'
|
260
|
-
)
|
261
|
-
end
|
262
|
-
end
|
263
|
-
|
264
|
-
describe '#get_raw_records' do
|
265
|
-
it 'raises a MethodNotImplementedError' do
|
266
|
-
expect { datatable.get_raw_records }.to raise_error(
|
267
|
-
AjaxDatatablesRails::Base::MethodNotImplementedError,
|
268
|
-
'Please implement this method in your class.'
|
269
|
-
)
|
270
|
-
end
|
271
|
-
end
|
272
|
-
end
|
273
|
-
end
|
274
|
-
|
275
|
-
|
276
|
-
describe AjaxDatatablesRails::Configuration do
|
277
|
-
let(:config) { AjaxDatatablesRails::Configuration.new }
|
278
|
-
|
279
|
-
describe "default config" do
|
280
|
-
it "default db_adapter should :pg (postgresql)" do
|
281
|
-
expect(config.db_adapter).to eq(:pg)
|
282
|
-
end
|
283
|
-
end
|
284
|
-
|
285
|
-
describe "custom config" do
|
286
|
-
it 'should accept db_adapter custom value' do
|
287
|
-
config.db_adapter = :mysql2
|
288
|
-
expect(config.db_adapter).to eq(:mysql2)
|
289
|
-
end
|
290
|
-
end
|
291
|
-
|
292
|
-
describe '#typecast' do
|
293
|
-
params = {
|
294
|
-
:draw => '5',
|
295
|
-
:columns => {
|
296
|
-
"0" => {
|
297
|
-
:data => '0',
|
298
|
-
:name => '',
|
299
|
-
:searchable => true,
|
300
|
-
:orderable => true,
|
301
|
-
:search => { :value => '', :regex => false }
|
302
|
-
},
|
303
|
-
"1" => {
|
304
|
-
:data => '1',
|
305
|
-
:name => '',
|
306
|
-
:searchable => true,
|
307
|
-
:orderable => true,
|
308
|
-
:search => { :value => '', :regex => false }
|
309
|
-
}
|
310
|
-
},
|
311
|
-
:order => { "0" => { :column => '1', :dir => 'desc' } },
|
312
|
-
:start => '0',
|
313
|
-
:length => '10',
|
314
|
-
:search => { :value => '', :regex => false },
|
315
|
-
'_' => '1403141483098'
|
316
|
-
}
|
317
|
-
let(:view) { double('view', :params => params) }
|
318
|
-
let(:datatable) { AjaxDatatablesRails::Base.new(view) }
|
319
|
-
|
320
|
-
it 'returns VARCHAR if :db_adapter is :pg' do
|
321
|
-
expect(datatable.send(:typecast)).to eq('VARCHAR')
|
322
|
-
end
|
323
|
-
|
324
|
-
it 'returns CHAR if :db_adapter is :mysql2' do
|
325
|
-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :mysql2 }
|
326
|
-
expect(datatable.send(:typecast)).to eq('CHAR')
|
327
|
-
end
|
328
|
-
|
329
|
-
it 'returns TEXT if :db_adapter is :sqlite3' do
|
330
|
-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :sqlite3 }
|
331
|
-
expect(datatable.send(:typecast)).to eq('TEXT')
|
332
|
-
end
|
333
|
-
end
|
334
|
-
end
|
335
|
-
|
336
|
-
describe AjaxDatatablesRails do
|
337
|
-
describe "configurations" do
|
338
|
-
context "configurable from outside" do
|
339
|
-
before(:each) do
|
340
|
-
AjaxDatatablesRails.configure do |config|
|
341
|
-
config.db_adapter = :mysql2
|
342
|
-
end
|
343
|
-
end
|
344
|
-
|
345
|
-
it "should have custom value" do
|
346
|
-
expect(AjaxDatatablesRails.config.db_adapter).to eq(:mysql2)
|
347
|
-
end
|
348
|
-
end
|
349
|
-
|
350
|
-
end
|
351
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
class KaminariDatatable < AjaxDatatablesRails::Base
|
4
|
-
end
|
5
|
-
|
6
|
-
describe KaminariDatatable do
|
7
|
-
before(:each) do
|
8
|
-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:paginator) { :kaminari }
|
9
|
-
end
|
10
|
-
|
11
|
-
describe '#paginate_records' do
|
12
|
-
let(:users_database) do
|
13
|
-
double('User',
|
14
|
-
:all => double('RecordCollection',
|
15
|
-
:page => double('Array', :per => [])
|
16
|
-
)
|
17
|
-
)
|
18
|
-
end
|
19
|
-
|
20
|
-
let(:datatable) { KaminariDatatable.new(double('view', :params => {})) }
|
21
|
-
let(:records) { users_database.all }
|
22
|
-
|
23
|
-
it 'calls #page on passed record collection' do
|
24
|
-
expect(records).to receive(:page)
|
25
|
-
datatable.send(:paginate_records, records)
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'calls #per_page on passed record collection' do
|
29
|
-
arry = double('Array', :per => [])
|
30
|
-
allow(records).to receive(:page).and_return(arry)
|
31
|
-
expect(arry).to receive(:per)
|
32
|
-
datatable.send(:paginate_records, records)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
class SimplePaginateDatatable < AjaxDatatablesRails::Base
|
4
|
-
include AjaxDatatablesRails::Extensions::SimplePaginator
|
5
|
-
end
|
6
|
-
|
7
|
-
describe SimplePaginateDatatable do
|
8
|
-
describe '#paginate_records' do
|
9
|
-
let(:users_database) do
|
10
|
-
double('User',
|
11
|
-
:all => double('RecordCollection',
|
12
|
-
:offset => double('Array', :limit => [])
|
13
|
-
)
|
14
|
-
)
|
15
|
-
end
|
16
|
-
|
17
|
-
let(:datatable) { SimplePaginateDatatable.new(double('view', :params => {})) }
|
18
|
-
let(:records) { users_database.all }
|
19
|
-
|
20
|
-
it 'calls #offset on passed record collection' do
|
21
|
-
expect(records).to receive(:offset)
|
22
|
-
datatable.send(:paginate_records, records)
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'calls #limit on passed record collection' do
|
26
|
-
arry = double('Array', :limit => [])
|
27
|
-
allow(records).to receive(:offset).and_return(arry)
|
28
|
-
expect(arry).to receive(:limit)
|
29
|
-
datatable.send(:paginate_records, records)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|