diff options
| author | Miquel Sabaté Solà <msabate@suse.com> | 2024-03-15 10:38:05 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <msabate@suse.com> | 2024-03-15 10:38:05 +0100 |
| commit | 002371920d6f9acedaf987f273bb138a95353ffd (patch) | |
| tree | 40a99ed7db2814504d056ee535460f2b0a301d21 | |
| parent | e46f849f1f8b3e2a002ac3b124331011a86f9ea3 (diff) | |
| download | operum-002371920d6f9acedaf987f273bb138a95353ffd.tar.gz operum-002371920d6f9acedaf987f273bb138a95353ffd.zip | |
Fixed search for tags with spaces
We should not split the body mindlessly, but we have to be more careful
about it.
Moreover, this commit also polishes the order for tags inside of
comments.
Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
| -rw-r--r-- | app/models/search.rb | 27 | ||||
| -rw-r--r-- | app/views/tags/_taggable.html.erb | 2 | ||||
| -rw-r--r-- | test/fixtures/tags.yml | 2 | ||||
| -rw-r--r-- | test/system/comments_test.rb | 4 |
4 files changed, 30 insertions, 5 deletions
diff --git a/app/models/search.rb b/app/models/search.rb index 7823a74..053e641 100644 --- a/app/models/search.rb +++ b/app/models/search.rb @@ -31,7 +31,7 @@ class Search < ApplicationRecord def parse_body res = { tag: [], plain: [] } - body.split.each do |part| + split_body.each do |part| if part.include?(':') parts = part.split(':', 2) parts[1] = clean_clause(part: parts[1]) @@ -47,6 +47,31 @@ class Search < ApplicationRecord res end + # Returns the body split into its constituents. Note that we cannot mindlessly + # do something like `body.split` because that would ignore quotes that are not + # to be split among other things. + def split_body + cur = '' + res = [] + co = '' + + body.each_char do |c| + if c.match?(/\s/) && co == '' + res << cur if cur.present? + cur = '' + else + # If it is a quote character, check whether it's being opened or + # closed. + co = c == co ? '' : c if c == '"' || c == "'" + + cur += c + end + end + + res << cur if cur.present? && co.blank? + res + end + # Returns the argument without any leading/trailing quotes. def clean_clause(part:) part.gsub(/^("|')+/, '').gsub(/("|')+$/, '') diff --git a/app/views/tags/_taggable.html.erb b/app/views/tags/_taggable.html.erb index 84e236b..5d848d0 100644 --- a/app/views/tags/_taggable.html.erb +++ b/app/views/tags/_taggable.html.erb @@ -3,7 +3,7 @@ <h5><%= I18n.t('tags.title') %> — <%= link_to I18n.t('tags.new-action'), new_tag_path %></h5> <div class="flex-list"> - <% Tag.find_each do |tag| %> + <% Tag.order(:name).find_each do |tag| %> <div class="flex-element"> <label> <%= form.check_box :tag_ids, { multiple: true, checked: tag_references&.include?(tag.id) }, tag.id, nil %> diff --git a/test/fixtures/tags.yml b/test/fixtures/tags.yml index 1e0ce52..fcfcfd7 100644 --- a/test/fixtures/tags.yml +++ b/test/fixtures/tags.yml @@ -2,4 +2,4 @@ tag1: name: 'tag1' tag2: - name: 'tag2' + name: 'Compound tag' diff --git a/test/system/comments_test.rb b/test/system/comments_test.rb index 33a1398..eb53412 100644 --- a/test/system/comments_test.rb +++ b/test/system/comments_test.rb @@ -31,7 +31,7 @@ class CommentsTest < ApplicationSystemTestCase click_on I18n.t('helpers.submit.create') assert_text 'a new comment' - assert_text "#{I18n.t('tags.title')}: #{tags(:tag1).name}, #{tags(:tag2).name}" + assert_text "#{I18n.t('tags.title')}: #{tags(:tag2).name}, #{tags(:tag1).name}" assert_equal page.current_path, thing_path(things(:thing1)) end @@ -82,7 +82,7 @@ class CommentsTest < ApplicationSystemTestCase click_on I18n.t('helpers.submit.create') assert_text 'a new comment' - assert_text "#{I18n.t('tags.title')}: #{tags(:tag1).name}, #{tags(:tag2).name}" + assert_text "#{I18n.t('tags.title')}: #{tags(:tag2).name}, #{tags(:tag1).name}" assert_equal page.current_path, edit_thing_path(things(:thing1)) end |
