Skip to content

Commit

Permalink
Adds typecast clause to convert all columns to text so we can search.
Browse files Browse the repository at this point in the history
People have been reporting that whenever you included a non-text column
in the `searchable_columns` array, the functionality broke given it
couldn't perform a text ILIKE search on non these fields.

This commit fixes this, by doing a typecast on all fields declared
inside the `searchable_columns` array.

Also, we have appended `if` clauses to allow access to `JSON` views
in the browser for debugging purposes.
  • Loading branch information
antillas21 committed Aug 31, 2014
1 parent 240e8fb commit 8d7d164
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/ajax-datatables-rails/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ def build_conditions_for(query)
def search_condition(column, value)
model, column = column.split('.')
model = model.singularize.titleize.gsub( / /, '' ).constantize
model.arel_table[column.to_sym].matches("%#{value}%")
casted_column = Arel::Nodes::NamedFunction.new('CAST', [model.arel_table[column.to_sym].as('CHAR')])
casted_column.matches("%#{value}%")
end

def aggregate_query
conditions = searchable_columns.each_with_index.map do |column, index|
value = params[:columns]["#{index}"][:search][:value]
value = params[:columns]["#{index}"][:search][:value] if params[:columns]
search_condition(column, value) unless value.blank?
end
conditions.compact.reduce(:and)
Expand All @@ -117,12 +118,12 @@ def per_page
end

def sort_column
sortable_columns[params[:order]['0'][:column].to_i]
sortable_columns[params[:order]['0'][:column].to_i] if params[:order]
end

def sort_direction
options = %w(desc asc)
options.include?(params[:order]['0'][:dir]) ? params[:order]['0'][:dir].upcase : 'ASC'
options.include?(params[:order]['0'][:dir]) ? params[:order]['0'][:dir].upcase : 'ASC' if params[:order]
end
end
end

0 comments on commit 8d7d164

Please sign in to comment.