-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd53c05
commit b79f3bf
Showing
4 changed files
with
45 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |