ajax-datatables-rails 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -3
- data/README.md +148 -9
- data/lib/ajax-datatables-rails.rb +2 -1
- data/lib/ajax-datatables-rails/base.rb +34 -12
- data/lib/ajax-datatables-rails/config.rb +24 -0
- data/lib/ajax-datatables-rails/version.rb +2 -2
- data/lib/generators/datatable/config_generator.rb +17 -0
- data/lib/generators/datatable/templates/ajax_datatables_rails_config.rb +4 -0
- data/spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb +78 -0
- metadata +26 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4dd7a0250f12d3ba425ebbadceec912cd9b5c8e3
|
4
|
+
data.tar.gz: 0f70205e103b95e91970f1a29c9da2149b52b9d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b88ac6e283edd40600632948dc002a7d4361052dc611a0b87997145fb100c92e15652f2a10c7f0fb0c8d76708a78bd3df407f8d822e321b2a7c35171b195f9f
|
7
|
+
data.tar.gz: 442c3da29cddba54dc636b7ce2b325f5a0ef504b12b1994cb9150ce044ba2c8e40360b18b462ab5e5de9300ee57ef7d36aa0ae2ac53714ed7377461bcb125abd
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.2.1
|
4
|
+
* Fix count method to work with select statements under Rails 4.1. Thanks to [Jason Mitchell](https://github.com/mitchej123) for the contribution.
|
5
|
+
* Edits to `README` documentation about the `options` hash. Thanks to [Jonathan E Hogue](https://github.com/hoguej) for pointing out that previous documentation was confusing and didn't address its usage properly.
|
6
|
+
* Edits to `README` documentation on complex model queries inside the `get_raw_records` method. A round of applause to [Zoltan Paulovics](https://github.com/zpaulovics) for contributing this awesome piece of documentation. :smile:
|
7
|
+
* Adds typecast step to `search_condition` method, so now we support having non-text columns inside the `searchable_columns` array.
|
8
|
+
* Adds support for multi-column sorting and multi-term search. Thanks to [Zoltan Paulovics](https://github.com/zpaulovics) for contributing this feature.
|
9
|
+
* Adds optional config initializer, so we can have a base to typecast non text-based columns and perform searches depending on the use of `:mysql2`, `:sqlite3` or `:pg`. Thanks to [M. Saiqul Haq](https://github.com/saiqulhaq) for contributing this feature.
|
10
|
+
|
3
11
|
## 0.2.0
|
4
12
|
* This version works with jQuery dataTables ver. 1.10 and it's new API syntax.
|
5
13
|
* Added `legacy` branch to repo. If your project is working with jQuery
|
@@ -12,7 +20,7 @@
|
|
12
20
|
|
13
21
|
## 0.1.1
|
14
22
|
* Fixes problem on `searchable_columns` where the corresponding model is
|
15
|
-
a composite model name, e.g. `UserData`, `BillingAddress`.
|
23
|
+
a composite model name, e.g. `UserData`, `BillingAddress`.
|
16
24
|
Thanks to [iruca3](https://github.com/iruca3) for the fix.
|
17
25
|
|
18
26
|
## 0.1.0
|
@@ -25,8 +33,8 @@ Thanks to [iruca3](https://github.com/iruca3) for the fix.
|
|
25
33
|
* Removes dependency to pass in a model name to the generator. This way,
|
26
34
|
the developer has more flexibility to implement whatever datatable feature is required.
|
27
35
|
* Datatable constructor accepts an optional `options` hash to provide
|
28
|
-
more flexibility.
|
36
|
+
more flexibility.
|
29
37
|
See [README](https://github.com/antillas21/ajax-datatables-rails/blob/master/README.mds#options) for examples.
|
30
38
|
* Sets generator inside the `Rails` namespace. To generate an
|
31
39
|
`AjaxDatatablesRails` child class, just execute the
|
32
|
-
generator like this: `$ rails generate datatable NAME`.
|
40
|
+
generator like this: `$ rails generate datatable NAME`.
|
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/antillas21/ajax-datatables-rails.svg?branch=master)](https://travis-ci.org/antillas21/ajax-datatables-rails)
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/ajax-datatables-rails.svg)](http://badge.fury.io/rb/ajax-datatables-rails)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/antillas21/ajax-datatables-rails/badges/gpa.svg)](https://codeclimate.com/github/antillas21/ajax-datatables-rails)
|
5
6
|
|
6
7
|
### Versions
|
7
8
|
|
@@ -37,7 +38,7 @@ Adding support for `Sequel`, `Mongoid` and `MongoMapper` is a planned feature fo
|
|
37
38
|
|
38
39
|
Add these lines to your application's Gemfile:
|
39
40
|
|
40
|
-
gem 'jquery-datatables-rails'
|
41
|
+
gem 'jquery-datatables-rails'
|
41
42
|
gem 'ajax-datatables-rails'
|
42
43
|
|
43
44
|
And then execute:
|
@@ -49,7 +50,8 @@ jQuery dataTables to your Rails project. You can always add the plugin assets
|
|
49
50
|
manually via the assets pipeline. If you decide to use the `jquery-datatables-rails` gem, please refer to its installation instructions [here](https://github.com/rweng/jquery-datatables-rails).
|
50
51
|
|
51
52
|
## Usage
|
52
|
-
*The following examples assume that we are setting up
|
53
|
+
*The following examples assume that we are setting up ajax-datatables-rails for an index of users from a `User` model, and that we are using postgresql as
|
54
|
+
our db, because you __should be using it__, if not, please refer to the [Searching on non text-based columns](#searching-on-non-text-based-columns) entry in the Additional Notes section.*
|
53
55
|
|
54
56
|
### Generate
|
55
57
|
Run the following command:
|
@@ -91,6 +93,7 @@ the gems bundled in your project. For example, if your models are using `Kaminar
|
|
91
93
|
* For `searchable_columns`, assign an array of the database columns that you want searchable by datatables. For example `[users.f_name, users.l_name]`
|
92
94
|
|
93
95
|
This gives us:
|
96
|
+
|
94
97
|
```ruby
|
95
98
|
include AjaxDatatablesRails::Extensions::Kaminari
|
96
99
|
|
@@ -101,9 +104,10 @@ end
|
|
101
104
|
def searchable_columns
|
102
105
|
@searchable_columns ||= ['users.f_name', 'users.l_name']
|
103
106
|
end
|
104
|
-
|
105
107
|
```
|
106
108
|
|
109
|
+
[See here](#searching-on-non-text-based-columns) for notes regarding database config (if using something different from `postgre`).
|
110
|
+
|
107
111
|
### Map data
|
108
112
|
```ruby
|
109
113
|
def data
|
@@ -148,6 +152,83 @@ end
|
|
148
152
|
```
|
149
153
|
|
150
154
|
Obviously, you can construct your query as required for the use case the datatable is used. Example: `User.active.with_recent_messages`.
|
155
|
+
__IMPORTANT:__ Make sure to return an `ActiveRecord::Relation` object as the end product of this method. Why? Because the result from
|
156
|
+
this method, will be chained (for now) to `ActiveRecord` methods for sorting, filtering and pagination.
|
157
|
+
|
158
|
+
#### Associated and nested models
|
159
|
+
The previous example has only one single model. But what about if you have some associated nested models and in a report you want to show fields from these tables.
|
160
|
+
|
161
|
+
Take an example that has an `Event, Course, Coursetype, Allocation, Teacher, Contact, Competency and CompetencyType` models. We want to have a datatables report which has the following column:
|
162
|
+
```ruby
|
163
|
+
'coursetypes.name',
|
164
|
+
'courses.name',
|
165
|
+
'events.title',
|
166
|
+
'events.event_start',
|
167
|
+
'events.event_end',
|
168
|
+
'contacts.full_name',
|
169
|
+
'competency_types.name',
|
170
|
+
'events.status'
|
171
|
+
```
|
172
|
+
We want to sort and search on all columns of the list. The related definition would be:
|
173
|
+
```ruby
|
174
|
+
|
175
|
+
def sortable_columns
|
176
|
+
@sortable_columns ||= [
|
177
|
+
'coursetypes.name',
|
178
|
+
'courses.name',
|
179
|
+
'events.title',
|
180
|
+
'events.event_start',
|
181
|
+
'events.event_end',
|
182
|
+
'contacts.last_name',
|
183
|
+
'competency_types.name',
|
184
|
+
'events.status'
|
185
|
+
]
|
186
|
+
end
|
187
|
+
|
188
|
+
def searchable_columns
|
189
|
+
@searchable_columns ||= [
|
190
|
+
'coursetypes.name',
|
191
|
+
'courses.name',
|
192
|
+
'events.title',
|
193
|
+
'events.event_start',
|
194
|
+
'events.event_end',
|
195
|
+
'contacts.last_name',
|
196
|
+
'competency_types.name',
|
197
|
+
'events.status'
|
198
|
+
]
|
199
|
+
end
|
200
|
+
|
201
|
+
def get_raw_records
|
202
|
+
Event.joins(
|
203
|
+
{ course: :coursetype },
|
204
|
+
{ allocations: {
|
205
|
+
teacher: [:contact, {competencies: :competency_type}]
|
206
|
+
}
|
207
|
+
}).distinct
|
208
|
+
end
|
209
|
+
```
|
210
|
+
|
211
|
+
__Some comments for the above code:__
|
212
|
+
|
213
|
+
1. In the list we show `full_name`, but in `sortable_columns` and `searchable_columns` we use `last_name` from the `Contact` model. The reason is we can use only database columns as sort or search fields and the full_name is not a database field.
|
214
|
+
|
215
|
+
2. In the `get_raw_records` method we have quite a complex query having one to many and may to many associations using the joins ActiveRecord method. The joins will generate INNER JOIN relations in the SQL query. In this case we do not include all event in the report if we have events which is not associated with any model record from the relation.
|
216
|
+
|
217
|
+
3. To have all event records in the list we should use the `.includes` method, which generate LEFT OUTER JOIN relation of the SQL query. __IMPORTANT:__ Make sure to append `.references(:related_model)` with any associated model. That forces the eager loading of all the associated models by one SQL query, and the search condition for any column works fine. Otherwise the `:recordsFiltered => filter_records(get_raw_records).count(:all)` will generate 2 SQL queries (one for the Event model, and then another for the associated tables). The `:recordsFiltered => filter_records(get_raw_records).count(:all)` will use only the first one to return from the ActiveRecord::Relation object in `get_raw_records` and you will get an error message of __Unknown column 'yourtable.yourfield' in 'where clause'__ in case the search field value is not empty.
|
218
|
+
|
219
|
+
So the query using the `.includes()` method is:
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
def get_raw_records
|
223
|
+
Event.includes(
|
224
|
+
{ course: :coursetype },
|
225
|
+
{ allocations: {
|
226
|
+
teacher: [:contact, { competencies: :competency_type }]
|
227
|
+
}
|
228
|
+
}
|
229
|
+
).references(:course).distinct
|
230
|
+
end
|
231
|
+
```
|
151
232
|
|
152
233
|
### Controller
|
153
234
|
Set up the controller to respond to JSON
|
@@ -208,7 +289,7 @@ jQuery(document).ready(function() {
|
|
208
289
|
$('#users-table').dataTable({
|
209
290
|
"processing": true,
|
210
291
|
"serverSide": true,
|
211
|
-
"ajax": $('#users-table').data('source')
|
292
|
+
"ajax": $('#users-table').data('source'),
|
212
293
|
"pagingType": "full_numbers",
|
213
294
|
// optional, if you want full pagination controls.
|
214
295
|
// Check dataTables documentation to learn more about
|
@@ -219,9 +300,43 @@ jQuery(document).ready(function() {
|
|
219
300
|
|
220
301
|
### Additional Notes
|
221
302
|
|
303
|
+
#### Searching on non text-based columns
|
304
|
+
|
305
|
+
It always comes the time when you need to add a non-string/non-text based column to the `@searchable_columns` array, so you can perform searches against these column types (example: numeric, date, time).
|
306
|
+
|
307
|
+
We recently added the ability to (automatically) typecast these column types and have this scenario covered. Please note however, if you are using something different from `postgre` (with the `:pg` gem), like `mysql` or `sqlite`, then you need to add an initializer in your application's `config/initializers` directory.
|
308
|
+
|
309
|
+
If you don't perform this step (again, if using something different from `postgre`), your database will complain that it does not understand the default typecast used to enable such searches.
|
310
|
+
|
311
|
+
You have two options to create this initializer:
|
312
|
+
|
313
|
+
* use the provided (and recommended) generator (and then just edit the file);
|
314
|
+
* create the file from scratch.
|
315
|
+
|
316
|
+
To use the generator, from the terminal execute:
|
317
|
+
|
318
|
+
```
|
319
|
+
$ bundle exec rails generate datatable:config
|
320
|
+
```
|
321
|
+
|
322
|
+
Doing so, will create the `config/initializers/ajax_datatables_rails.rb` file with the following content:
|
323
|
+
|
324
|
+
```ruby
|
325
|
+
AjaxDatatablesRails.configure do |config|
|
326
|
+
# available options for db_adapter are: :pg, :mysql2, :sqlite3
|
327
|
+
# config.db_adapter = :pg
|
328
|
+
end
|
329
|
+
```
|
330
|
+
|
331
|
+
Uncomment the `config.db_adapter` line and set the corresponding value to your
|
332
|
+
database and gem. This is all you need.
|
333
|
+
|
334
|
+
If you want to make the file from scratch, just copy the above code block into a file inside the `config/initializers` directory.
|
335
|
+
|
336
|
+
|
222
337
|
#### Using view helpers
|
223
338
|
|
224
|
-
Sometimes you'll need to use view helper methods like `link_to`, `h`, `mailto`, `edit_resource_path` in the returned JSON representation returned by the `data` method.
|
339
|
+
Sometimes you'll need to use view helper methods like `link_to`, `h`, `mailto`, `edit_resource_path` in the returned JSON representation returned by the `data` method.
|
225
340
|
|
226
341
|
To have these methods available to be used, this is the way to go:
|
227
342
|
|
@@ -231,10 +346,10 @@ class MyCustomDatatable < AjaxDatatablesRails::Base
|
|
231
346
|
def_delegator :@view, :link_to
|
232
347
|
def_delegator :@view, :h
|
233
348
|
def_delegator :@view, :mail_to
|
234
|
-
|
349
|
+
|
235
350
|
# or define them in one pass
|
236
351
|
def_delegators :@view, :link_to, :h, :mailto, :edit_resource_path, :other_method
|
237
|
-
|
352
|
+
|
238
353
|
# now, you'll have these methods available to be used anywhere
|
239
354
|
# example: mapping the 2d jsonified array returned.
|
240
355
|
def data
|
@@ -261,9 +376,23 @@ end
|
|
261
376
|
datatable = UnrespondedMessagesDatatable.new(view_context,
|
262
377
|
{ :foo => { :bar => Baz.new }, :from => 1.month.ago }
|
263
378
|
)
|
379
|
+
```
|
380
|
+
So, now inside your class code, you can use those options like this:
|
381
|
+
|
382
|
+
|
383
|
+
```ruby
|
384
|
+
# let's see an example
|
385
|
+
def from
|
386
|
+
@from ||= options[:from].beginning_of_day
|
387
|
+
end
|
388
|
+
|
389
|
+
def to
|
390
|
+
@to ||= Date.today.end_of_day
|
391
|
+
end
|
264
392
|
|
265
|
-
|
266
|
-
|
393
|
+
def get_raw_records
|
394
|
+
Message.unresponded.where(received_at: from..to)
|
395
|
+
end
|
267
396
|
```
|
268
397
|
|
269
398
|
#### Generator Syntax
|
@@ -285,6 +414,16 @@ $ rails generate datatable UnrespondedMessages
|
|
285
414
|
|
286
415
|
In the end, it's up to the developer which model(s), scope(s), relationship(s) (or else) to employ inside the datatable class to retrieve records from the database.
|
287
416
|
|
417
|
+
## Tutorial
|
418
|
+
|
419
|
+
Tutorial for Integrating `ajax-datatable-rails`, on Rails 4 .
|
420
|
+
|
421
|
+
[Part-1 The-Installation](https://github.com/antillas21/ajax-datatables-rails/wiki/Part-1----The-Installation)
|
422
|
+
|
423
|
+
[Part 2 The Datatables with ajax functionality](https://github.com/antillas21/ajax-datatables-rails/wiki/Part-2-The-Datatables-with-ajax-functionality)
|
424
|
+
|
425
|
+
The complete project code for this tutorial series is available on [github](https://github.com/trkrameshkumar/simple_app).
|
426
|
+
|
288
427
|
|
289
428
|
## Contributing
|
290
429
|
|
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'ajax-datatables-rails/version'
|
2
|
+
require 'ajax-datatables-rails/config'
|
2
3
|
require 'ajax-datatables-rails/base'
|
3
4
|
require 'ajax-datatables-rails/extensions/simple_paginator'
|
4
5
|
require 'ajax-datatables-rails/extensions/kaminari'
|
5
6
|
require 'ajax-datatables-rails/extensions/will_paginate'
|
6
7
|
|
7
8
|
module AjaxDatatablesRails
|
8
|
-
end
|
9
|
+
end
|
@@ -11,6 +11,10 @@ module AjaxDatatablesRails
|
|
11
11
|
@options = options
|
12
12
|
end
|
13
13
|
|
14
|
+
def config
|
15
|
+
@config ||= AjaxDatatablesRails.config
|
16
|
+
end
|
17
|
+
|
14
18
|
def sortable_columns
|
15
19
|
@sortable_columns ||= []
|
16
20
|
end
|
@@ -36,8 +40,8 @@ module AjaxDatatablesRails
|
|
36
40
|
def as_json(options = {})
|
37
41
|
{
|
38
42
|
:draw => params[:draw].to_i,
|
39
|
-
:recordsTotal => get_raw_records.count,
|
40
|
-
:recordsFiltered => filter_records(get_raw_records).count,
|
43
|
+
:recordsTotal => get_raw_records.count(:all),
|
44
|
+
:recordsFiltered => filter_records(get_raw_records).count(:all),
|
41
45
|
:data => data
|
42
46
|
}
|
43
47
|
end
|
@@ -52,12 +56,16 @@ module AjaxDatatablesRails
|
|
52
56
|
records = get_raw_records
|
53
57
|
records = sort_records(records)
|
54
58
|
records = filter_records(records)
|
55
|
-
records = paginate_records(records)
|
59
|
+
records = paginate_records(records) unless params[:length] == '-1'
|
56
60
|
records
|
57
61
|
end
|
58
62
|
|
59
63
|
def sort_records(records)
|
60
|
-
|
64
|
+
sort_by = []
|
65
|
+
params[:order].each_value do |item|
|
66
|
+
sort_by << "#{sort_column(item)} #{sort_direction(item)}"
|
67
|
+
end
|
68
|
+
records.order(sort_by.join(", "))
|
61
69
|
end
|
62
70
|
|
63
71
|
def paginate_records(records)
|
@@ -74,7 +82,7 @@ module AjaxDatatablesRails
|
|
74
82
|
end
|
75
83
|
|
76
84
|
def simple_search(records)
|
77
|
-
return records unless (params[:search] && params[:search][:value])
|
85
|
+
return records unless (params[:search].present? && params[:search][:value].present?)
|
78
86
|
conditions = build_conditions_for(params[:search][:value])
|
79
87
|
records = records.where(conditions) if conditions
|
80
88
|
records
|
@@ -87,23 +95,37 @@ module AjaxDatatablesRails
|
|
87
95
|
end
|
88
96
|
|
89
97
|
def build_conditions_for(query)
|
90
|
-
|
98
|
+
search_for = query.split(' ')
|
99
|
+
criteria = search_for.inject([]) do |criteria, atom|
|
100
|
+
criteria << searchable_columns.map { |col| search_condition(col, atom) }.reduce(:or)
|
101
|
+
end.reduce(:and)
|
102
|
+
criteria
|
91
103
|
end
|
92
104
|
|
93
105
|
def search_condition(column, value)
|
94
106
|
model, column = column.split('.')
|
95
107
|
model = model.singularize.titleize.gsub( / /, '' ).constantize
|
96
|
-
|
108
|
+
|
109
|
+
casted_column = ::Arel::Nodes::NamedFunction.new('CAST', [model.arel_table[column.to_sym].as(typecast)])
|
110
|
+
casted_column.matches("%#{value}%")
|
97
111
|
end
|
98
112
|
|
99
113
|
def aggregate_query
|
100
114
|
conditions = searchable_columns.each_with_index.map do |column, index|
|
101
|
-
value = params[:columns]["#{index}"][:search][:value]
|
115
|
+
value = params[:columns]["#{index}"][:search][:value] if params[:columns]
|
102
116
|
search_condition(column, value) unless value.blank?
|
103
117
|
end
|
104
118
|
conditions.compact.reduce(:and)
|
105
119
|
end
|
106
120
|
|
121
|
+
def typecast
|
122
|
+
case config.db_adapter
|
123
|
+
when :pg then 'VARCHAR'
|
124
|
+
when :mysql2 then 'CHAR'
|
125
|
+
when :sqlite3 then 'TEXT'
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
107
129
|
def offset
|
108
130
|
(page - 1) * per_page
|
109
131
|
end
|
@@ -116,13 +138,13 @@ module AjaxDatatablesRails
|
|
116
138
|
params.fetch(:length, 10).to_i
|
117
139
|
end
|
118
140
|
|
119
|
-
def sort_column
|
120
|
-
sortable_columns[
|
141
|
+
def sort_column(item)
|
142
|
+
sortable_columns[item['column'].to_i]
|
121
143
|
end
|
122
144
|
|
123
|
-
def sort_direction
|
145
|
+
def sort_direction(item)
|
124
146
|
options = %w(desc asc)
|
125
|
-
options.include?(
|
147
|
+
options.include?(item['dir']) ? item['dir'].upcase : 'ASC'
|
126
148
|
end
|
127
149
|
end
|
128
150
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'active_support/configurable'
|
2
|
+
|
3
|
+
module AjaxDatatablesRails
|
4
|
+
|
5
|
+
# configure AjaxDatatablesRails global settings
|
6
|
+
# AjaxDatatablesRails.configure do |config|
|
7
|
+
# config.db_adapter = :pg
|
8
|
+
# end
|
9
|
+
def self.configure &block
|
10
|
+
yield @config ||= AjaxDatatablesRails::Configuration.new
|
11
|
+
end
|
12
|
+
|
13
|
+
# AjaxDatatablesRails global settings
|
14
|
+
def self.config
|
15
|
+
@config ||= AjaxDatatablesRails::Configuration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
class Configuration
|
19
|
+
include ActiveSupport::Configurable
|
20
|
+
|
21
|
+
# default db_adapter is pg (postgresql)
|
22
|
+
config_accessor(:db_adapter) { :pg }
|
23
|
+
end
|
24
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
1
|
module AjaxDatatablesRails
|
2
|
-
VERSION = '0.2.
|
3
|
-
end
|
2
|
+
VERSION = '0.2.1'
|
3
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Datatable
|
4
|
+
module Generators
|
5
|
+
class ConfigGenerator < ::Rails::Generators::Base
|
6
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
7
|
+
desc <<DESC
|
8
|
+
Description:
|
9
|
+
Creates an initializer file for AjaxDatatablesRails configuration at config/initializers.
|
10
|
+
DESC
|
11
|
+
|
12
|
+
def copy_config_file
|
13
|
+
template 'ajax_datatables_rails_config.rb', 'config/initializers/ajax_datatables_rails.rb'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -221,3 +221,81 @@ describe AjaxDatatablesRails::Base do
|
|
221
221
|
end
|
222
222
|
end
|
223
223
|
end
|
224
|
+
|
225
|
+
|
226
|
+
describe AjaxDatatablesRails::Configuration do
|
227
|
+
let(:config) { AjaxDatatablesRails::Configuration.new }
|
228
|
+
|
229
|
+
describe "default config" do
|
230
|
+
it "default db_adapter should :pg (postgresql)" do
|
231
|
+
expect(config.db_adapter).to eq(:pg)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
describe "custom config" do
|
236
|
+
it 'should accept db_adapter custom value' do
|
237
|
+
config.db_adapter = :mysql2
|
238
|
+
expect(config.db_adapter).to eq(:mysql2)
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
describe '#typecast' do
|
243
|
+
params = {
|
244
|
+
:draw => '5',
|
245
|
+
:columns => {
|
246
|
+
"0" => {
|
247
|
+
:data => '0',
|
248
|
+
:name => '',
|
249
|
+
:searchable => true,
|
250
|
+
:orderable => true,
|
251
|
+
:search => { :value => '', :regex => false }
|
252
|
+
},
|
253
|
+
"1" => {
|
254
|
+
:data => '1',
|
255
|
+
:name => '',
|
256
|
+
:searchable => true,
|
257
|
+
:orderable => true,
|
258
|
+
:search => { :value => '', :regex => false }
|
259
|
+
}
|
260
|
+
},
|
261
|
+
:order => { "0" => { :column => '1', :dir => 'desc' } },
|
262
|
+
:start => '0',
|
263
|
+
:length => '10',
|
264
|
+
:search => { :value => '', :regex => false },
|
265
|
+
'_' => '1403141483098'
|
266
|
+
}
|
267
|
+
let(:view) { double('view', :params => params) }
|
268
|
+
let(:datatable) { AjaxDatatablesRails::Base.new(view) }
|
269
|
+
|
270
|
+
it 'returns VARCHAR if :db_adapter is :pg' do
|
271
|
+
expect(datatable.send(:typecast)).to eq('VARCHAR')
|
272
|
+
end
|
273
|
+
|
274
|
+
it 'returns CHAR if :db_adapter is :mysql2' do
|
275
|
+
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :mysql2 }
|
276
|
+
expect(datatable.send(:typecast)).to eq('CHAR')
|
277
|
+
end
|
278
|
+
|
279
|
+
it 'returns TEXT if :db_adapter is :sqlite3' do
|
280
|
+
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:db_adapter) { :sqlite3 }
|
281
|
+
expect(datatable.send(:typecast)).to eq('TEXT')
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
describe AjaxDatatablesRails do
|
287
|
+
describe "configurations" do
|
288
|
+
context "configurable from outside" do
|
289
|
+
before(:each) do
|
290
|
+
AjaxDatatablesRails.configure do |config|
|
291
|
+
config.db_adapter = :mysql2
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
it "should have custom value" do
|
296
|
+
expect(AjaxDatatablesRails.config.db_adapter).to eq(:mysql2)
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
end
|
301
|
+
end
|
metadata
CHANGED
@@ -1,97 +1,97 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ajax-datatables-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Quenneville
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: generator_spec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: pry
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rake
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rails
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: 3.1.0
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: 3.1.0
|
97
97
|
description: A gem that simplifies using datatables and hundreds of records via ajax
|
@@ -101,12 +101,20 @@ executables: []
|
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
|
+
- CHANGELOG.md
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- lib/ajax-datatables-rails.rb
|
104
110
|
- lib/ajax-datatables-rails/base.rb
|
111
|
+
- lib/ajax-datatables-rails/config.rb
|
105
112
|
- lib/ajax-datatables-rails/extensions/kaminari.rb
|
106
113
|
- lib/ajax-datatables-rails/extensions/simple_paginator.rb
|
107
114
|
- lib/ajax-datatables-rails/extensions/will_paginate.rb
|
108
115
|
- lib/ajax-datatables-rails/version.rb
|
109
|
-
- lib/
|
116
|
+
- lib/generators/datatable/config_generator.rb
|
117
|
+
- lib/generators/datatable/templates/ajax_datatables_rails_config.rb
|
110
118
|
- lib/generators/rails/datatable_generator.rb
|
111
119
|
- lib/generators/rails/templates/datatable.rb
|
112
120
|
- spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb
|
@@ -114,11 +122,6 @@ files:
|
|
114
122
|
- spec/ajax-datatables-rails/simple_paginator_spec.rb
|
115
123
|
- spec/ajax-datatables-rails/will_paginate_spec.rb
|
116
124
|
- spec/spec_helper.rb
|
117
|
-
- CHANGELOG.md
|
118
|
-
- Gemfile
|
119
|
-
- LICENSE
|
120
|
-
- Rakefile
|
121
|
-
- README.md
|
122
125
|
homepage: ''
|
123
126
|
licenses: []
|
124
127
|
metadata: {}
|
@@ -128,17 +131,17 @@ require_paths:
|
|
128
131
|
- lib
|
129
132
|
required_ruby_version: !ruby/object:Gem::Requirement
|
130
133
|
requirements:
|
131
|
-
- -
|
134
|
+
- - ">="
|
132
135
|
- !ruby/object:Gem::Version
|
133
136
|
version: 1.9.2
|
134
137
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
138
|
requirements:
|
136
|
-
- -
|
139
|
+
- - ">="
|
137
140
|
- !ruby/object:Gem::Version
|
138
141
|
version: '0'
|
139
142
|
requirements: []
|
140
143
|
rubyforge_project:
|
141
|
-
rubygems_version: 2.
|
144
|
+
rubygems_version: 2.2.2
|
142
145
|
signing_key:
|
143
146
|
specification_version: 4
|
144
147
|
summary: A wrapper around datatable's ajax methods that allow synchronization with
|