-
Notifications
You must be signed in to change notification settings - Fork 22
Make workshop text field searches ignore punctuation (hyphens, ampersands, periods, dashes, quotes) #737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Make workshop text field searches ignore punctuation (hyphens, ampersands, periods, dashes, quotes) #737
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
75e71ab
WIP: punctuation agnostic
maebeale 64cbab0
update claude
maebeale 344cc72
Strip puncutation from searches and add index to bookmarks to improve…
maebeale 2bf1425
Set up claude to autorun rubocop
maebeale 771d9a3
Update workshop searching to be punctuation agnostic
maebeale 9ec7e3f
Add extra searching to find more variations using the word and
maebeale 11e5981
Correctly look for person name if user when doing author search
maebeale d35fdff
Avoid brakeman error
maebeale f836a79
Claude can check brakeman
maebeale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,14 @@ | ||
| #!/bin/bash | ||
| INPUT=$(cat) | ||
| FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty') | ||
| if [[ ! "$FILE_PATH" =~ \.rb$|\.erb$ ]]; then | ||
| exit 0 | ||
| fi | ||
| # Get path relative to project root | ||
| REL_PATH="${FILE_PATH#$CLAUDE_PROJECT_DIR/}" | ||
| OUTPUT=$(bundle exec brakeman --only-files "$REL_PATH" --no-pager --format text --quiet --no-summary 2>/dev/null) | ||
| if [ -n "$OUTPUT" ] && ! echo "$OUTPUT" | grep -q "No warnings found"; then | ||
| echo "Brakeman warnings in $REL_PATH:" >&2 | ||
| echo "$OUTPUT" >&2 | ||
| fi | ||
| exit 0 |
This file contains hidden or 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,8 @@ | ||
| #!/bin/bash | ||
| INPUT=$(cat) | ||
| FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty') | ||
| if [[ ! "$FILE_PATH" =~ \.rb$ ]]; then | ||
| exit 0 | ||
| fi | ||
| bundle exec rubocop -A --format quiet "$FILE_PATH" 2>/dev/null | ||
| exit 0 |
This file contains hidden or 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,19 @@ | ||
| { | ||
| "hooks": { | ||
| "PostToolUse": [ | ||
| { | ||
| "matcher": "Edit|Write", | ||
| "hooks": [ | ||
| { | ||
| "type": "command", | ||
| "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/rubocop-autofix.sh" | ||
| }, | ||
| { | ||
| "type": "command", | ||
| "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/brakeman-check.sh" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,72 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module PunctuationStrippable | ||
| extend ActiveSupport::Concern | ||
|
|
||
| PUNCTUATION_CHARS_SQL = [ | ||
| "'-'", # hyphen | ||
| "'&'", # ampersand | ||
| "'.'", # period | ||
| "0xE28094", # em dash — | ||
| "0xE28093", # en dash – | ||
| "'\"'", # double quote " | ||
| "\"'\"", # single quote ' | ||
| "0xE28098", # left single curly quote ' | ||
| "0xE28099", # right single curly quote ' | ||
| "0xE2809C", # left double curly quote " | ||
| "0xE2809D", # right double curly quote " | ||
| "'/'", # slash | ||
| "':'", # colon | ||
| "'+'", # plus | ||
| "'!'", # exclamation | ||
| "CHAR(63)", # question mark ? | ||
| "','", # comma | ||
| "'('", # open paren | ||
| "')'", # close paren | ||
| "0xE280A6" # ellipsis … | ||
| ].freeze | ||
|
|
||
| PUNCTUATION_REGEX = /[-&.—–'"''""\/:+!?,()…]/ | ||
|
|
||
| module ClassMethods | ||
| # "spaced" — punctuation → space, collapse multiple spaces. | ||
| # Matches "self care" to "self-care". | ||
| def strip_punctuation_sql_spaced(field_name) | ||
| result = normalize_synonyms_sql(field_name) | ||
| PUNCTUATION_CHARS_SQL.each { |c| result = "REPLACE(#{result}, #{c}, ' ')" } | ||
| 3.times { result = "REPLACE(#{result}, ' ', ' ')" } | ||
| result | ||
| end | ||
|
|
||
| # "spaceless" — punctuation AND spaces → removed entirely. | ||
| # Matches "selfcare" to "self-care" and to "self care". | ||
| def strip_punctuation_sql_spaceless(field_name) | ||
| result = normalize_synonyms_sql(field_name) | ||
| PUNCTUATION_CHARS_SQL.each { |c| result = "REPLACE(#{result}, #{c}, '')" } | ||
| result = "REPLACE(#{result}, ' ', '')" | ||
| result | ||
| end | ||
|
|
||
| def strip_punctuation_spaced(text) | ||
| normalize_synonyms(text).gsub(PUNCTUATION_REGEX, " ").gsub(/\s+/, " ") | ||
| end | ||
|
|
||
| def strip_punctuation_spaceless(text) | ||
| normalize_synonyms(text).gsub(PUNCTUATION_REGEX, "").gsub(/\s+/, "") | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Normalize synonyms so both sides strip the same way. | ||
| # " and " → " & " (then & gets stripped as punctuation) | ||
| # "w/" → "with" (then / gets stripped, leaving "with" on both sides) | ||
| def normalize_synonyms_sql(field_name) | ||
| result = "REPLACE(#{field_name}, ' and ', ' & ')" | ||
| "REPLACE(#{result}, 'w/', 'with')" | ||
| end | ||
|
|
||
| def normalize_synonyms(text) | ||
| text.to_s.gsub(/ and /i, " & ").gsub(/w\//i, "with") | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| class WorkshopSearchService | ||
| include ActionPolicy::Behaviour | ||
| include PunctuationStrippable | ||
| authorize :user | ||
|
|
||
| attr_reader :params, :user, :admin | ||
|
|
@@ -127,22 +128,34 @@ def filter_by_tag_names | |
|
|
||
| def filter_by_title | ||
| return unless params[:title].present? | ||
| @workshops = @workshops.search("title:#{params[:title]}") | ||
| @workshops = @workshops.title(params[:title]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stop using SearchCop for title search so we can instead apply this stripped chars version of searching. |
||
| end | ||
|
|
||
| def filter_by_query | ||
| return unless params[:query].present? | ||
|
|
||
| results = @workshops.search(params[:query]) # Use the SearchCop search scope directly on the relation | ||
| spaced = ActiveRecord::Base.sanitize_sql_like( | ||
| self.class.strip_punctuation_spaced(params[:query]).strip | ||
| ) | ||
| spaceless = ActiveRecord::Base.sanitize_sql_like( | ||
| self.class.strip_punctuation_spaceless(params[:query]).strip | ||
| ) | ||
| return if spaced.blank? | ||
|
|
||
| # Match against spaced variant (punctuation → space) and spaceless variant (punctuation + spaces removed) | ||
| fields = %w[workshops.title workshops.full_name action_text_rich_texts.plain_text_body] | ||
| conditions = fields.flat_map do |field| | ||
| [ | ||
| "#{self.class.strip_punctuation_sql_spaced(field)} LIKE :spaced", | ||
| "#{self.class.strip_punctuation_sql_spaceless(field)} LIKE :spaceless" | ||
| ] | ||
| end.join(" OR ") | ||
|
|
||
| # If SearchCop returned an Array (e.g., because of scoring), convert back to Relation | ||
| if results.is_a?(Array) | ||
| ordered_ids = results.map(&:id) | ||
| @workshops = Workshop.where(id: ordered_ids) | ||
| .order(Arel.sql("FIELD(id, #{ordered_ids.join(',')})")) | ||
| else | ||
| @workshops = results | ||
| end | ||
| @workshops = @workshops | ||
| .joins("LEFT JOIN action_text_rich_texts ON action_text_rich_texts.record_id = workshops.id " \ | ||
| "AND action_text_rich_texts.record_type = 'Workshop'") | ||
| .where(conditions, spaced: "%#{spaced}%", spaceless: "%#{spaceless}%") | ||
| .distinct | ||
| end | ||
|
|
||
| # --- Search methods --- | ||
|
|
@@ -151,13 +164,17 @@ def search_by_author_name(workshops, author_name) | |
| return workshops if author_name.blank? | ||
|
|
||
| sanitized = author_name.strip.gsub(/\s+/, "") | ||
| workshops.left_outer_joins(:user) | ||
| workshops.left_outer_joins(user: :person) | ||
| .where( | ||
| "LOWER(REPLACE(workshops.full_name, ' ', '')) LIKE :name | ||
| OR LOWER(REPLACE(CONCAT(users.first_name, users.last_name), ' ', '')) LIKE :name | ||
| OR LOWER(REPLACE(CONCAT(users.last_name, users.first_name), ' ', '')) LIKE :name | ||
| OR LOWER(REPLACE(users.first_name, ' ', '')) LIKE :name | ||
| OR LOWER(REPLACE(users.last_name, ' ', '')) LIKE :name", | ||
| OR LOWER(REPLACE(users.last_name, ' ', '')) LIKE :name | ||
| OR LOWER(REPLACE(CONCAT(people.first_name, people.last_name), ' ', '')) LIKE :name | ||
| OR LOWER(REPLACE(CONCAT(people.last_name, people.first_name), ' ', '')) LIKE :name | ||
| OR LOWER(REPLACE(people.first_name, ' ', '')) LIKE :name | ||
| OR LOWER(REPLACE(people.last_name, ' ', '')) LIKE :name", | ||
| name: "%#{sanitized}%" | ||
| ) | ||
| end | ||
|
|
||
6 changes: 6 additions & 0 deletions
6
db/migrate/20260215122713_add_polymorphic_index_to_bookmarks.rb
This file contains hidden or 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,6 @@ | ||
| class AddPolymorphicIndexToBookmarks < ActiveRecord::Migration[7.2] | ||
| def change | ||
| add_index :bookmarks, [ :bookmarkable_type, :bookmarkable_id ], | ||
| name: "index_bookmarks_on_bookmarkable" | ||
| end | ||
| end |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove dupe