Fix case sensitivity in search fields and filter alias matching#68
Fix case sensitivity in search fields and filter alias matching#68
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds case-insensitive matching for search fields and filter alias names to improve the flexibility of the query helper. Previously, alias name matching was case-sensitive, which could cause issues when users provided filter or search field names with different casing than defined in the column maps.
Changes:
- Made filter alias matching case-insensitive in
create_filtersmethod - Made search field matching case-insensitive in
search_filtermethod
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| lib/query_helper/sql_filter.rb | Added case-insensitive comparison when finding column maps by alias name in filter creation |
| lib/query_helper.rb | Added case-insensitive comparison when matching search fields to column map aliases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
lib/query_helper/sql_filter.rb
Outdated
| @filter_values.each do |comparate_alias, criteria| | ||
| # Find the sql mapping if it exists | ||
| map = @column_maps.find { |m| m.alias_name == comparate_alias } | ||
| map = @column_maps.find { |m| m.alias_name.downcase == comparate_alias.downcase } |
There was a problem hiding this comment.
The case-insensitive comparison approach here is inconsistent with the codebase. The sql_sort.rb file uses the casecmp? method for case-insensitive alias matching (line 37 in sql_sort.rb), which is the idiomatic Ruby approach. Using casecmp? would be more consistent, more readable, and doesn't create intermediate string objects. Consider changing this to: map = @column_maps.find { |m| m.alias_name.casecmp?(comparate_alias) }
| map = @column_maps.find { |m| m.alias_name.downcase == comparate_alias.downcase } | |
| map = @column_maps.find { |m| m.alias_name.casecmp?(comparate_alias) } |
lib/query_helper.rb
Outdated
| placement = :where | ||
| maps = column_maps.select do |cm| | ||
| if @search_fields.include? cm.alias_name | ||
| if @search_fields.map(&:downcase).include? cm.alias_name.downcase |
There was a problem hiding this comment.
This implementation has a performance issue: the @search_fields.map(&:downcase) array is created on every iteration of the select block, once for each column_map. This should be computed once before the select block. Additionally, the case-insensitive comparison approach is inconsistent with the codebase - sql_sort.rb uses casecmp? for alias matching (line 37 in sql_sort.rb), which is the idiomatic Ruby approach. Consider refactoring to: downcased_search_fields = @search_fields.map(&:downcase) before the select, then use downcased_search_fields.include?(cm.alias_name.downcase), or better yet, use @search_fields.any? { |sf| sf.casecmp?(cm.alias_name) } for consistency with sql_sort.rb.
| if @search_fields.map(&:downcase).include? cm.alias_name.downcase | |
| if @search_fields.any? { |sf| sf.casecmp?(cm.alias_name) } |
Improvements to case-insensitivity in filtering and searching:
search_filtercompare alias names in a case-insensitive way by downcasing both the search fields and the column map alias names (lib/query_helper.rb).create_filtersto perform case-insensitive matching when finding column maps by alias name (lib/query_helper/sql_filter.rb).