module AjaxDatatablesRails::Datatable::Column::Search
Constants
- EMPTY_VALUE
- LARGEST_PQ_INTEGER
- NOT_NULL_VALUE
- SMALLEST_PQ_INTEGER
Public Instance Methods
cond()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 17 def cond @view_column.fetch(:cond, :like) end
filter()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 21 def filter @view_column[:cond].call(self, formatted_value) end
search()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 25 def search @search ||= SimpleSearch.new(options[:search]) end
search_query()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 33 def search_query search.regexp? ? regex_search : non_regex_search end
searchable?()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 13 def searchable? @view_column.fetch(:searchable, true) end
searched?()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 29 def searched? search.value.present? end
use_regex?()
click to toggle source
Add use_regex option to allow bypassing of regex search
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 38 def use_regex? @view_column.fetch(:use_regex, true) end
Private Instance Methods
empty_search()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 98 def empty_search casted_column.matches(EMPTY_VALUE) end
integer?(string)
click to toggle source
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 115 def integer?(string) Integer(string) true rescue ArgumentError false end
non_regex_search()
click to toggle source
rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 58 def non_regex_search case cond when Proc filter when :eq, :not_eq, :lt, :gt, :lteq, :gteq, :in searchable_integer? ? raw_search(cond) : empty_search when :null_value null_value_search when :start_with casted_column.matches("#{formatted_value}%") when :end_with casted_column.matches("%#{formatted_value}") when :like casted_column.matches("%#{formatted_value}%") when :string_eq raw_search(:eq) when :string_in raw_search(:in) when :date_range date_range_search end end
null_value_search()
click to toggle source
rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 82 def null_value_search if formatted_value == NOT_NULL_VALUE table[field].not_eq(nil) else table[field].eq(nil) end end
out_of_range?(search_value)
click to toggle source
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 111 def out_of_range?(search_value) Integer(search_value) > LARGEST_PQ_INTEGER || Integer(search_value) < SMALLEST_PQ_INTEGER end
raw_search(cond)
click to toggle source
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 90 def raw_search(cond) if custom_field? ::Arel::Nodes::SqlLiteral.new(field).eq(formatted_value) else table[field].send(cond, formatted_value) end end
regex_search()
click to toggle source
Using multi-select filters in JQuery Datatable
auto-enables regex_search. Unfortunately regex_search
doesn't work when filtering on primary keys with integer. It generates this kind of query : AND (“regions”.“id” ~ '2|3') which throws an error : operator doesn't exist : integer ~ unknown The solution is to bypass regex_search
and use non_regex_search
with :in operator
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 49 def regex_search if use_regex? ::Arel::Nodes::Regexp.new((custom_field? ? field : table[field]), ::Arel::Nodes.build_quoted(formatted_value)) else non_regex_search end end
searchable_integer?()
click to toggle source
# File lib/ajax-datatables-rails/datatable/column/search.rb, line 102 def searchable_integer? if formatted_value.is_a?(Array) valids = formatted_value.map { |v| integer?(v) && !out_of_range?(v) } !valids.include?(false) else integer?(formatted_value) && !out_of_range?(formatted_value) end end