Skip to content

Commit b79f3bf

Browse files
committed
Add tests on custom_field feature
1 parent dd53c05 commit b79f3bf

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ def table
3636
end
3737

3838
def model
39-
@model ||= source.split('.').first.constantize
39+
@model ||= custom_field? ? source : source.split('.').first.constantize
4040
end
4141

4242
def field
43-
@field ||= source.split('.').last.to_sym
43+
@field ||= custom_field? ? source : source.split('.').last.to_sym
4444
end
4545

4646
def custom_field?

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,11 @@ def null_value_search
8888
end
8989

9090
def raw_search(cond)
91-
if custom_field?
92-
::Arel::Nodes::SqlLiteral.new(field).eq(formatted_value)
93-
else
94-
table[field].send(cond, formatted_value)
95-
end
91+
table[field].send(cond, formatted_value) unless custom_field?
9692
end
9793

9894
def text_search(value)
99-
casted_column.matches(value)
95+
casted_column.matches(value) unless custom_field?
10096
end
10197

10298
def empty_search

spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb

+24-2
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,12 @@
410410
create(:user, first_name: 'phil', post_id: largest_postgresql_integer_value)
411411
end
412412

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

418-
it 'Returns an empty result if input value is too small' do
418+
it 'returns an empty result if input value is too small' do
419419
datatable.params[:columns]['5'][:search][:value] = smallest_postgresql_integer_value - 1
420420
expect(datatable.data.size).to eq 0
421421
end
@@ -597,6 +597,28 @@
597597
}.to raise_error(AjaxDatatablesRails::Error::InvalidSearchCondition).with_message('foo')
598598
end
599599
end
600+
601+
context 'custom column' do
602+
describe 'it can filter records with custom column' do
603+
let(:datatable) { DatatableCustomColumn.new(sample_params) }
604+
605+
before do
606+
create(:user, username: 'msmith', email: '[email protected]', first_name: 'Mary', last_name: 'Smith')
607+
create(:user, username: 'jsmith', email: '[email protected]', first_name: 'John', last_name: 'Smith')
608+
create(:user, username: 'johndoe', email: '[email protected]', first_name: 'John', last_name: 'Doe')
609+
end
610+
611+
it 'filters records' do
612+
skip('unsupported database adapter') if RunningSpec.oracle? || RunningSpec.sqlite?
613+
614+
datatable.params[:columns]['4'][:search][:value] = 'John'
615+
datatable.params[:order]['0'][:column] = '4'
616+
expect(datatable.data.size).to eq 2
617+
item = datatable.data.first
618+
expect(item[:full_name]).to eq 'John Doe'
619+
end
620+
end
621+
end
600622
end
601623

602624
describe 'formatter option' do
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
class DatatableCustomColumn < ComplexDatatable
4+
def view_columns
5+
super.deep_merge(full_name: { cond: filter_full_name })
6+
end
7+
8+
def get_raw_records
9+
User.select("*, CONCAT(first_name, ' ', last_name) as full_name")
10+
end
11+
12+
private
13+
14+
def filter_full_name
15+
->(_column, value) { ::Arel::Nodes::SqlLiteral.new("CONCAT(first_name, ' ', last_name)").matches("#{value}%") }
16+
end
17+
end

0 commit comments

Comments
 (0)