Skip to content

Commit

Permalink
We should use formatted_value when checking for searchable integers
Browse files Browse the repository at this point in the history
  • Loading branch information
n-rodriguez committed May 15, 2018
1 parent e6fb8f2 commit 511e0ac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/ajax-datatables-rails/datatable/column/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ def empty_search
end

def is_searchable_integer?
if search.value.is_a?(Array)
valids = search.value.map { |v| is_integer?(v) && !is_out_of_range?(v) }
if formated_value.is_a?(Array)
valids = formated_value.map { |v| is_integer?(v) && !is_out_of_range?(v) }
!valids.include?(false)
else
is_integer?(search.value) && !is_out_of_range?(search.value)
is_integer?(formated_value) && !is_out_of_range?(formated_value)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,23 @@
end
end

describe 'it can filter records with condition :in with regex' do
let(:datatable) { DatatableCondInWithRegex.new(view) }

before(:each) do
create(:user, first_name: 'john', post_id: 1)
create(:user, first_name: 'mary', post_id: 2)
end

it 'should filter records matching' do
datatable.params[:columns]['4'][:search][:value] = '1|2'
datatable.params[:order]['0'] = { column: '4', dir: 'asc' }
expect(datatable.data.size).to eq 2
item = datatable.data.first
expect(item[:first_name]).to eq 'john'
end
end

describe 'Integer overflows' do
let(:datatable) { DatatableCondEq.new(view) }
let(:largest_postgresql_integer_value) { 2147483647 }
Expand Down
10 changes: 10 additions & 0 deletions spec/support/datatable_cond_numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ def view_columns
super.deep_merge(post_id: { cond: :in })
end
end

class DatatableCondInWithRegex < DatatableCondIn
def view_columns
super.deep_merge(post_id: { cond: :in, use_regex: false, orderable: true, formater: ->(str) { cast_regex_value(str) } })
end

def cast_regex_value(value)
value.split('|').map(&:to_i)
end
end

0 comments on commit 511e0ac

Please sign in to comment.