Skip to content

Commit

Permalink
Add :string_eq condition. Fix #291
Browse files Browse the repository at this point in the history
  • Loading branch information
n-rodriguez committed Jun 4, 2018
1 parent 6a8e53d commit 5035718
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ end

`cond` can be :

* `:like`, `:start_with`, `:end_with` for string or full text search
* `:like`, `:start_with`, `:end_with`, `:string_eq` for string or full text search
* `:eq`, `:not_eq`, `:lt`, `:gt`, `:lteq`, `:gteq`, `:in` for numeric
* `:date_range` for date range (only for Rails > 4.2.x, see [here](#daterange-search))
* `:null_value` for nil field
Expand Down
6 changes: 4 additions & 2 deletions lib/ajax-datatables-rails/datatable/column/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def non_regex_search
when Proc
filter
when :eq, :not_eq, :lt, :gt, :lteq, :gteq, :in
is_searchable_integer? ? numeric_search : empty_search
is_searchable_integer? ? raw_search(cond) : empty_search
when :null_value
null_value_search
when :start_with
Expand All @@ -66,6 +66,8 @@ def non_regex_search
casted_column.matches("%#{formatted_value}")
when :like
casted_column.matches("%#{formatted_value}%")
when :string_eq
raw_search(:eq)
end
end

Expand All @@ -77,7 +79,7 @@ def null_value_search
end
end

def numeric_search
def raw_search(cond)
if custom_field?
::Arel::Nodes::SqlLiteral.new(field).eq(formatted_value)
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,38 @@
end
end

describe 'it can filter records with condition :like' do
let(:datatable) { DatatableCondLike.new(view) }

before(:each) do
create(:user, email: 'john@foo.com')
create(:user, email: 'mary@bar.com')
end

it 'should filter records matching' do
datatable.params[:columns]['1'][:search][:value] = 'foo'
expect(datatable.data.size).to eq 1
item = datatable.data.first
expect(item[:email]).to eq 'john@foo.com'
end
end

describe 'it can filter records with condition :string_eq' do
let(:datatable) { DatatableCondStringEq.new(view) }

before(:each) do
create(:user, email: 'john@foo.com')
create(:user, email: 'mary@bar.com')
end

it 'should filter records matching' do
datatable.params[:columns]['1'][:search][:value] = 'john@foo.com'
expect(datatable.data.size).to eq 1
item = datatable.data.first
expect(item[:email]).to eq 'john@foo.com'
end
end

describe 'it can filter records with condition :null_value' do
let(:datatable) { DatatableCondNullValue.new(view) }

Expand Down
12 changes: 12 additions & 0 deletions spec/support/datatable_cond_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ def view_columns
end
end

class DatatableCondLike < ComplexDatatable
def view_columns
super.deep_merge(email: { cond: :like })
end
end

class DatatableCondStringEq < ComplexDatatable
def view_columns
super.deep_merge(email: { cond: :string_eq })
end
end

class DatatableCondNullValue < ComplexDatatable
def view_columns
super.deep_merge(email: { cond: :null_value })
Expand Down

0 comments on commit 5035718

Please # to comment.