Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Check which tags are in use with one query on the tags page (#4688)

GFRa35f418210b4
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index a4b48a772d..99d808a09a 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -3,7 +3,8 @@ class TagsController < ApplicationController
before_action :set_tag, only: %i[edit update destroy]
def index
- @tags = Current.family.tags.alphabetically
+ @tags = Current.family.tags.alphabetically.to_a
+ @tag_ids_with_transactions = Tag.ids_with_transactions(family: Current.family, tag_ids: @tags.map(&:id))
render layout: "settings"
end
diff --git a/app/models/tag.rb b/app/models/tag.rb
index 63c9c34066..2c792bdfcb 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -31,6 +31,21 @@ class Tag < ApplicationRecord
def untagged_name
I18n.t(UNTAGGED_NAME_KEY)
end
+
+ # The given tags that are on at least one of the family's transactions, as
+ # a hash keyed by tag id, so a tag list can check each tag without a query
+ # per row (see Category.ids_with_transactions).
+ def ids_with_transactions(family:, tag_ids:)
+ tag_ids = Array(tag_ids).compact
+ return {} if tag_ids.empty?
+
+ family.transactions
+ .joins(:taggings)
+ .where(taggings: { tag_id: tag_ids })
+ .distinct
+ .pluck("taggings.tag_id")
+ .index_with(true)
+ end
end
# The value the transactions-filter checkbox submits for this tag: the
diff --git a/app/views/tags/_tag.html.erb b/app/views/tags/_tag.html.erb
index 65b280445a..8c839d87e2 100644
--- a/app/views/tags/_tag.html.erb
+++ b/app/views/tags/_tag.html.erb
@@ -1,4 +1,4 @@
-<%# locals: (tag:) %>
+<%# locals: (tag:, has_transactions: false) %>
<div id="<%= dom_id(tag) %>" class="flex justify-between items-center p-4 bg-container hover:bg-surface-hover">
<div class="flex w-full items-center gap-2.5">
@@ -11,7 +11,7 @@
<%= render DS::Menu.new do |menu| %>
<% menu.with_item(variant: "link", text: t(".edit"), href: edit_tag_path(tag), icon: "pencil", data: { turbo_frame: "modal" }) %>
- <% if tag.transactions.any? %>
+ <% if has_transactions %>
<% menu.with_item(
variant: "link",
text: t(".delete"),
diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb
index ba30078ac6..f22f9a7a81 100644
--- a/app/views/tags/index.html.erb
+++ b/app/views/tags/index.html.erb
@@ -32,7 +32,10 @@
<div class="bg-container rounded-lg shadow-border-xs">
<div class="overflow-hidden rounded-lg">
- <%= render partial: @tags, spacer_template: "shared/ruler" %>
+ <% @tags.each_with_index do |tag, index| %>
+ <%= render "shared/ruler" if index.positive? %>
+ <%= render "tags/tag", tag: tag, has_transactions: @tag_ids_with_transactions.key?(tag.id) %>
+ <% end %>
</div>
</div>
</div>
diff --git a/test/controllers/tags_controller_test.rb b/test/controllers/tags_controller_test.rb
index d0cd4a761e..a6b3253bc4 100644
--- a/test/controllers/tags_controller_test.rb
+++ b/test/controllers/tags_controller_test.rb
@@ -19,6 +19,26 @@ class TagsControllerTest < ActionDispatch::IntegrationTest
end
end
+ test "index offers the deletion dialog only for tags on transactions, without a query per tag" do
+ used = tags(:one)
+ unused = tags(:three)
+ assert used.transactions.any?
+ assert_not unused.transactions.any?
+
+ # Each capture follows a write, which clears the query cache tests keep
+ # across requests.
+ @user.family.tags.create!(name: "Unused A")
+ queries_with_few = capture_sql_queries { get tags_url }.size
+ 3.times { |i| @user.family.tags.create!(name: "Unused #{i + 1}") }
+ queries_with_more = capture_sql_queries { get tags_url }.size
+
+ assert_response :success
+ assert_equal queries_with_few, queries_with_more
+ assert_select "##{dom_id(used)} a[href='#{new_tag_deletion_path(used)}']", count: 1
+ assert_select "##{dom_id(unused)} form[action='#{tag_path(unused)}']", count: 1
+ assert_select "##{dom_id(unused)} a[href='#{new_tag_deletion_path(unused)}']", count: 0
+ end
+
test "should get new" do
get new_tag_url
assert_response :success
diff --git a/test/models/tag_test.rb b/test/models/tag_test.rb
index da19faf9f8..428647c272 100644
--- a/test/models/tag_test.rb
+++ b/test/models/tag_test.rb
@@ -16,6 +16,15 @@ class TagTest < ActiveSupport::TestCase
end
end
+ test "ids_with_transactions lists only the given tags that are on a transaction" do
+ family = families(:dylan_family)
+ ids = Tag.ids_with_transactions(family: family, tag_ids: [ tags(:one).id, tags(:two).id, tags(:three).id ])
+
+ assert_equal({ tags(:one).id => true, tags(:two).id => true }, ids)
+ assert_equal({}, Tag.ids_with_transactions(family: families(:empty), tag_ids: [ tags(:one).id ]))
+ assert_equal({}, Tag.ids_with_transactions(family: family, tag_ids: []))
+ end
+
test "rejects the reserved Untagged filter sentinel as a name" do
tag = families(:dylan_family).tags.new(name: Tag::UNTAGGED_FILTER_VALUE, color: "#e99537")