Skip to content

Commit

Permalink
add not like operator (does not contain)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quent1Pr committed Nov 17, 2021
1 parent b4950d0 commit 09e29bb
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 3 deletions.
7 changes: 7 additions & 0 deletions app/assets/javascripts/rails_admin/ra.filter-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@
.prop("selected", field_operator == "like")
.text(RailsAdmin.I18n.t("contains"))
)
.append(
$(
'<option data-additional-fieldset="additional-fieldset" value="not_like"></option>'
)
.prop("selected", field_operator == "not_like")
.text(RailsAdmin.I18n.t("does_not_contain"))
)
.append(
$(
'<option data-additional-fieldset="additional-fieldset" value="is"></option>'
Expand Down
1 change: 1 addition & 0 deletions config/locales/rails_admin.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ en:
time: Time ...
number: Number ...
contains: Contains
does_not_contain: Does not contain
is_exactly: Is exactly
starts_with: Starts with
ends_with: Ends with
Expand Down
10 changes: 8 additions & 2 deletions lib/rails_admin/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def build_statement_for_string_or_text

@value = begin
case @operator
when 'default', 'like'
when 'default', 'like', 'not_like'
"%#{@value}%"
when 'starts_with'
"#{@value}%"
Expand All @@ -264,7 +264,13 @@ def build_statement_for_string_or_text
end

if ['postgresql', 'postgis'].include? ar_adapter
["(#{@column} ILIKE ?)", @value]
if @operator == 'not_like'
["(#{@column} NOT ILIKE ?)", @value]
else
["(#{@column} ILIKE ?)", @value]
end
elsif @operator == 'not_like'
["(LOWER(#{@column}) NOT LIKE ?)", @value]
else
["(LOWER(#{@column}) LIKE ?)", @value]
end
Expand Down
2 changes: 2 additions & 0 deletions lib/rails_admin/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ def build_statement_for_string_or_text
return if @value.blank?
@value = begin
case @operator
when 'not_like'
Regexp.compile("^((?!#{Regexp.escape(@value)}).)*$", Regexp::IGNORECASE)
when 'default', 'like'
Regexp.compile(Regexp.escape(@value), Regexp::IGNORECASE)
when 'starts_with'
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def current_user_method(&block)
end

def default_search_operator=(operator)
if %w(default like starts_with ends_with is =).include? operator
if %w(default like not_like starts_with ends_with is =).include? operator
@default_search_operator = operator
else
raise(ArgumentError.new("Search operator '#{operator}' not supported"))
Expand Down
8 changes: 8 additions & 0 deletions spec/rails_admin/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
'(LOWER(field) LIKE ?)'
end
end
let(:not_like) do
if ['postgresql', 'postgis'].include? activerecord_config[:adapter]
'(field NOT ILIKE ?)'
else
'(LOWER(field) NOT LIKE ?)'
end
end

def predicates_for(scope)
scope.where_clause.instance_variable_get(:@predicates)
Expand Down Expand Up @@ -274,6 +281,7 @@ def build_statement(type, value, operator)
expect(build_statement(:string, 'foo', 'was')).to be_nil
expect(build_statement(:string, 'foo', 'default')).to eq([like, '%foo%'])
expect(build_statement(:string, 'foo', 'like')).to eq([like, '%foo%'])
expect(build_statement(:string, 'foo', 'not_like')).to eq([not_like, '%foo%'])
expect(build_statement(:string, 'foo', 'starts_with')).to eq([like, 'foo%'])
expect(build_statement(:string, 'foo', 'ends_with')).to eq([like, '%foo'])
expect(build_statement(:string, 'foo', 'is')).to eq(['(field = ?)', 'foo'])
Expand Down
1 change: 1 addition & 0 deletions spec/rails_admin/adapters/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def parse_value(value)
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'was')).to be_nil
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'default')).to eq(field: /foo/i)
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'like')).to eq(field: /foo/i)
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'not_like')).to eq(field: /^((?!foo).)*$/i)
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'starts_with')).to eq(field: /^foo/i)
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'ends_with')).to eq(field: /foo$/i)
expect(@abstract_model.send(:build_statement, :field, :string, 'foo', 'is')).to eq(field: 'foo')
Expand Down

0 comments on commit 09e29bb

Please sign in to comment.