Skip to content

Commit

Permalink
Add tests on custom_field feature
Browse files Browse the repository at this point in the history
  • Loading branch information
n-rodriguez committed Feb 9, 2021
1 parent dd53c05 commit b79f3bf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/ajax-datatables-rails/datatable/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def table
end

def model
@model ||= source.split('.').first.constantize
@model ||= custom_field? ? source : source.split('.').first.constantize
end

def field
@field ||= source.split('.').last.to_sym
@field ||= custom_field? ? source : source.split('.').last.to_sym
end

def custom_field?
Expand Down
8 changes: 2 additions & 6 deletions lib/ajax-datatables-rails/datatable/column/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,11 @@ def null_value_search
end

def raw_search(cond)
if custom_field?
::Arel::Nodes::SqlLiteral.new(field).eq(formatted_value)
else
table[field].send(cond, formatted_value)
end
table[field].send(cond, formatted_value) unless custom_field?
end

def text_search(value)
casted_column.matches(value)
casted_column.matches(value) unless custom_field?
end

def empty_search
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@
create(:user, first_name: 'phil', post_id: largest_postgresql_integer_value)
end

it 'Returns an empty result if input value is too large' do
it 'returns an empty result if input value is too large' do
datatable.params[:columns]['5'][:search][:value] = largest_postgresql_integer_value + 1
expect(datatable.data.size).to eq 0
end

it 'Returns an empty result if input value is too small' do
it 'returns an empty result if input value is too small' do
datatable.params[:columns]['5'][:search][:value] = smallest_postgresql_integer_value - 1
expect(datatable.data.size).to eq 0
end
Expand Down Expand Up @@ -597,6 +597,28 @@
}.to raise_error(AjaxDatatablesRails::Error::InvalidSearchCondition).with_message('foo')
end
end

context 'custom column' do
describe 'it can filter records with custom column' do
let(:datatable) { DatatableCustomColumn.new(sample_params) }

before do
create(:user, username: 'msmith', email: '[email protected]', first_name: 'Mary', last_name: 'Smith')
create(:user, username: 'jsmith', email: '[email protected]', first_name: 'John', last_name: 'Smith')
create(:user, username: 'johndoe', email: '[email protected]', first_name: 'John', last_name: 'Doe')
end

it 'filters records' do
skip('unsupported database adapter') if RunningSpec.oracle? || RunningSpec.sqlite?

datatable.params[:columns]['4'][:search][:value] = 'John'
datatable.params[:order]['0'][:column] = '4'
expect(datatable.data.size).to eq 2
item = datatable.data.first
expect(item[:full_name]).to eq 'John Doe'
end
end
end
end

describe 'formatter option' do
Expand Down
17 changes: 17 additions & 0 deletions spec/support/datatables/datatable_custom_column.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class DatatableCustomColumn < ComplexDatatable
def view_columns
super.deep_merge(full_name: { cond: filter_full_name })
end

def get_raw_records
User.select("*, CONCAT(first_name, ' ', last_name) as full_name")
end

private

def filter_full_name
->(_column, value) { ::Arel::Nodes::SqlLiteral.new("CONCAT(first_name, ' ', last_name)").matches("#{value}%") }
end
end

0 comments on commit b79f3bf

Please sign in to comment.