Skip to content

Commit

Permalink
Allow searching from a given tag
Browse files Browse the repository at this point in the history
This feature is useful if you want to have a quick search on how many
resources apply for a single tag.

Signed-off-by: Miquel Sabaté Solà <[email protected]>
  • Loading branch information
mssola committed Nov 11, 2024
1 parent 2a4fedb commit c0befbd
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ GEM
mini_magick (4.13.2)
mini_mime (1.1.5)
minitest (5.25.1)
msgpack (1.7.3)
net-imap (0.5.0)
msgpack (1.7.4)
net-imap (0.5.1)
date
net-protocol
net-protocol (0.2.2)
Expand Down Expand Up @@ -242,7 +242,7 @@ GEM
rubocop-ast (>= 1.32.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.34.1)
rubocop-ast (1.35.0)
parser (>= 3.3.1.0)
rubocop-minitest (0.36.0)
rubocop (>= 1.61, < 2.0)
Expand All @@ -260,7 +260,7 @@ GEM
ffi (~> 1.12)
logger
rubyzip (2.3.2)
securerandom (0.3.1)
securerandom (0.3.2)
selenium-webdriver (4.26.0)
base64 (~> 0.2)
logger (~> 1.4)
Expand Down
15 changes: 12 additions & 3 deletions app/assets/stylesheets/tags.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@
.tag-container {
display: flex;
align-items: flex-start;
}

.tag-container .name {
flex-grow: 1;
.name {
flex-grow: 1;
}

.name a {
text-decoration: none;
color: var(--text) !important;
}

.name a:hover {
text-decoration: underline;
}
}

.tag-container .action {
Expand Down
10 changes: 8 additions & 2 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class TagsController < ApplicationController
before_action :set_tag, only: %i[edit update destroy]
before_action :set_tag, only: %i[edit update destroy search]

def index
@tags = Tag.order(:name)
Expand Down Expand Up @@ -57,10 +57,16 @@ def destroy
redirect_to tags_url, notice: t('tags.destroy-success')
end

def search
@search = Search.new(name: 'temporary', body: "tag:\"#{@tag.name}\"")

render 'searches/show'
end

private

def set_tag
@tag = Tag.find(params[:id])
@tag = Tag.find(params[:id] || params[:tag_id])
end

def tag_params
Expand Down
2 changes: 1 addition & 1 deletion app/views/searches/show.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= render "search", items: @search.results %>
<%= render "searches/search", items: @search.results %>
4 changes: 2 additions & 2 deletions app/views/tags/_tag.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="tag-container" id="<%= dom_id tag %>">
<div class="name">
<%= tag.name %>
<%= link_to tag.name, edit_tag_path(tag), title: I18n.t('general.edit').capitalize %>
</div>
<div class="action">
<%= link_to I18n.t('general.edit'), edit_tag_path(tag) %> |
<%= link_to I18n.t('searches.object'), tag_search_path(tag) %> |
<%= link_to I18n.t('general.delete'), tag_path(tag), data: { "turbo-method": :delete } %>
</div>
</div>
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
resource :exports, only: %i[new show]
end

resources :tags
resources :tags do
get 'search' => 'tags#search'
end

resources :things, except: %i[index] do
resources :comments, only: %i[create update destroy]
end
Expand Down
13 changes: 10 additions & 3 deletions test/system/tags_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'application_system_test_case'

class SharedSearchesTest < ApplicationSystemTestCase
class TagsTest < ApplicationSystemTestCase
setup { sign_in! }

test 'lists all the tags' do
Expand Down Expand Up @@ -34,7 +34,7 @@ class SharedSearchesTest < ApplicationSystemTestCase
test 'can visit the edit_tag path from #index' do
visit tags_url

click_link(I18n.t('general.edit'), match: :first)
click_link(Tag.first.name, match: :first)

assert_text I18n.t('tags.update')
end
Expand Down Expand Up @@ -73,11 +73,18 @@ class SharedSearchesTest < ApplicationSystemTestCase
test 'can delete an existing tag' do
visit tags_url

# TODO: flaky
assert_difference 'Tag.count', -1 do
click_link(I18n.t('general.delete'), match: :first)

assert_text I18n.t('tags.title')
end
end

test 'can search on a given tag' do
visit tags_url

click_link(I18n.t('searches.object'), match: :first)

assert_text Thing.find_by(target: 'target1').title
end
end

0 comments on commit c0befbd

Please sign in to comment.