8000 Updates code to work with jQuery dataTables ver. 1.10 · daniALGA/ajax-datatables-rails@d553727 · GitHub
[go: up one dir, main page]

Skip to content

Commit d553727

Browse files
committed
Updates code to work with jQuery dataTables ver. 1.10
* Code updated to work with dataTables 1.10 neew API and syntax. * Bumps version number. * Edits CHANGELOG. * Edits README.
1 parent 2dd7de3 commit d553727

File tree

5 files changed

+87
-36
lines changed

5 files changed

+87
-36
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

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+
39
## 0.1.2
410
* Fixes `where` clause being built even when search term is an empty string.
511
Thanks to [e-fisher](https://github.com/e-fisher) for spotting and fixing this.

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ Adding support for `Sequel`, `Mongoid` and `MongoMapper` is a planned feature fo
3737

3838
Add these lines to your application's Gemfile:
3939

40-
gem 'jquery-datatables-rails'
40+
gem 'jquery-datatables-rails', git: 'git://github.com/rweng/jquery-datatables-rails.git', branch: 'master'
4141
gem 'ajax-datatables-rails'
4242

4343
And then execute:
4444

4545
$ bundle
4646

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+
4751
## Usage
4852
*The following examples assume that we are setting up rails-datatables for an index of users from a `User` model*
4953

@@ -187,10 +191,10 @@ Finally, the javascript to tie this all together. In the appropriate `js.coffee`
187191
```coffeescript
188192
$ ->
189193
$('#users-table').dataTable
190-
bProcessing: true
191-
bServerSide: true
192-
sAjaxSource: $('#users-table').data('source')
193-
sPaginationType: 'full_numbers'
194+
processing: true
195+
serverSide: true
196+
ajax: $('#users-table').data('source')
197+
pagingType: 'full_numbers'
194198
# optional, if you want full pagination controls.
195199
# Check dataTables documentation to learn more about
196200
# available options.
@@ -202,10 +206,10 @@ or, if you're using plain javascript:
202206

203207
jQuery(document).ready(function() {
204208
$('#users-table').dataTable({
205-
'bProcessing': true,
206-
'bServerSide': true,
207-
'sAjaxSource': $('#users-table').data('source'),
208-
'sPaginationType': 'full_numbers',
209+
"processing": true,
210+
"serverSide": true,
211+
"ajax": $('#users-table').data('source')
212+
"pagingType": "full_numbers",
209213
// optional, if you want full pagination controls.
210214
// Check dataTables documentation to learn more about
211215
// available options.

lib/ajax-datatables-rails/base.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def get_raw_records
3535

3636
def as_json(options = {})
3737
{
38-
:sEcho => params[:sEcho].to_i,
39-
:iTotalRecords => get_raw_records.count,
40-
:iTotalDisplayRecords => filter_records(get_raw_records).count,
41-
:aaData => data
38+
:draw => params[:draw].to_i,
39+
:recordsTotal => get_raw_records.count,
40+
:recordsFiltered => filter_records(get_raw_records).count,
41+
:data => data
4242
}
4343
end
4444

@@ -74,8 +74,8 @@ def filter_records(records)
7474
end
7575

7676
def simple_search(records)
77-
return records unless params[:sSearch]
78-
conditions = build_conditions_for(params[:sSearch])
77+
return records unless (params[:search] && params[:search][:value])
78+
conditions = build_conditions_for(params[:search][:value])
7979
records = records.where(conditions) if conditions
8080
records
8181
end
@@ -98,7 +98,7 @@ def search_condition(column, value)
9898

9999
def aggregate_query
100100
conditions = searchable_columns.each_with_index.map do |column, index|
101-
value = params["sSearch_#{index}".to_sym]
101+
value = params[:columns]["#{index}"][:search][:value]
102102
search_condition(column, value) unless value.blank?
103103
end
104104
conditions.compact.reduce(:and)
@@ -109,20 +109,20 @@ def offset
109109
end
110110

111111
def page
112-
(params[:iDisplayStart].to_i / per_page) + 1
112+
(params[:start].to_i / per_page) + 1
113113
end
114114

115115
def per_page
116-
params.fetch(:iDisplayLength, 10).to_i
116+
params.fetch(:length, 10).to_i
117117
end
118118

119119
def sort_column
120-
sortable_columns[params[:iSortCol_0].to_i]
120+
sortable_columns[params[:order]['0'][:column].to_i]
121121
end
122122

123123
def sort_direction
124124
options = %w(desc asc)
125-
options.include?(params[:sSortDir_0]) ? params[:sSortDir_0].upcase : 'ASC'
125+
options.include?(params[:order]['0'][:dir]) ? params[:order]['0'][:dir].upcase : 'ASC'
126126
end
127127
end
128128
end

lib/ajax-datatables-rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module AjaxDatatablesRails
2-
VERSION = '0.1.2'
2+
VERSION = '0.2.0'
33
end

spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,28 @@ def self.arel_table
2020
end
2121

2222
params = {
23-
:sEcho => '0', :sSortDir_0 => 'asc',
24-
:iSortCol_0 => '1', :iDisplayStart => '11'
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'
2545
}
2646
let(:view) { double('view', :params => params) }
2747

@@ -44,16 +64,16 @@ def self.arel_table
4464
expect(datatable.send(:offset)).to eq(0)
4565
end
4666

47-
it 'matches the value on view params[:iDisplayStart] minus 1' do
48-
paginated_view = double('view', :params => { :iDisplayStart => '11' })
67+
it 'matches the value on view params[:start] minus 1' do
68+
paginated_view = double('view', :params => { :start => '11' })
4969
datatable = AjaxDatatablesRails::Base.new(paginated_view)
5070
expect(datatable.send(:offset)).to eq(10)
5171
end
5272
end
5373

5474
describe '#page' do
55-
it 'calculates page number from params[:iDisplayStart] and #per_page' do
56-
paginated_view = double('view', :params => { :iDisplayStart => '11' })
75+
it 'calculates page number from params[:start] and #per_page' do
76+
paginated_view = double('view', :params => { :start => '11' })
5777
datatable = AjaxDatatablesRails::Base.new(paginated_view)
5878
expect(datatable.send(:page)).to eq(2)
5979
end
@@ -65,17 +85,24 @@ def self.arel_table
6585
expect(datatable.send(:per_page)).to eq(10)
6686
end
6787

68-
it 'matches the value on view params[:iDisplayLength]' do
69-
other_view = double('view', :params => { :iDisplayLength => 20 })
88+
it 'matches the value on view params[:length]' do
89+
other_view = double('view', :params => { :length => 20 })
7090
datatable = AjaxDatatablesRails::Base.new(other_view)
7191
expect(datatable.send(:per_page)).to eq(20)
7292
end
7393
end
7494

7595
describe '#sort_column' do
7696
it 'returns a column name from the #sorting_columns array' do
77-
sort_view = double('view', :params => { :iSortCol_0 => '1' })
78-
datatable = AjaxDatatablesRails::Base.new(view)
97+
sort_view = double(
98+
'view',
99+
:params => {
100+
:order => {
101+
'0' => { :column => '1' }
102+
}
103+
}
104+
)
105+
datatable = AjaxDatatablesRails::Base.new(sort_view)
79106
datatable.stub(:sortable_columns) { ['foo', 'bar', 'baz'] }
80107

81108
expect(datatable.send(:sort_column)).to eq('bar')
@@ -84,13 +111,27 @@ def self.arel_table
84111

85112
describe '#sort_direction' do
86113
it 'matches value of params[:sSortDir_0]' do
87-
sorting_view = double('view', :params => { :sSortDir_0 => 'desc' })
114+
sorting_view = double(
115+
'view',
116+
:params => {
117+
:order => {
118+
'0' => { :column => '1', :dir => 'desc' }
119+
}
120+
}
121+
)
88122
datatable = AjaxDatatablesRails::Base.new(sorting_view)
89123
expect(datatable.send(:sort_direction)).to eq('DESC')
90124
end
91125

92126
it 'can only be one option from ASC or DESC' do
93-
sorting_view = double('view', :params => { :sSortDir_0 => 'foo' })
127+
sorting_view = double(
128+
'view',
129+
:params => {
130+
:order => {
131+
'0' => { :column => '1', :dir => 'foo' }
132+
}
133+
}
134+
)
94135
datatable = AjaxDatatablesRails::Base.new(sorting_view)
95136
expect(datatable.send(:sort_direction)).to eq('ASC')
96137
end
@@ -113,7 +154,7 @@ def self.arel_table
113154

114155
describe 'perform' do
115156
let(:results) { double('Collection', :offset => [], :limit => []) }
116-
let(:view) { double('view', :params => {}) }
157+
let(:view) { double('view', :params => params) }
117158
let(:datatable) { AjaxDatatablesRails::Base.new(view) }
118159

119160
describe '#paginate_records' do
@@ -133,7 +174,7 @@ def self.arel_table
133174

134175
describe '#filter_records' do
135176
let(:records) { double('User', :where => []) }
136-
let(:search_view) { double('view', :params => { :sSearch => 'foo' }) }
177+
let(:search_view) { double('view', :params => params) }
137178

138179
it 'applies search like functionality on a collection' do
139180
datatable = AjaxDatatablesRails::Base.new(search_view)
@@ -146,7 +187,7 @@ def self.arel_table
146187

147188
describe '#filter_records with multi word model' do
148189
let(:records) { double('UserData', :where => []) }
149-
let(:search_view) { double('view', :params => { :sSearch => 'bar' }) }
190+
let(:search_view) { double('view', :params => params) }
150191

151192
it 'applies search like functionality on a collection' do
152193
datatable = AjaxDatatablesRails::Base.new(search_view)

0 commit comments

Comments
 (0)
0