ajax-datatables-rails 0.1.2 → 0.2.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/CHANGELOG.md +6 -0
- data/README.md +30 -18
- data/lib/ajax-datatables-rails/base.rb +11 -11
- data/lib/ajax-datatables-rails/version.rb +1 -1
- data/spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb +56 -15
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41e30fdcf9f0f29415c014aee01b74328f2faf2c
|
4
|
+
data.tar.gz: 3d58fa14346fbc70152eaa3851755e53cc6dc751
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8673b396a04b6cc7fdf3412f5602d3753d072a53bf6c293b96a0a9e82f61981a443f9a820627f8d126d2c2f989037f463931185e09318e9ef22193c084a55c0
|
7
|
+
data.tar.gz: 217019709cb55ca0a495c81632b507a81f868cf7bbd115a73b6c635ab0b69b8f5838cc2b0343524f8c18ac963e671e5ef861512c68cd2dace7f5ee42038f2dd3
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.2.0
|
4
|
+
* This version works with jQuery dataTables ver. 1.10 and it's new API syntax.
|
5
|
+
* Added `legacy` branch to repo. If your project is working with jQuery
|
6
|
+
dataTables ver. 1.9, this is the branch you need to pull, or use the last
|
7
|
+
`0.1.x` version of this gem.
|
8
|
+
|
3
9
|
## 0.1.2
|
4
10
|
* Fixes `where` clause being built even when search term is an empty string.
|
5
11
|
Thanks to [e-fisher](https://github.com/e-fisher) for spotting and fixing this.
|
data/README.md
CHANGED
@@ -3,17 +3,25 @@
|
|
3
3
|
[](https://travis-ci.org/antillas21/ajax-datatables-rails)
|
4
4
|
[](http://badge.fury.io/rb/ajax-datatables-rails)
|
5
5
|
|
6
|
-
###
|
6
|
+
### Versions
|
7
7
|
|
8
|
-
|
9
|
-
>
|
10
|
-
> New maintainer here. Just to let you know that we have
|
11
|
-
> released version 0.1.0 which includes breaking changes.
|
12
|
-
>
|
13
|
-
> Please check the [CHANGELOG](https://github.com/antillas21/ajax-datatables-rails/blob/master/CHANGELOG.md) to learn about these changes.
|
14
|
-
>
|
15
|
-
> All changes have been documented below.
|
8
|
+
[Datatables](http://datatables.net) recently released version 1.10 and deprecated version 1.9 which includes a new API and features.
|
16
9
|
|
10
|
+
If you have dataTables 1.9 in your project and want to keep using it, please use this gem's version `0.1.x` in your `Gemfile`:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
# specific version number
|
14
|
+
gem 'ajax-datatables-rails', '0.1.2'
|
15
|
+
|
16
|
+
# or, support on datatables 1.9
|
17
|
+
gem 'ajax-datatables-rails', git: 'git://github.com/antillas21/ajax-datatables-rails.git', branch: 'legacy'
|
18
|
+
```
|
19
|
+
|
20
|
+
If you have dataTables 1.10 in your project, then use the gem's latest version, or point to the `master` branch.
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
## Description
|
17
25
|
|
18
26
|
Datatables is a nifty jquery plugin that adds the ability to paginate, sort, and search your html tables. When dealing with large tables (more than a couple hundred rows) however, we run into performance issues. These can be fixed by using server-side pagination, but this breaks some datatables functionality.
|
19
27
|
|
@@ -29,13 +37,17 @@ Adding support for `Sequel`, `Mongoid` and `MongoMapper` is a planned feature fo
|
|
29
37
|
|
30
38
|
Add these lines to your application's Gemfile:
|
31
39
|
|
32
|
-
gem 'jquery-datatables-rails'
|
40
|
+
gem 'jquery-datatables-rails', git: 'git://github.com/rweng/jquery-datatables-rails.git', branch: 'master'
|
33
41
|
gem 'ajax-datatables-rails'
|
34
42
|
|
35
43
|
And then execute:
|
36
44
|
|
37
45
|
$ bundle
|
38
46
|
|
47
|
+
The `jquery-datatables-rails` gem is listed as a convenience, to ease adding
|
48
|
+
jQuery dataTables to your Rails project. You can always add the plugin assets
|
49
|
+
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
|
+
|
39
51
|
## Usage
|
40
52
|
*The following examples assume that we are setting up rails-datatables for an index of users from a `User` model*
|
41
53
|
|
@@ -179,10 +191,10 @@ Finally, the javascript to tie this all together. In the appropriate `js.coffee`
|
|
179
191
|
```coffeescript
|
180
192
|
$ ->
|
181
193
|
$('#users-table').dataTable
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
194
|
+
processing: true
|
195
|
+
serverSide: true
|
196
|
+
ajax: $('#users-table').data('source')
|
197
|
+
pagingType: 'full_numbers'
|
186
198
|
# optional, if you want full pagination controls.
|
187
199
|
# Check dataTables documentation to learn more about
|
188
200
|
# available options.
|
@@ -194,10 +206,10 @@ or, if you're using plain javascript:
|
|
194
206
|
|
195
207
|
jQuery(document).ready(function() {
|
196
208
|
$('#users-table').dataTable({
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
209
|
+
"processing": true,
|
210
|
+
"serverSide": true,
|
211
|
+
"ajax": $('#users-table').data('source')
|
212
|
+
"pagingType": "full_numbers",
|
201
213
|
// optional, if you want full pagination controls.
|
202
214
|
// Check dataTables documentation to learn more about
|
203
215
|
// available options.
|
@@ -35,10 +35,10 @@ module AjaxDatatablesRails
|
|
35
35
|
|
36
36
|
def as_json(options = {})
|
37
37
|
{
|
38
|
-
:
|
39
|
-
:
|
40
|
-
:
|
41
|
-
:
|
38
|
+
:draw => params[:draw].to_i,
|
39
|
+
:recordsTotal => get_raw_records.count,
|
40
|
+
:recordsFiltered => filter_records(get_raw_records).count,
|
41
|
+
:data => data
|
42
42
|
}
|
43
43
|
end
|
44
44
|
|
@@ -74,8 +74,8 @@ module AjaxDatatablesRails
|
|
74
74
|
end
|
75
75
|
|
76
76
|
def simple_search(records)
|
77
|
-
return records unless params[:
|
78
|
-
conditions = build_conditions_for(params[:
|
77
|
+
return records unless (params[:search] && params[:search][:value])
|
78
|
+
conditions = build_conditions_for(params[:search][:value])
|
79
79
|
records = records.where(conditions) if conditions
|
80
80
|
records
|
81
81
|
end
|
@@ -98,7 +98,7 @@ module AjaxDatatablesRails
|
|
98
98
|
|
99
99
|
def aggregate_query
|
100
100
|
conditions = searchable_columns.each_with_index.map do |column, index|
|
101
|
-
value = params["
|
101
|
+
value = params[:columns]["#{index}"][:search][:value]
|
102
102
|
search_condition(column, value) unless value.blank?
|
103
103
|
end
|
104
104
|
conditions.compact.reduce(:and)
|
@@ -109,20 +109,20 @@ module AjaxDatatablesRails
|
|
109
109
|
end
|
110
110
|
|
111
111
|
def page
|
112
|
-
(params[:
|
112
|
+
(params[:start].to_i / per_page) + 1
|
113
113
|
end
|
114
114
|
|
115
115
|
def per_page
|
116
|
-
params.fetch(:
|
116
|
+
params.fetch(:length, 10).to_i
|
117
117
|
end
|
118
118
|
|
119
119
|
def sort_column
|
120
|
-
sortable_columns[params[:
|
120
|
+
sortable_columns[params[:order]['0'][:column].to_i]
|
121
121
|
end
|
122
122
|
|
123
123
|
def sort_direction
|
124
124
|
options = %w(desc asc)
|
125
|
-
options.include?(params[:
|
125
|
+
options.include?(params[:order]['0'][:dir]) ? params[:order]['0'][:dir].upcase : 'ASC'
|
126
126
|
end
|
127
127
|
end
|
128
128
|
end
|
@@ -20,8 +20,28 @@ describe AjaxDatatablesRails::Base do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
params = {
|
23
|
-
:
|
24
|
-
:
|
23
|
+
:draw => '5',
|
24
|
+
:columns => {
|
25
|
+
"0" => {
|
26
|
+
:data => '0',
|
27
|
+
:name => '',
|
28
|
+
:searchable => true,
|
29
|
+
:orderable => true,
|
30
|
+
:search => { :value => '', :regex => false }
|
31
|
+
},
|
32
|
+
"1" => {
|
33
|
+
:data => '1',
|
34
|
+
:name => '',
|
35
|
+
:searchable => true,
|
36
|
+
:orderable => true,
|
37
|
+
:search => { :value => '', :regex => false }
|
38
|
+
}
|
39
|
+
},
|
40
|
+
:order => { "0" => { :column => '1', :dir => 'desc' } },
|
41
|
+
:start => '0',
|
42
|
+
:length => '10',
|
43
|
+
:search => { :value => '', :regex => false },
|
44
|
+
'_' => '1403141483098'
|
25
45
|
}
|
26
46
|
let(:view) { double('view', :params => params) }
|
27
47
|
|
@@ -44,16 +64,16 @@ describe AjaxDatatablesRails::Base do
|
|
44
64
|
expect(datatable.send(:offset)).to eq(0)
|
45
65
|
end
|
46
66
|
|
47
|
-
it 'matches the value on view params[:
|
48
|
-
paginated_view = double('view', :params => { :
|
67
|
+
it 'matches the value on view params[:start] minus 1' do
|
68
|
+
paginated_view = double('view', :params => { :start => '11' })
|
49
69
|
datatable = AjaxDatatablesRails::Base.new(paginated_view)
|
50
70
|
expect(datatable.send(:offset)).to eq(10)
|
51
71
|
end
|
52
72
|
end
|
53
73
|
|
54
74
|
describe '#page' do
|
55
|
-
it 'calculates page number from params[:
|
56
|
-
paginated_view = double('view', :params => { :
|
75
|
+
it 'calculates page number from params[:start] and #per_page' do
|
76
|
+
paginated_view = double('view', :params => { :start => '11' })
|
57
77
|
datatable = AjaxDatatablesRails::Base.new(paginated_view)
|
58
78
|
expect(datatable.send(:page)).to eq(2)
|
59
79
|
end
|
@@ -65,8 +85,8 @@ describe AjaxDatatablesRails::Base do
|
|
65
85
|
expect(datatable.send(:per_page)).to eq(10)
|
66
86
|
end
|
67
87
|
|
68
|
-
it 'matches the value on view params[:
|
69
|
-
other_view = double('view', :params => { :
|
88
|
+
it 'matches the value on view params[:length]' do
|
89
|
+
other_view = double('view', :params => { :length => 20 })
|
70
90
|
datatable = AjaxDatatablesRails::Base.new(other_view)
|
71
91
|
expect(datatable.send(:per_page)).to eq(20)
|
72
92
|
end
|
@@ -74,8 +94,15 @@ describe AjaxDatatablesRails::Base do
|
|
74
94
|
|
75
95
|
describe '#sort_column' do
|
76
96
|
it 'returns a column name from the #sorting_columns array' do
|
77
|
-
sort_view = double(
|
78
|
-
|
97
|
+
sort_view = double(
|
98
|
+
'view',
|
99
|
+
:params => {
|
100
|
+
:order => {
|
101
|
+
'0' => { :column => '1' }
|
102
|
+
}
|
103
|
+
}
|
104
|
+
)
|
105
|
+
datatable = AjaxDatatablesRails::Base.new(sort_view)
|
79
106
|
datatable.stub(:sortable_columns) { ['foo', 'bar', 'baz'] }
|
80
107
|
|
81
108
|
expect(datatable.send(:sort_column)).to eq('bar')
|
@@ -84,13 +111,27 @@ describe AjaxDatatablesRails::Base do
|
|
84
111
|
|
85
112
|
describe '#sort_direction' do
|
86
113
|
it 'matches value of params[:sSortDir_0]' do
|
87
|
-
sorting_view = double(
|
114
|
+
sorting_view = double(
|
115
|
+
'view',
|
116
|
+
:params => {
|
117
|
+
:order => {
|
118
|
+
'0' => { :column => '1', :dir => 'desc' }
|
119
|
+
}
|
120
|
+
}
|
121
|
+
)
|
88
122
|
datatable = AjaxDatatablesRails::Base.new(sorting_view)
|
89
123
|
expect(datatable.send(:sort_direction)).to eq('DESC')
|
90
124
|
end
|
91
125
|
|
92
126
|
it 'can only be one option from ASC or DESC' do
|
93
|
-
sorting_view = double(
|
127
|
+
sorting_view = double(
|
128
|
+
'view',
|
129
|
+
:params => {
|
130
|
+
:order => {
|
131
|
+
'0' => { :column => '1', :dir => 'foo' }
|
132
|
+
}
|
133
|
+
}
|
134
|
+
)
|
94
135
|
datatable = AjaxDatatablesRails::Base.new(sorting_view)
|
95
136
|
expect(datatable.send(:sort_direction)).to eq('ASC')
|
96
137
|
end
|
@@ -113,7 +154,7 @@ describe AjaxDatatablesRails::Base do
|
|
113
154
|
|
114
155
|
describe 'perform' do
|
115
156
|
let(:results) { double('Collection', :offset => [], :limit => []) }
|
116
|
-
let(:view) { double('view', :params =>
|
157
|
+
let(:view) { double('view', :params => params) }
|
117
158
|
let(:datatable) { AjaxDatatablesRails::Base.new(view) }
|
118
159
|
|
119
160
|
describe '#paginate_records' do
|
@@ -133,7 +174,7 @@ describe AjaxDatatablesRails::Base do
|
|
133
174
|
|
134
175
|
describe '#filter_records' do
|
135
176
|
let(:records) { double('User', :where => []) }
|
136
|
-
let(:search_view) { double('view', :params =>
|
177
|
+
let(:search_view) { double('view', :params => params) }
|
137
178
|
|
138
179
|
it 'applies search like functionality on a collection' do
|
139
180
|
datatable = AjaxDatatablesRails::Base.new(search_view)
|
@@ -146,7 +187,7 @@ describe AjaxDatatablesRails::Base do
|
|
146
187
|
|
147
188
|
describe '#filter_records with multi word model' do
|
148
189
|
let(:records) { double('UserData', :where => []) }
|
149
|
-
let(:search_view) { double('view', :params =>
|
190
|
+
let(:search_view) { double('view', :params => params) }
|
150
191
|
|
151
192
|
it 'applies search like functionality on a collection' do
|
152
193
|
datatable = AjaxDatatablesRails::Base.new(search_view)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ajax-datatables-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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-06-
|
11
|
+
date: 2014-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|