Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Show tags in the transaction list with an inline picker (#3734)

Mikael Møllera9d82a97d132
diff --git a/app/components/DS/tooltip.html.erb b/app/components/DS/tooltip.html.erb
index c4a52cc73e..4bdf82d6d9 100644
--- a/app/components/DS/tooltip.html.erb
+++ b/app/components/DS/tooltip.html.erb
@@ -1,5 +1,9 @@
<span data-controller="DS--tooltip" data-DS--tooltip-placement-value="<%= placement %>" data-DS--tooltip-offset-value="<%= offset %>" data-DS--tooltip-cross-axis-value="<%= cross_axis %>" class="<%= class_names("inline-flex", html_class) %>">
- <% if as == :button %>
+ <% if trigger? %>
+ <span class="inline-flex min-w-0 items-center" aria-describedby="<%= tooltip_id %>">
+ <%= trigger %>
+ </span>
+ <% elsif as == :button %>
<%# Wrap the trigger icon in a focusable `<button>` so AT users (and
keyboard-only users) can land on the tooltip anchor. The Lucide
icon itself carries `aria-hidden`, so the button's accessible
@@ -30,10 +34,10 @@
<div role="tooltip"
id="<%= tooltip_id %>"
data-DS--tooltip-target="tooltip"
- class="hidden absolute z-50 bg-inverse text-sm px-1.5 py-1 rounded-md">
+ class="<%= class_names("hidden absolute z-50", panel_classes) %>">
<%# `max-w-[20rem]` scales with the root font-size so AT users with
a larger base font don't see the tooltip clipped. %>
- <div class="text-inverse font-normal max-w-[20rem]">
+ <div class="<%= content_classes %>">
<%= tooltip_content %>
</div>
</div>
diff --git a/app/components/DS/tooltip.rb b/app/components/DS/tooltip.rb
index d79a937d0c..1f26323c0e 100644
--- a/app/components/DS/tooltip.rb
+++ b/app/components/DS/tooltip.rb
@@ -1,7 +1,14 @@
class DS::Tooltip < ApplicationComponent
AS_OPTIONS = %i[button span].freeze
+ VARIANTS = %i[inverse surface].freeze
- attr_reader :placement, :offset, :cross_axis, :icon_name, :size, :color, :tooltip_id, :as, :html_class
+ attr_reader :placement, :offset, :cross_axis, :icon_name, :size, :color, :tooltip_id, :as, :html_class, :variant
+
+ # Optional custom trigger that replaces the icon, for hover-to-reveal
+ # details on existing markup (e.g. a summary pill). It renders like
+ # `as: :span`: pass it only inside an already-focusable ancestor, or on
+ # markup that needs no keyboard reveal of its own.
+ renders_one :trigger
# NOTE: tooltip content must be non-interactive — no buttons, links,
# or form controls inside. Tooltips are exposed via `aria-describedby`,
@@ -24,8 +31,15 @@ class DS::Tooltip < ApplicationComponent
# sizing/layout utilities (e.g. `size-full items-center justify-center`)
# when `as: :span` should fill an icon-only link so hover covers the
# whole hit target, not just the icon glyph.
- def initialize(text: nil, placement: "top", offset: 10, cross_axis: 0, icon: "info", size: "sm", color: "default", as: :button, html_class: nil)
+ #
+ # `variant:` picks the panel surface.
+ # :inverse (default) — dark-on-light hint bubble for short help text.
+ # :surface — themed card matching the chart hover cards
+ # (`.chart-tooltip`), for richer content such as colored pills that
+ # would lose contrast on the inverse surface.
+ def initialize(text: nil, placement: "top", offset: 10, cross_axis: 0, icon: "info", size: "sm", color: "default", as: :button, html_class: nil, variant: :inverse)
raise ArgumentError, "as: must be one of #{AS_OPTIONS.inspect}" unless AS_OPTIONS.include?(as)
+ raise ArgumentError, "variant: must be one of #{VARIANTS.inspect}" unless VARIANTS.include?(variant.to_sym)
@text = text
@placement = placement
@@ -36,10 +50,23 @@ class DS::Tooltip < ApplicationComponent
@color = color
@as = as
@html_class = html_class
+ @variant = variant.to_sym
@tooltip_id = "tooltip-#{SecureRandom.hex(4)}"
end
+ def panel_classes
+ variant == :surface ? "chart-tooltip text-sm" : "bg-inverse text-sm px-1.5 py-1 rounded-md"
+ end
+
+ def content_classes
+ variant == :surface ? "text-primary font-normal max-w-[20rem]" : "text-inverse font-normal max-w-[20rem]"
+ end
+
def tooltip_content
+ # With a `trigger` slot the block exists to set the slot, so its return
+ # value must not shadow `text:`.
+ return @text if trigger? && @text.present?
+
content? ? content : @text
end
end
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb
index f0678dd480..984f4aeca1 100644
--- a/app/controllers/accounts_controller.rb
+++ b/app/controllers/accounts_controller.rb
@@ -119,7 +119,7 @@ class AccountsController < ApplicationController
if transactions.any?
ActiveRecord::Associations::Preloader.new(
records: transactions,
- associations: [ :transfer_as_inflow, :transfer_as_outflow, :category, :merchant ]
+ associations: [ :transfer_as_inflow, :transfer_as_outflow, :category, :merchant, :tags ]
).call
end
diff --git a/app/controllers/concerns/account_authorizable.rb b/app/controllers/concerns/account_authorizable.rb
index b2b75043fa..8c051446fd 100644
--- a/app/controllers/concerns/account_authorizable.rb
+++ b/app/controllers/concerns/account_authorizable.rb
@@ -12,7 +12,7 @@ module AccountAuthorizable
allowed = case level
when :write then permission.in?([ :owner, :full_control ])
- when :annotate then permission.in?([ :owner, :full_control, :read_write ])
+ when :annotate then permission == :owner || permission.to_s.in?(AccountShare::ANNOTATE_PERMISSIONS)
when :owner then permission == :owner
else false
end
diff --git a/app/controllers/tag/dropdowns_controller.rb b/app/controllers/tag/dropdowns_controller.rb
new file mode 100644
index 0000000000..1b5178c317
--- /dev/null
+++ b/app/controllers/tag/dropdowns_controller.rb
@@ -0,0 +1,8 @@
+class Tag::DropdownsController < ApplicationController
+ def show
+ @entry = Current.accessible_entries.where(entryable_type: "Transaction").find(params[:entry_id])
+ @transaction = @entry.transaction
+ @tags = Current.family.tags.alphabetically
+ @selected_tag_ids = @transaction.tag_ids.to_set
+ end
+end
diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb
index eb09ed8018..9aeaa75489 100644
--- a/app/controllers/transactions_controller.rb
+++ b/app/controllers/transactions_controller.rb
@@ -242,15 +242,46 @@ class TransactionsController < ApplicationController
def update_tags
return unless require_account_permission!(@entry.account, :annotate, redirect_path: transaction_path(@entry))
- tag_ids = Current.family.tags.where(id: tag_ids_param).pluck(:id)
+ # The transaction-row tag picker toggles one tag at a time; the drawer's
+ # multiselect sends the full set.
+ if params[:toggle_tag_id].present?
+ @toggled_tag = Current.family.tags.find(params[:toggle_tag_id])
+ @entry.transaction.toggle_tag!(@toggled_tag)
+ else
+ @entry.transaction.tag_ids = Current.family.tags.where(id: tag_ids_param).pluck(:id)
+ end
- @entry.transaction.tag_ids = tag_ids
@entry.lock_saved_attributes!
@entry.mark_user_modified!
@entry.transaction.lock_attr!(:tag_ids)
@entry.sync_account_later
- render json: { tag_ids: @entry.transaction.tag_ids }
+ respond_to do |format|
+ # JSON stays first so Accept: */* callers keep the original response.
+ format.json { render json: { tag_ids: @entry.transaction.tag_ids } }
+ # Without Turbo the row picker's button_to posts as plain HTML.
+ format.html { redirect_back_or_to transaction_path(@entry) }
+ format.turbo_stream do
+ transaction = @entry.transaction
+ streams = %i[desktop mobile].map do |variant|
+ turbo_stream.replace(
+ dom_id(transaction, "tag_summary_#{variant}"),
+ partial: "tags/summary",
+ locals: { transaction: transaction, variant: variant }
+ )
+ end
+ if @toggled_tag
+ # autofocus hands keyboard focus back to the re-rendered option,
+ # which Turbo focuses after the stream renders.
+ streams << turbo_stream.replace(
+ "#{dom_id(@entry, :tag_option)}_#{@toggled_tag.id}",
+ partial: "tag/dropdowns/row",
+ locals: { tag: @toggled_tag, entry: @entry, selected: transaction.tag_ids.include?(@toggled_tag.id), autofocus: true }
+ )
+ end
+ render turbo_stream: streams
+ end
+ end
end
def merge_duplicate
diff --git a/app/helpers/transactions_helper.rb b/app/helpers/transactions_helper.rb
index 3d44a8f254..2e1d51a322 100644
--- a/app/helpers/transactions_helper.rb
+++ b/app/helpers/transactions_helper.rb
@@ -35,6 +35,15 @@ module TransactionsHelper
entry.split_child? && Current.user.show_split_grouped? && params_grouped == "true"
end
+ # Whether the current user may annotate (e.g. re-tag) entries on this
+ # account. Memoized per request so list rows don't query per row.
+ def can_annotate_account?(account)
+ return false unless Current.user
+
+ @annotatable_account_ids ||= Account.annotatable_by(Current.user).pluck(:id).to_set
+ @annotatable_account_ids.include?(account.id)
+ end
+
# ---- Transaction extra details helpers ----
# Returns a structured hash describing extra details for a transaction.
# Input can be a Transaction or an Entry (responds_to :transaction).
diff --git a/app/javascript/controllers/tag_fit_controller.js b/app/javascript/controllers/tag_fit_controller.js
new file mode 100644
index 0000000000..9eb476600d
--- /dev/null
+++ b/app/javascript/controllers/tag_fit_controller.js
@@ -0,0 +1,46 @@
+import { Controller } from "@hotwired/stimulus";
+
+// Shows a transaction's tags as full pills when they all fit on one line,
+// otherwise the compact letter-badge pill. The available width comes from the
+// closest `[data-tag-fit-bounds]` ancestor (its value is the share of that
+// width the tags may use), which is sized by the grid/line rather than by the
+// tags, so toggling between the two forms can't feed back into the observer.
+export default class extends Controller {
+ static targets = ["full", "compact"];
+
+ connect() {
+ this.bounds =
+ this.element.closest("[data-tag-fit-bounds]") ||
+ this.element.parentElement;
+ this.resizeObserver = new ResizeObserver(() => this.fit());
+ this.resizeObserver.observe(this.bounds);
+ this.fit();
+ }
+
+ disconnect() {
+ this.resizeObserver?.disconnect();
+ }
+
+ fit() {
+ // Hidden bounds (e.g. the mobile line on desktop) have no width to fit.
+ if (this.bounds.clientWidth === 0) return;
+
+ this.showFull(true);
+ const pills = this.fullTarget.firstElementChild;
+ this.showFull(pills.scrollWidth <= this.availableWidth());
+ }
+
+ availableWidth() {
+ const style = getComputedStyle(this.bounds);
+ const padding =
+ Number.parseFloat(style.paddingLeft) +
+ Number.parseFloat(style.paddingRight);
+ const share = Number.parseFloat(this.bounds.dataset.tagFitBounds) || 1;
+ return (this.bounds.clientWidth - padding) * share;
+ }
+
+ showFull(full) {
+ this.fullTarget.hidden = !full;
+ this.compactTarget.hidden = full;
+ }
+}
diff --git a/app/models/account.rb b/app/models/account.rb
index 77f5516ae9..f17ac5b71e 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -80,6 +80,15 @@ class Account < ApplicationRecord
.distinct
}
+ # Accounts a user can annotate (category, tags, notes, merchant): owned,
+ # or shared with full_control or read_write
+ scope :annotatable_by, ->(user) {
+ left_joins(:account_shares)
+ .where("accounts.owner_id = :uid OR (account_shares.user_id = :uid AND account_shares.permission IN (:permissions))",
+ uid: user.id, permissions: AccountShare::ANNOTATE_PERMISSIONS)
+ .distinct
+ }
+
# Accounts that count in a user's financial calculations
scope :included_in_finances_for, ->(user) {
left_joins(:account_shares)
diff --git a/app/models/account_share.rb b/app/models/account_share.rb
index 8737df6253..e4fdca42ff 100644
--- a/app/models/account_share.rb
+++ b/app/models/account_share.rb
@@ -3,6 +3,9 @@ class AccountShare < ApplicationRecord
belongs_to :user
PERMISSIONS = %w[full_control read_write read_only].freeze
+ # Share tiers that may annotate (category, tags, notes, merchant); owners
+ # always can. Single source for the per-account check and bulk scopes.
+ ANNOTATE_PERMISSIONS = %w[full_control read_write].freeze
validates :permission, inclusion: { in: PERMISSIONS }
validates :user_id, uniqueness: { scope: :account_id }
@@ -24,7 +27,7 @@ class AccountShare < ApplicationRecord
end
def can_annotate?
- full_control? || read_write?
+ permission.in?(ANNOTATE_PERMISSIONS)
end
def can_edit?
diff --git a/app/models/transaction.rb b/app/models/transaction.rb
index ccbbcdfd4b..6d59912990 100644
--- a/app/models/transaction.rb
+++ b/app/models/transaction.rb
@@ -161,6 +161,17 @@ class Transaction < ApplicationRecord
update!(category: category)
end
+ # Adds or removes one tag while holding the row lock. Assigning tag_ids
+ # replaces the whole set, so two quick toggles computed from the same
+ # snapshot would drop one of them; per-tagging writes under the lock can't.
+ def toggle_tag!(tag)
+ with_lock do
+ existing = taggings.where(tag: tag)
+ existing.exists? ? existing.destroy_all : taggings.create!(tag: tag)
+ end
+ tags.reset
+ end
+
# Marks a category as recently used. Called explicitly from the manual
# category-assignment controllers (picker, edit form, categorization
# wizard, create-and-assign) after a successful save — not wired to a
diff --git a/app/views/tag/dropdowns/_row.html.erb b/app/views/tag/dropdowns/_row.html.erb
new file mode 100644
index 0000000000..1c8e346b80
--- /dev/null
+++ b/app/views/tag/dropdowns/_row.html.erb
@@ -0,0 +1,21 @@
+<%# locals: (tag:, entry:, selected:, autofocus: false) %>
+
+<%= content_tag :div,
+ id: "#{dom_id(entry, :tag_option)}_#{tag.id}",
+ role: "option",
+ aria: { selected: selected.to_s },
+ class: ["filterable-item flex items-center border-none rounded-lg px-2 py-1 w-full hover:bg-container-inset-hover",
+ { "bg-container-inset": selected }],
+ data: { filter_name: tag.name } do %>
+ <%= button_to tags_transaction_path(entry),
+ method: :patch,
+ params: { toggle_tag_id: tag.id },
+ autofocus: autofocus,
+ class: "flex w-full items-center gap-1.5 cursor-pointer" do %>
+ <span class="flex items-center justify-center w-5 h-5 shrink-0">
+ <% if selected %><%= icon("check") %><% end %>
+ </span>
+
+ <%= render DS::Pill.new(label: tag.name, marker: false, size: :md, custom_color: tag.color, truncate: true) %>
+ <% end %>
+<% end %>
diff --git a/app/views/tag/dropdowns/show.html.erb b/app/views/tag/dropdowns/show.html.erb
new file mode 100644
index 0000000000..8baf62491a
--- /dev/null
+++ b/app/views/tag/dropdowns/show.html.erb
@@ -0,0 +1,45 @@
+<%= turbo_frame_tag "tag_dropdown" do %>
+ <div class="flex flex-col relative" data-controller="list-filter">
+ <div class="grow p-1.5">
+ <div class="relative flex items-center bg-container border border-secondary rounded-lg">
+ <input
+ placeholder="<%= t(".search_placeholder") %>"
+ autocomplete="nope"
+ type="search"
+ role="combobox"
+ aria-label="<%= t(".search_placeholder") %>"
+ aria-expanded="false"
+ aria-autocomplete="list"
+ class="bg-container placeholder:text-sm placeholder:text-secondary font-normal h-10 relative pl-10 w-full border-none rounded-lg focus:outline-hidden focus:ring-0"
+ data-list-filter-target="input"
+ data-action="list-filter#filter keydown->list-filter#handleKeydown">
+ <%= icon("search", class: "absolute inset-0 ml-2 transform top-1/2 -translate-y-1/2") %>
+ </div>
+ </div>
+
+ <div data-list-filter-target="list" role="listbox" aria-multiselectable="true" class="flex flex-col gap-0.5 p-1.5 mt-0.5 mr-2 max-h-64 overflow-y-scroll scrollbar">
+ <div class="pb-2 pl-4 mr-2 text-secondary hidden" data-list-filter-target="emptyMessage">
+ <%= t(".no_tags") %>
+ </div>
+
+ <% if @tags.any? %>
+ <% @tags.each do |tag| %>
+ <%= render "tag/dropdowns/row", tag: tag, entry: @entry, selected: @selected_tag_ids.include?(tag.id) %>
+ <% end %>
+ <% else %>
+ <p class="py-6 text-center text-sm text-secondary"><%= t(".empty") %></p>
+ <% end %>
+ </div>
+
+ <%= render "shared/ruler", classes: "my-2" %>
+
+ <div class="relative p-1.5 w-full">
+ <%= link_to tags_path,
+ data: { turbo_frame: "_top" },
+ class: "flex text-sm font-medium items-center gap-2 text-secondary w-full rounded-lg p-2 hover:bg-container-inset-hover" do %>
+ <%= icon("settings-2") %>
+ <p><%= t(".manage") %></p>
+ <% end %>
+ </div>
+ </div>
+<% end %>
diff --git a/app/views/tags/_initial.html.erb b/app/views/tags/_initial.html.erb
new file mode 100644
index 0000000000..8b3962a91f
--- /dev/null
+++ b/app/views/tags/_initial.html.erb
@@ -0,0 +1,7 @@
+<%# locals: (tag:) %>
+<%# Opaque soft tint (same hue treatment as the tag pill) so overlapping
+ badges stay legible and the letter reads on light and dark surfaces. %>
+<span class="inline-flex items-center justify-center size-5 shrink-0 rounded-full text-xs font-semibold leading-none uppercase"
+ style="background-color: color-mix(in oklab, <%= tag.color %> 20%, var(--color-container)); color: <%= tag.color %>;"
+ data-tag-initial
+ aria-hidden="true"><%= tag.name.first %></span>
diff --git a/app/views/tags/_menu.html.erb b/app/views/tags/_menu.html.erb
new file mode 100644
index 0000000000..c1c62b4819
--- /dev/null
+++ b/app/views/tags/_menu.html.erb
@@ -0,0 +1,26 @@
+<%# locals: (entry:, transaction:, variant: :desktop) %>
+
+<%# Always mounted, even without tags, so the toggle stream can fill in the
+ first tag. The mobile slot hides itself while the summary is empty
+ (touch has no hover affordance to show). %>
+<div class="w-full min-w-0">
+ <% if transaction.transfer? || !can_annotate_account?(entry.account) %>
+ <%# Read-only: transfer tags must stay in sync across both legs (edit them
+ from the drawer), and read-only shares can't change tags at all. %>
+ <%= render "tags/summary", transaction: transaction, variant: variant, editable: false %>
+ <% else %>
+ <%= render DS::Popover.new(variant: "button", aria_label: t(".label")) do |popover| %>
+ <% popover.with_button(class: "flex w-full min-w-0 items-center") do %>
+ <%= render "tags/summary", transaction: transaction, variant: variant %>
+ <% end %>
+
+ <% popover.with_custom_content do %>
+ <%= turbo_frame_tag "tag_dropdown", src: tag_dropdown_path(entry_id: entry.id), loading: :lazy do %>
+ <div class="p-6 flex items-center justify-center">
+ <p class="text-sm text-secondary animate-pulse"><%= t(".loading") %></p>
+ </div>
+ <% end %>
+ <% end %>
+ <% end %>
+ <% end %>
+</div>
diff --git a/app/views/tags/_summary.html.erb b/app/views/tags/_summary.html.erb
new file mode 100644
index 0000000000..e71ab5e708
--- /dev/null
+++ b/app/views/tags/_summary.html.erb
@@ -0,0 +1,35 @@
+<%# locals: (transaction:, variant: :desktop, editable: true) %>
+<% tags = transaction.tags.sort_by { |tag| [ tag.name.downcase, tag.id ] } %>
+
+<%# Several tags render both as full pills and as the compact summary;
+ the tag-fit controller shows the pills whenever they all fit the
+ available width and falls back to the summary otherwise. %>
+<span id="<%= dom_id(transaction, "tag_summary_#{variant}") %>" class="inline-flex min-w-0 max-w-full" <%= "data-tag-empty" if tags.empty? %>>
+ <% if tags.empty? %>
+ <% if editable && variant.to_sym == :desktop %>
+ <%= render DS::Tooltip.new(text: t(".add")) do |tooltip| %>
+ <% tooltip.with_trigger do %>
+ <span class="invisible group-hover:visible in-focus-visible:visible inline-flex items-center justify-center w-7 h-7 rounded-full text-secondary hover:bg-container-inset-hover">
+ <%= icon("tag", size: "sm", color: "current") %>
+ <span class="sr-only"><%= t(".add") %></span>
+ </span>
+ <% end %>
+ <% end %>
+ <% end %>
+ <% elsif tags.one? %>
+ <%= render "tags/summary_tooltip", tags: tags, editable: editable %>
+ <% else %>
+ <span class="inline-flex min-w-0 max-w-full" data-controller="tag-fit">
+ <span data-tag-fit-target="full" hidden>
+ <%= tag.span class: ["inline-flex items-center gap-1", { "rounded-full focus-ring": !editable }] do %>
+ <% tags.each do |tag| %>
+ <%= render DS::Pill.new(label: tag.name, custom_color: tag.color, marker: false, size: :md, title: "") %>
+ <% end %>
+ <% end %>
+ </span>
+ <span class="inline-flex min-w-0 max-w-full" data-tag-fit-target="compact">
+ <%= render "tags/summary_tooltip", tags: tags, editable: editable %>
+ </span>
+ </span>
+ <% end %>
+</span>
diff --git a/app/views/tags/_summary_tooltip.html.erb b/app/views/tags/_summary_tooltip.html.erb
new file mode 100644
index 0000000000..abb4cf673a
--- /dev/null
+++ b/app/views/tags/_summary_tooltip.html.erb
@@ -0,0 +1,45 @@
+<%# locals: (tags:, editable:) %>
+<% names = tags.map(&:name).join(", ") %>
+
+<%# A single tag as a (truncating) pill, or several as a neutral pill of
+ letter badges (first two plus "+N" beyond three). Hovering lists the
+ full tag names. %>
+<%= render DS::Tooltip.new(placement: "top", variant: :surface, html_class: "min-w-0 max-w-full") do |tooltip| %>
+ <% tooltip.with_trigger do %>
+ <%# Editable summaries sit inside the picker button, which takes focus.
+ Read-only ones need their own tab stop so keyboard users can open
+ the tooltip with the full names. %>
+ <%= tag.span class: ["inline-flex min-w-0", { "rounded-full focus-ring": !editable }], tabindex: (editable ? nil : 0) do %>
+ <% if tags.one? %>
+ <%# Empty title: the tooltip below replaces the pill's native one. %>
+ <%= render DS::Pill.new(
+ label: tags.first.name,
+ custom_color: tags.first.color,
+ marker: false,
+ size: :md,
+ truncate: true,
+ title: "") %>
+ <% else %>
+ <% shown = tags.size > 3 ? tags.first(2) : tags %>
+ <%= tag.span class: ["inline-flex shrink-0 items-center gap-1 rounded-full border border-secondary bg-container-inset p-0.5 text-sm leading-none", { "pr-2": tags.size > shown.size }] do %>
+ <span class="inline-flex items-center -space-x-1">
+ <% shown.each do |tag| %>
+ <span class="inline-flex rounded-full ring-2 ring-container-inset"><%= render "tags/initial", tag: tag %></span>
+ <% end %>
+ </span>
+ <% if tags.size > shown.size %>
+ <span class="text-xs font-medium text-secondary" aria-hidden="true">+<%= tags.size - shown.size %></span>
+ <% end %>
+ <span class="sr-only"><%= t("tags.summary.title", names: names) %></span>
+ <% end %>
+ <% end %>
+ <% end %>
+ <% end %>
+ <ul class="flex flex-col items-start gap-1 py-0.5" data-tag-summary-tooltip>
+ <% tags.each do |tag| %>
+ <li class="flex max-w-full min-w-0">
+ <%= render DS::Pill.new(label: tag.name, custom_color: tag.color, marker: false, size: :md, truncate: true, title: "") %>
+ </li>
+ <% end %>
+ </ul>
+<% end %>
diff --git a/app/views/transactions/_list.html.erb b/app/views/transactions/_list.html.erb
index 5d57daa191..e7aed3e134 100644
--- a/app/views/transactions/_list.html.erb
+++ b/app/views/transactions/_list.html.erb
@@ -24,7 +24,7 @@
<div class="grow overflow-y-auto">
<% if @transactions.any? %>
<div class="grid-cols-12 bg-container-inset rounded-xl px-5 py-3 text-xs uppercase font-medium text-secondary items-center mb-4 grid">
- <div class="pl-0.5 col-span-8 flex items-center gap-4">
+ <div class="pl-0.5 col-span-5 flex items-center gap-4">
<%= check_box_tag "selection_entry",
class: "checkbox checkbox--light hidden lg:block",
data: {
@@ -34,7 +34,8 @@
<p><%= t(".transaction") %></p>
</div>
- <p class="col-span-2 md:block hidden"><%= t("transactions.form.category_label") %></p>
+ <p class="col-span-3 md:block hidden"><%= t("transactions.form.category_label") %></p>
+ <p class="col-span-2 md:block hidden"><%= t("transactions.form.tags_label") %></p>
<p class="col-span-2 col-start-11 md:col-start-auto justify-self-end md:block"><%= t("transactions.show.amount") %></p>
</div>
<% end %>
diff --git a/app/views/transactions/_transaction.html.erb b/app/views/transactions/_transaction.html.erb
index 0455b4047e..fc22593bdf 100644
--- a/app/views/transactions/_transaction.html.erb
+++ b/app/views/transactions/_transaction.html.erb
@@ -14,7 +14,7 @@
data-controller="clickable-row"
data-action="click->clickable-row#open">
- <div class="pr-4 lg:pr-10 flex items-center gap-3 lg:gap-4 col-span-7 min-w-0">
+ <div class="pr-4 lg:pr-10 flex items-center gap-3 lg:gap-4 col-span-5 min-w-0 max-md:flex-1">
<%= check_box_tag dom_id(entry, "selection"),
disabled: transaction.transfer.present?,
class: "checkbox checkbox--light hidden lg:block",
@@ -39,7 +39,7 @@
<% end %>
</div>
- <div class="max-w-full min-w-0 <%= "opacity-50 text-secondary" if entry.excluded %>">
+ <div class="flex-1 max-w-full min-w-0 <%= "opacity-50 text-secondary" if entry.excluded %>">
<%= content_tag :div, class: ["flex items-center gap-3 lg:gap-4"] do %>
<div class="hidden lg:flex shrink-0">
<% if transaction.transfer? %>
@@ -69,9 +69,9 @@
<% end %>
</div>
- <div class="truncate">
+ <div class="truncate flex-1 min-w-0">
<div class="space-y-0.5">
- <div class="flex items-center gap-1 min-w-0">
+ <div class="flex items-center gap-1 min-w-0" data-tag-fit-bounds="0.5">
<div class="truncate flex-shrink">
<%= link_to(
entry.name,
@@ -144,6 +144,10 @@
<%= render "transactions/transfer_match", transaction: transaction %>
<% end %>
</div>
+
+ <div class="md:hidden flex shrink-0 min-w-0 max-w-1/2 has-[[data-tag-empty]]:hidden">
+ <%= render "tags/menu", entry: entry, transaction: transaction, variant: :mobile %>
+ </div>
</div>
<div class="text-secondary text-xs font-normal">
@@ -177,7 +181,7 @@
</span>
<div class="flex min-w-0 items-center gap-1">
<%= render "categories/category_name_mobile", transaction: transaction %>
- <% if transaction.merchant&.present? %>
+ <% if transaction.merchant&.present? %>
<span class="lg:hidden min-w-0 truncate">• <%= transaction.merchant.name %></span>
<% end %>
</div>
@@ -189,7 +193,7 @@
</div>
</div>
- <div class="hidden md:flex min-w-0 items-center gap-1 col-span-3">
+ <div class="hidden md:flex min-w-0 items-center gap-1 col-span-3 pr-2">
<% if entry.account.supports_trades? && !transaction.transfer? %>
<%# For investment/crypto accounts, show activity label instead of category %>
<%= render "investment_activity/quick_edit_badge", entry: entry, entryable: transaction %>
@@ -198,6 +202,10 @@
<% end %>
</div>
+ <div class="hidden md:flex md:w-1/5 md:shrink-0 lg:w-auto min-w-0 items-center col-span-2 pr-2" data-tag-fit-bounds="1">
+ <%= render "tags/menu", entry: entry, transaction: transaction, variant: :desktop %>
+ </div>
+
<div class="shrink-0 col-span-4 lg:col-span-2 ml-auto flex items-center justify-end gap-2 <%= "opacity-50" if entry.excluded %>">
<%# Protection indicator - shows on hover when entry is protected from sync %>
<% if entry.protected_from_sync? && !entry.excluded? %>
diff --git a/config/locales/views/tag/dropdowns/en.yml b/config/locales/views/tag/dropdowns/en.yml
new file mode 100644
index 0000000000..2306b4b5c2
--- /dev/null
+++ b/config/locales/views/tag/dropdowns/en.yml
@@ -0,0 +1,9 @@
+---
+en:
+ tag:
+ dropdowns:
+ show:
+ empty: No tags yet
+ manage: Manage tags
+ no_tags: No tags found
+ search_placeholder: Search
diff --git a/config/locales/views/tags/en.yml b/config/locales/views/tags/en.yml
index 0764778599..f374d56bff 100644
--- a/config/locales/views/tags/en.yml
+++ b/config/locales/views/tags/en.yml
@@ -17,8 +17,14 @@ en:
new: New tag
tags: Tags
delete_all: Delete all
+ menu:
+ label: Edit tags
+ loading: Loading tags...
new:
new: New tag
+ summary:
+ add: Add tags
+ title: 'Tags: %{names}'
tag:
delete: Delete
edit: Edit
diff --git a/config/routes.rb b/config/routes.rb
index d87c360813..75a42fc1f9 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -427,6 +427,10 @@ Rails.application.routes.draw do
delete :destroy_all, on: :collection
end
+ namespace :tag do
+ resource :dropdown, only: :show
+ end
+
namespace :category do
resource :dropdown, only: :show
end
diff --git a/test/components/DS/tooltip_test.rb b/test/components/DS/tooltip_test.rb
new file mode 100644
index 0000000000..ef801dd3ef
--- /dev/null
+++ b/test/components/DS/tooltip_test.rb
@@ -0,0 +1,33 @@
+require "test_helper"
+
+class DS::TooltipTest < ViewComponent::TestCase
+ test "defaults to the inverse surface with an icon trigger" do
+ render_inline(DS::Tooltip.new(text: "Helpful hint"))
+
+ assert_selector "button[aria-describedby]"
+ assert_selector "[role=tooltip].bg-inverse", visible: :all, text: "Helpful hint"
+ assert_selector "[role=tooltip] .text-inverse", visible: :all
+ end
+
+ test "surface variant uses the themed card" do
+ render_inline(DS::Tooltip.new(text: "Card content", variant: :surface))
+
+ assert_selector "[role=tooltip].chart-tooltip", visible: :all, text: "Card content"
+ assert_selector "[role=tooltip] .text-primary", visible: :all
+ assert_no_selector "[role=tooltip].bg-inverse", visible: :all
+ end
+
+ test "custom trigger replaces the icon" do
+ render_inline(DS::Tooltip.new(text: "Full name")) do |tooltip|
+ tooltip.with_trigger { "Trigger pill" }
+ end
+
+ assert_selector "span[aria-describedby]", text: "Trigger pill"
+ assert_no_selector "button"
+ assert_selector "[role=tooltip]", visible: :all, text: "Full name"
+ end
+
+ test "rejects unknown variants" do
+ assert_raises(ArgumentError) { DS::Tooltip.new(variant: :neon) }
+ end
+end
diff --git a/test/components/previews/tooltip_component_preview.rb b/test/components/previews/tooltip_component_preview.rb
index a6345ef0be..42c182101c 100644
--- a/test/components/previews/tooltip_component_preview.rb
+++ b/test/components/previews/tooltip_component_preview.rb
@@ -31,4 +31,10 @@ class TooltipComponentPreview < ViewComponent::Preview
end
end
end
+
+ def with_custom_trigger
+ render DS::Tooltip.new(text: "Groceries, Vacation, Reimbursable", variant: :surface) do |tooltip|
+ tooltip.with_trigger { render DS::Pill.new(label: "3 tags", tone: :neutral, marker: false) }
+ end
+ end
end
diff --git a/test/controllers/tag/dropdowns_controller_test.rb b/test/controllers/tag/dropdowns_controller_test.rb
new file mode 100644
index 0000000000..f53dfc3717
--- /dev/null
+++ b/test/controllers/tag/dropdowns_controller_test.rb
@@ -0,0 +1,29 @@
+require "test_helper"
+
+class Tag::DropdownsControllerTest < ActionDispatch::IntegrationTest
+ include ActionView::RecordIdentifier
+
+ setup do
+ sign_in users(:family_admin)
+ @entry = entries(:transaction)
+ @entry.entryable.update!(tag_ids: [ tags(:one).id ])
+ ensure_tailwind_build
+ end
+
+ test "lists the family's tags and marks applied ones" do
+ get tag_dropdown_url(entry_id: @entry.id)
+
+ assert_response :success
+ assert_select "##{dom_id(@entry, :tag_option)}_#{tags(:one).id}[aria-selected=true]"
+ assert_select "##{dom_id(@entry, :tag_option)}_#{tags(:two).id}[aria-selected=false]"
+ end
+
+ test "does not load another family's transaction" do
+ other_entry = entries(:transaction)
+ sign_in users(:empty)
+
+ get tag_dropdown_url(entry_id: other_entry.id)
+
+ assert_response :not_found
+ end
+end
diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rb
index 905575daca..4b2c571572 100644
--- a/test/controllers/transactions_controller_test.rb
+++ b/test/controllers/transactions_controller_test.rb
@@ -2,6 +2,7 @@ require "test_helper"
class TransactionsControllerTest < ActionDispatch::IntegrationTest
include EntryableResourceInterfaceTest, EntriesTestHelper
+ include ActionView::RecordIdentifier
setup do
sign_in @user = users(:family_admin)
@@ -489,6 +490,63 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
assert_equal original_tag_ids, read_only_entry.reload.entryable.tag_ids
end
+ test "tag-only endpoint toggles a single tag and streams the row's tag UI" do
+ @entry.entryable.update!(tag_ids: [ tags(:one).id ], locked_attributes: {})
+
+ patch tags_transaction_url(@entry), params: { toggle_tag_id: tags(:two).id }, as: :turbo_stream
+
+ assert_response :success
+ assert_equal [ tags(:one).id, tags(:two).id ].sort, @entry.reload.entryable.tag_ids.sort
+ assert @entry.entryable.locked?(:tag_ids)
+ assert_select "turbo-stream[action=replace][target=?]", dom_id(@entry.entryable, "tag_summary_desktop")
+ assert_select "turbo-stream[action=replace][target=?]", dom_id(@entry.entryable, "tag_summary_mobile")
+ assert_select "turbo-stream[action=replace][target=?]", "#{dom_id(@entry, :tag_option)}_#{tags(:two).id}"
+
+ patch tags_transaction_url(@entry), params: { toggle_tag_id: tags(:one).id }, as: :turbo_stream
+
+ assert_response :success
+ assert_equal [ tags(:two).id ], @entry.reload.entryable.tag_ids
+ end
+
+ test "tag-only endpoint falls back to a redirect for plain HTML toggles" do
+ @entry.entryable.update!(tag_ids: [], locked_attributes: {})
+
+ patch tags_transaction_url(@entry), params: { toggle_tag_id: tags(:one).id }
+
+ assert_redirected_to transaction_path(@entry)
+ assert_equal [ tags(:one).id ], @entry.reload.entryable.tag_ids
+ end
+
+ test "tag-only endpoint does not toggle tags from another family" do
+ other_tag = users(:empty).family.tags.create!(name: "Other family")
+ original_tag_ids = @entry.entryable.tag_ids
+
+ patch tags_transaction_url(@entry), params: { toggle_tag_id: other_tag.id }, as: :turbo_stream
+
+ assert_response :not_found
+ assert_equal original_tag_ids, @entry.reload.entryable.tag_ids
+ end
+
+ test "tag-only endpoint does not toggle tags for read-only users" do
+ sign_in users(:family_member)
+ read_only_entry = entries(:transfer_in)
+ original_tag_ids = read_only_entry.entryable.tag_ids
+
+ patch tags_transaction_url(read_only_entry), params: { toggle_tag_id: tags(:one).id }, as: :turbo_stream
+
+ assert_equal original_tag_ids, read_only_entry.reload.entryable.tag_ids
+ end
+
+ test "transaction rows show tags" do
+ @entry.entryable.update!(tag_ids: [ tags(:one).id ])
+
+ get transactions_url
+
+ assert_response :success
+ assert_select "##{dom_id(@entry.entryable, "tag_summary_desktop")}", text: /#{tags(:one).name}/
+ assert_select "##{dom_id(@entry.entryable, "tag_summary_mobile")}", text: /#{tags(:one).name}/
+ end
+
test "split parent rows mark amount as privacy-sensitive" do
entry = create_transaction(account: accounts(:depository), amount: 100, name: "Split parent")
diff --git a/test/models/account_test.rb b/test/models/account_test.rb
index 9d4f8d3cf2..aa1d7bf9b4 100644
--- a/test/models/account_test.rb
+++ b/test/models/account_test.rb
@@ -920,4 +920,15 @@ class AccountTest < ActiveSupport::TestCase
assert_nil account.history_start_date
end
+
+ test "annotatable_by includes owned and annotate-tier shares but not read-only ones" do
+ member = users(:family_member)
+ ids = Account.annotatable_by(member).pluck(:id)
+
+ assert_includes ids, accounts(:depository).id, "full_control share"
+ assert_not_includes ids, accounts(:credit_card).id, "read_only share"
+
+ accounts(:credit_card).account_shares.find_by!(user: member).update!(permission: "read_write")
+ assert_includes Account.annotatable_by(member).pluck(:id), accounts(:credit_card).id, "read_write share"
+ end
end
diff --git a/test/models/transaction_test.rb b/test/models/transaction_test.rb
index ea4efcff7e..106cad4d99 100644
--- a/test/models/transaction_test.rb
+++ b/test/models/transaction_test.rb
@@ -307,4 +307,27 @@ class TransactionTest < ActiveSupport::TestCase
assert_nil category.reload.last_used_at
end
+
+ test "toggle_tag! adds a missing tag and removes a present one" do
+ transaction = transactions(:one)
+ transaction.update!(tag_ids: [ tags(:one).id ])
+
+ transaction.toggle_tag!(tags(:two))
+ assert_equal [ tags(:one).id, tags(:two).id ].sort, transaction.tag_ids.sort
+
+ transaction.toggle_tag!(tags(:one))
+ assert_equal [ tags(:two).id ], transaction.reload.tag_ids
+ end
+
+ test "toggle_tag! keeps a tag added by a concurrent toggle" do
+ transaction = transactions(:one)
+ transaction.update!(tag_ids: [])
+ stale_copy = Transaction.find(transaction.id)
+ stale_copy.tag_ids # load the pre-toggle snapshot
+
+ transaction.toggle_tag!(tags(:one))
+ stale_copy.toggle_tag!(tags(:two))
+
+ assert_equal [ tags(:one).id, tags(:two).id ].sort, transaction.reload.tag_ids.sort
+ end
end
diff --git a/test/system/transactions_test.rb b/test/system/transactions_test.rb
index 056bf8ad2e..6d2ec82630 100644
--- a/test/system/transactions_test.rb
+++ b/test/system/transactions_test.rb
@@ -140,6 +140,73 @@ class TransactionsTest < ApplicationSystemTestCase
assert_text @transaction.name
end
+ test "can toggle tags from the transaction row" do
+ transaction = @transaction.entryable
+ summary_id = dom_id(transaction, "tag_summary_desktop")
+ option_id = "#{dom_id(@transaction, :tag_option)}_#{tags(:two).id}"
+
+ within "##{summary_id}" do
+ assert_text tags(:one).name
+ end
+
+ find("##{summary_id}").click
+ find("##{option_id} button").click
+
+ assert_selector "##{option_id}[aria-selected='true']"
+ assert_selector "##{summary_id} [data-tag-fit-target=compact] [data-tag-initial]", count: 2, visible: :all
+ assert_equal [ tags(:one).id, tags(:two).id ].sort, transaction.reload.tag_ids.sort
+ end
+
+ test "tags show as full pills when they fit and collapse when the column shrinks" do
+ transaction = @transaction.entryable
+ short_tags = %w[Ab Cd].map { |name| @user.family.tags.create!(name: name, color: Tag::COLORS.first) }
+ transaction.update!(tag_ids: short_tags.map(&:id))
+ visit transactions_url(per_page: @page_size)
+
+ summary = "##{dom_id(transaction, "tag_summary_desktop")}"
+ cell = "document.querySelector('#{summary}').closest('[data-tag-fit-bounds]')"
+
+ page.execute_script("#{cell}.style.width = '400px'")
+ assert_selector "#{summary} [data-tag-fit-target=full]", text: "Ab"
+ assert_no_selector "#{summary} [data-tag-fit-target=compact]"
+
+ page.execute_script("#{cell}.style.width = '48px'")
+ assert_selector "#{summary} [data-tag-fit-target=compact] [data-tag-initial]", count: 2
+ assert_no_selector "#{summary} [data-tag-fit-target=full]"
+
+ page.execute_script("#{cell}.style.width = '400px'")
+ assert_selector "#{summary} [data-tag-fit-target=full]", text: "Cd"
+ end
+
+ test "a first tag added on desktop also shows on the mobile row" do
+ transaction = @uncategorized_transaction.entryable
+ assert_empty transaction.tags
+ option_id = "#{dom_id(@uncategorized_transaction, :tag_option)}_#{tags(:one).id}"
+
+ find("##{dom_id(@uncategorized_transaction)}").hover
+ find("##{dom_id(transaction, "tag_summary_desktop")}").click
+ find("##{option_id} button").click
+ assert_selector "##{option_id}[aria-selected='true']"
+
+ page.current_window.resize_to(390, 900)
+ assert_selector "##{dom_id(transaction, "tag_summary_mobile")}", text: tags(:one).name
+ ensure
+ page.current_window.resize_to(1400, 1400)
+ end
+
+ test "keyboard focus stays on a tag option after toggling it" do
+ summary_id = dom_id(@transaction.entryable, "tag_summary_desktop")
+ option_id = "#{dom_id(@transaction, :tag_option)}_#{tags(:two).id}"
+
+ find("##{summary_id}").click
+ button = find("##{option_id} button")
+ button.send_keys(:enter)
+
+ assert_selector "##{option_id}[aria-selected='true']"
+ assert page.evaluate_script("document.activeElement.closest('##{option_id}') !== null"),
+ "focus should remain on the toggled option"
+ end
+
test "can select and deselect entire page of transactions" do
all_transactions_checkbox.check
assert_selection_count(number_of_transactions_on_page)
diff --git a/test/views/transactions/tag_summary_view_test.rb b/test/views/transactions/tag_summary_view_test.rb
new file mode 100644
index 0000000000..77d8f60aaf
--- /dev/null
+++ b/test/views/transactions/tag_summary_view_test.rb
@@ -0,0 +1,182 @@
+require "test_helper"
+
+class Transactions::TagSummaryViewTest < ActionView::TestCase
+ setup do
+ @family = families(:dylan_family)
+ @user = users(:family_admin)
+ Current.session = Session.create!(user: @user)
+
+ @accessible_account_ids = @user.accessible_accounts.pluck(:id).to_set
+ @split_parent_entry_ids = Set.new
+
+ @entry = entries(:transaction)
+ @transaction = @entry.transaction
+ end
+
+ test "untagged row renders only the hover add affordance" do
+ @transaction.update!(tag_ids: [])
+
+ summary = render_summary
+
+ assert_equal I18n.t("tags.summary.add"), summary.at_css("[role=tooltip]").text.strip
+ assert_empty summary.css("[title]")
+ assert_select summary, "[data-tag-initial]", count: 0
+ end
+
+ test "single tag renders as a full pill" do
+ tag = create_tags(1).first
+ @transaction.update!(tag_ids: [ tag.id ])
+
+ summary = render_summary
+
+ assert_includes summary.at_css("[aria-describedby]").text, tag.name
+ assert_select summary, "[aria-describedby] [data-tag-initial]", count: 0
+ end
+
+ test "single tag shows its full name on hover" do
+ tag = @family.tags.create!(name: "A very long reimbursable business trip tag", color: Tag::COLORS.first)
+ @transaction.update!(tag_ids: [ tag.id ])
+
+ summary = render_summary
+
+ assert_equal [ tag.name ], summary.css("[data-tag-summary-tooltip] li span.truncate").map { |node| node.text.strip }
+ assert_empty summary.css("[aria-describedby] [title]").map { |node| node["title"] }.reject(&:blank?)
+ end
+
+ test "two or three tags render as letter badges with every tag in the tooltip" do
+ tags = create_tags(3)
+ @transaction.update!(tag_ids: tags.map(&:id))
+
+ summary = render_summary
+ tooltip = summary.at_css("[role=tooltip]")
+
+ assert_equal 3, summary.css("[aria-describedby] [data-tag-initial]").size
+ assert_equal tags.map { |tag| tag.name.first }, summary.css("[aria-describedby] [data-tag-initial]").map { |node| node.text.strip }
+ assert_not_includes summary.at_css("[aria-describedby]").text, "+"
+ assert_equal tags.map(&:name), tooltip.css("[data-tag-summary-tooltip] li span.truncate").map { |node| node.text.strip }
+ end
+
+ test "several tags render full pills and the compact summary for the fit controller" do
+ tags = create_tags(3)
+ @transaction.update!(tag_ids: tags.map(&:id))
+
+ fit = render_summary.at_css("[data-controller=tag-fit]")
+
+ assert_equal tags.map(&:name), fit.css("[data-tag-fit-target=full] > span > span").map { |node| node.text.strip }
+ assert fit.at_css("[data-tag-fit-target=full]").key?("hidden"), "full pills start hidden until measured"
+ assert_not fit.at_css("[data-tag-fit-target=compact]").key?("hidden")
+ end
+
+ test "more than three tags render two letter badges and an overflow count" do
+ tags = create_tags(5)
+ @transaction.update!(tag_ids: tags.map(&:id))
+
+ summary = render_summary
+
+ assert_equal 2, summary.css("[aria-describedby] [data-tag-initial]").size
+ assert_includes summary.at_css("[aria-describedby]").text, "+3"
+ assert_equal 5, summary.css("[data-tag-summary-tooltip] li").size
+ end
+
+ test "mobile shows tappable tag pills after the title" do
+ tags = create_tags(2)
+ @transaction.update!(tag_ids: tags.map(&:id))
+
+ html = Nokogiri::HTML.fragment(render_row)
+ line = html.at_css("[data-tag-fit-bounds='0.5']")
+ mobile = line.at_css("##{dom_id(@transaction, "tag_summary_mobile")}")
+
+ assert mobile, "mobile tags sit on the title line"
+ assert mobile.ancestors("button").any?, "mobile tags open the tag picker"
+ assert_equal tags.map(&:name), mobile.css("[data-tag-fit-target=full] > span > span").map { |node| node.text.strip }
+ end
+
+ test "untagged rows keep a hidden, empty mobile target for the first tag" do
+ @transaction.update!(tag_ids: [])
+
+ html = Nokogiri::HTML.fragment(render_row)
+ mobile = html.at_css("##{dom_id(@transaction, "tag_summary_mobile")}")
+
+ assert mobile, "the toggle stream needs a mobile target to replace"
+ assert mobile.key?("data-tag-empty")
+ assert_nil mobile.at_css("[role=tooltip]"), "no add affordance on touch"
+ assert_includes mobile.ancestors("div").map { |node| node["class"].to_s }.join(" "), "has-[[data-tag-empty]]:hidden"
+ end
+
+ test "read-only shares get a focusable summary instead of the picker" do
+ sign_in_as_family_member
+ entry = entries(:transfer_in) # credit card, shared read-only
+ entry.transaction.update!(tag_ids: [ tags(:one).id, tags(:two).id ])
+
+ html = Nokogiri::HTML.fragment(render(partial: "transactions/transaction", locals: { entry: entry.reload, balance_trend: nil, view_ctx: "global" }))
+ summary = html.at_css("##{dom_id(entry.transaction, "tag_summary_desktop")}")
+
+ assert_nil html.at_css("turbo-frame#tag_dropdown")
+ assert summary.at_css("[aria-describedby] [tabindex='0']"), "read-only trigger should be keyboard focusable"
+ end
+
+ test "editable rows keep the picker and add no extra tab stop" do
+ tags = create_tags(2)
+ @transaction.update!(tag_ids: tags.map(&:id))
+
+ html = Nokogiri::HTML.fragment(render_row)
+
+ assert html.at_css("turbo-frame#tag_dropdown")
+ assert_nil html.at_css("##{dom_id(@transaction, "tag_summary_desktop")} [tabindex='0']")
+ end
+
+ test "transfer rows show read-only tag pills on mobile" do
+ outflow_tx = Transaction.create!(kind: "funds_movement")
+ outflow_entry = Entry.create!(
+ account: accounts(:depository), entryable: outflow_tx,
+ name: "Transfer out", amount: 100, currency: "USD", date: Date.today
+ )
+ tags = create_tags(2)
+ outflow_tx.update!(tag_ids: tags.map(&:id))
+
+ html = Nokogiri::HTML.fragment(render(partial: "transactions/transaction", locals: { entry: outflow_entry.reload, balance_trend: nil, view_ctx: "global" }))
+ mobile = html.at_css("##{dom_id(outflow_tx, "tag_summary_mobile")}")
+
+ assert_equal tags.map(&:name), mobile.css("[data-tag-fit-target=full] > span > span").map { |node| node.text.strip }
+ assert_empty mobile.ancestors("button")
+ end
+
+ test "transfer rows render tags read-only" do
+ outflow_tx = Transaction.create!(kind: "funds_movement")
+ outflow_entry = Entry.create!(
+ account: accounts(:depository), entryable: outflow_tx,
+ name: "Transfer out", amount: 100, currency: "USD", date: Date.today
+ )
+ inflow_tx = Transaction.create!(kind: "funds_movement")
+ Entry.create!(
+ account: accounts(:credit_card), entryable: inflow_tx,
+ name: "Transfer in", amount: -100, currency: "USD", date: Date.today
+ )
+ Transfer.create!(inflow_transaction: inflow_tx, outflow_transaction: outflow_tx, status: "confirmed")
+
+ html = Nokogiri::HTML.fragment(render(partial: "transactions/transaction", locals: { entry: outflow_entry, balance_trend: nil, view_ctx: "global" }))
+
+ assert html.at_css("##{dom_id(outflow_tx, "tag_summary_desktop")}")
+ assert_nil html.at_css("turbo-frame#tag_dropdown")
+ assert_empty html.at_css("##{dom_id(outflow_tx, "tag_summary_desktop")}").text.strip
+ end
+
+ private
+ def sign_in_as_family_member
+ @user = users(:family_member)
+ Current.session = Session.create!(user: @user)
+ @accessible_account_ids = @user.accessible_accounts.pluck(:id).to_set
+ end
+
+ def render_row
+ render(partial: "transactions/transaction", locals: { entry: @entry.reload, balance_trend: nil, view_ctx: "global" })
+ end
+
+ def render_summary
+ Nokogiri::HTML.fragment(render_row).at_css("##{dom_id(@transaction, "tag_summary_desktop")}")
+ end
+
+ def create_tags(count)
+ Array.new(count) { |i| @family.tags.create!(name: "#{("A".ord + i).chr}lpha tag #{i}", color: Tag::COLORS[i]) }
+ end
+end