Skip to content

Commit f778e58

Browse files
committed
Fix undefined method `fetch' for nil:NilClass
Fix #307
1 parent ace8e8a commit f778e58

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Raise an error when column's `cond:` setting is unknown
1313
* Make global search and column search work together (merge: [#350](https://github.com/jbox-web/ajax-datatables-rails/pull/350), fixes: [#258](https://github.com/jbox-web/ajax-datatables-rails/issues/258))
1414
* Fix: date_range doesn't support searching by a date greater than today (merge: [#351](https://github.com/jbox-web/ajax-datatables-rails/pull/351))
15+
* Fix: undefined method `fetch' for nil:NilClass (fix: [#307](https://github.com/jbox-web/ajax-datatables-rails/issues/307))
1516

1617
* `AjaxDatatablesRails.config` is removed with no replacement. The gem is now configless :)
1718
* `AjaxDatatablesRails.config.db_adapter=` is removed and is configured per datatable class now. It defaults to Rails DB adapter. (fixes [#364](https://github.com/jbox-web/ajax-datatables-rails/issues/364))

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ def initialize(datatable, index, options)
1515
@datatable = datatable
1616
@index = index
1717
@options = options
18-
@view_column = datatable.view_columns[options[:data].to_sym]
18+
@view_column = datatable.view_columns[column_name]
1919
validate_settings!
2020
end
2121

22+
def column_name
23+
@column_name ||= options[:data]&.to_sym
24+
end
25+
2226
def data
2327
options[:data].presence || options[:name]
2428
end
@@ -87,9 +91,15 @@ def casted_column
8791
end
8892

8993
def validate_settings!
94+
raise AjaxDatatablesRails::Error::InvalidSearchColumn, "Unknown column. Check that `data` field is filled on JS side with the column name" if column_name.empty?
95+
raise AjaxDatatablesRails::Error::InvalidSearchColumn, "Check that column '#{column_name}' exists in view_columns" unless valid_search_column?(column_name)
9096
raise AjaxDatatablesRails::Error::InvalidSearchCondition, cond unless valid_search_condition?(cond)
9197
end
9298

99+
def valid_search_column?(column_name)
100+
!datatable.view_columns[column_name].nil?
101+
end
102+
93103
VALID_SEARCH_CONDITIONS = [
94104
# String condition
95105
:start_with, :end_with, :like, :string_eq, :string_in, :null_value,

lib/ajax-datatables-rails/error.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module AjaxDatatablesRails
44
module Error
55
class BaseError < StandardError; end
6+
class InvalidSearchColumn < BaseError; end
67
class InvalidSearchCondition < BaseError; end
78
end
89
end

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,24 @@
202202
expect(column.send(:type_cast)).to eq('VARCHAR(4000)')
203203
end
204204
end
205+
206+
describe 'when empty column' do
207+
before do
208+
datatable.params[:columns] = {'0'=>{'data'=>'', 'name'=>'', 'searchable'=>'true', 'orderable'=>'true', 'search'=>{'value'=>'searchvalue', 'regex'=>'false'}}}
209+
end
210+
211+
it 'raises error' do
212+
expect { datatable.to_json }.to raise_error(AjaxDatatablesRails::Error::InvalidSearchColumn).with_message("Unknown column. Check that `data` field is filled on JS side with the column name")
213+
end
214+
end
215+
216+
describe 'when unknown column' do
217+
before do
218+
datatable.params[:columns] = {'0'=>{'data'=>'foo', 'name'=>'', 'searchable'=>'true', 'orderable'=>'true', 'search'=>{'value'=>'searchvalue', 'regex'=>'false'}}}
219+
end
220+
221+
it 'raises error' do
222+
expect { datatable.to_json }.to raise_error(AjaxDatatablesRails::Error::InvalidSearchColumn).with_message("Check that column 'foo' exists in view_columns")
223+
end
224+
end
205225
end

0 commit comments

Comments
 (0)