Skip to content

Commit bb910b2

Browse files
committed
Added processing arrays in json params
1 parent 2f826df commit bb910b2

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

lib/ajax-datatables-rails/datatable/datatable.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ def page
7070
def get_param(param)
7171
return {} if options[param].nil?
7272

73-
options[param].to_unsafe_h.with_indifferent_access
73+
if options[param].is_a? Array
74+
hash = {}
75+
options[param].each_with_index { |value, index| hash[index] = value }
76+
hash
77+
else
78+
options[param].to_unsafe_h.with_indifferent_access
79+
end
7480
end
7581

7682
end

spec/ajax-datatables-rails/datatable/datatable_spec.rb

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
describe AjaxDatatablesRails::Datatable::Datatable do
44

55
let(:datatable) { ComplexDatatable.new(sample_params).datatable }
6+
let(:datatable_json) { ComplexDatatable.new(sample_params_json).datatable }
67
let(:order_option) { {'0'=>{'column'=>'0', 'dir'=>'asc'}, '1'=>{'column'=>'1', 'dir'=>'desc'}} }
8+
let(:order_option_json) { [{'column'=>'0', 'dir'=>'asc'}, {'column'=>'1', 'dir'=>'desc'}] }
79

8-
describe 'order methods' do
10+
shared_examples 'order methods' do
911
it 'should be orderable' do
1012
expect(datatable.orderable?).to eq(true)
1113
end
@@ -35,6 +37,28 @@
3537
end
3638
end
3739

40+
shared_examples 'columns methods' do
41+
it 'should have 4 columns' do
42+
expect(datatable.columns.count).to eq(6)
43+
end
44+
45+
it 'child class' do
46+
expect(datatable.columns.first).to be_a(AjaxDatatablesRails::Datatable::Column)
47+
end
48+
end
49+
50+
describe 'with query params' do
51+
it_behaves_like 'order methods'
52+
it_behaves_like 'columns methods'
53+
end
54+
55+
describe 'with json params' do
56+
let(:order_option) { order_option_json }
57+
let(:datatable) { datatable_json }
58+
it_behaves_like 'order methods'
59+
it_behaves_like 'columns methods'
60+
end
61+
3862
describe 'search methods' do
3963
it 'should be searchable' do
4064
datatable.options[:search][:value] = 'atom'
@@ -51,16 +75,6 @@
5175
end
5276
end
5377

54-
describe 'columns methods' do
55-
it 'should have 4 columns' do
56-
expect(datatable.columns.count).to eq(6)
57-
end
58-
59-
it 'child class' do
60-
expect(datatable.columns.first).to be_a(AjaxDatatablesRails::Datatable::Column)
61-
end
62-
end
63-
6478
describe 'option methods' do
6579
before :each do
6680
datatable.options[:start] = '50'

spec/support/test_helpers.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ def sample_params
5151
}
5252
)
5353
end
54+
55+
def sample_params_json
56+
hash_params = sample_params.to_unsafe_h
57+
hash_params["columns"] = hash_params["columns"].values
58+
hash_params["order"] = hash_params["order"].values
59+
ActionController::Parameters.new(hash_params)
60+
end
5461
# rubocop:enable Metrics/MethodLength
5562

5663
class ComplexDatatable < AjaxDatatablesRails::ActiveRecord

0 commit comments

Comments
 (0)