Skip to content

Commit 3ed9123

Browse files
feat(operators): add i_contains operator support (#725)
1 parent 063992d commit 3ed9123

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

app/services/forest_liana/filters_parser.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def parse_condition_without_smart_field(condition)
9696
value = current_resource.defined_enums[association_field][value]
9797
end
9898

99-
parsed_field = parse_field_name(field_name)
99+
case_insensitive = operator == 'i_contains'
100+
parsed_field = parse_field_name(field_name, case_insensitive)
100101
parsed_operator = parse_operator(operator)
101102
parsed_value = parse_value(operator, value)
102103
field_and_operator = "#{parsed_field} #{parsed_operator}"
@@ -120,7 +121,7 @@ def parse_operator(operator)
120121
'>'
121122
when 'less_than', 'before'
122123
'<'
123-
when 'contains', 'starts_with', 'ends_with'
124+
when 'contains', 'starts_with', 'ends_with', 'i_contains'
124125
'LIKE'
125126
when 'not_contains'
126127
'NOT LIKE'
@@ -145,6 +146,8 @@ def parse_value(operator, value)
145146
value
146147
when 'contains', 'not_contains'
147148
"%#{value}%"
149+
when 'i_contains'
150+
"%#{value.downcase}%"
148151
when 'starts_with'
149152
"#{value}%"
150153
when 'ends_with'
@@ -161,7 +164,7 @@ def parse_value(operator, value)
161164
end
162165
end
163166

164-
def parse_field_name(field)
167+
def parse_field_name(field, insensitive = false)
165168
if is_belongs_to(field)
166169
current_resource = @resource.reflect_on_association(field.split(':').first.to_sym)&.klass
167170
raise ForestLiana::Errors::HTTP422Error.new("Field '#{field}' not found") unless current_resource
@@ -181,7 +184,7 @@ def parse_field_name(field)
181184
raise ForestLiana::Errors::HTTP422Error.new("Field '#{field}' not found")
182185
end
183186

184-
"#{quoted_table_name}.#{quoted_field_name}"
187+
insensitive ? "LOWER(#{quoted_table_name}.#{quoted_field_name})" : "#{quoted_table_name}.#{quoted_field_name}"
185188
end
186189

187190
def is_belongs_to(field)

spec/services/forest_liana/filters_parser_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ module ForestLiana
281281
it { expect(result).to eq "\"trees\".\"name\" LIKE '%3'" }
282282
end
283283

284+
context 'when the condition uses the i_contains operator' do
285+
let(:condition) { { 'field' => 'name', 'operator' => 'i_contains', 'value' => 'Tree' } }
286+
287+
it { expect(result).to eq "LOWER(\"trees\".\"name\") LIKE '%tree%'" }
288+
end
289+
284290
context 'when the condition uses the blank operator' do
285291
let(:condition) { { 'field' => 'name', 'operator' => 'blank' } }
286292

0 commit comments

Comments
 (0)