Files
sure/app/models/tag.rb
T
e3220de2e4 feat(transactions): add "No merchant"/"Untagged" filter options (#3135)
* feat(transactions): add "No merchant"/"Untagged" filter options

Extends the existing "Uncategorized" filter pattern to the merchant
and tag filters on the transactions page, closing discussions #3118
and #3117.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(transactions): avoid PG DISTINCT/ORDER error and name collisions in No merchant/Untagged filters

- Replace the top-level .distinct on the Untagged branch with an id
  subquery, since PostgreSQL rejects a DISTINCT select combined with
  reverse_chronological's CASE-expression ORDER BY unless that
  expression is also in the select list (PG::InvalidColumnReference).
- Switch both filters from matching on the localized display name to a
  stable, non-localized sentinel value (Merchant::NO_MERCHANT_FILTER_VALUE,
  Tag::UNTAGGED_FILTER_VALUE), so a real merchant/tag that happens to be
  named "No merchant"/"Untagged" (or a translation of either) can no
  longer be misdetected as the synthetic filter option. This also drops
  the per-locale I18n lookup previously needed for locale-safe detection.
- Add a badge special case so the filter chip still shows the
  translated label instead of the raw sentinel.

Addresses review feedback from chatgpt-codex-connector and
coderabbitai on PR #3135.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(transactions): reserve sentinel filter values and move value selection into models

- Merchant/Tag now reject a name equal to their own sentinel value
  (NO_MERCHANT_FILTER_VALUE / UNTAGGED_FILTER_VALUE), closing the
  remaining collision where a merchant or tag literally named
  "__no_merchant__"/"__untagged__" would be misdetected as the
  synthetic filter option.
- Added Merchant#filter_value / Tag#filter_value so the
  persisted-vs-synthetic checkbox value is computed in the model
  instead of the view template, per CodeRabbit's nitpick and this
  repo's "domain logic out of views" convention.

Addresses further review feedback from chatgpt-codex-connector and
coderabbitai on PR #3135.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(transactions): scope the Untagged id subquery to the current family

Transaction.left_joins(:tags) queried across every family's
transactions/taggings/tags before being intersected with the
family-scoped outer query. Functionally correct (the outer query still
restricted results to the right family), but it meant every request
selecting "Untagged" ran a join across the whole platform's data
instead of just the current family's, unlike every other filter in
this file. Use family.transactions.left_joins(:tags) instead.

Reported by jjmata on PR #3135.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(transactions): use token-backed fallback color for No merchant icon

Merchant.no_merchant hardcoded #737373 into DS::FilledIcon instead of
letting its token-backed default (var(--color-gray-500)) apply, bypassing
theme changes.

---------

Co-authored-by: Gerald <248542187+gfr-free@users.noreply.github.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-08 09:06:27 +02:00

55 lines
1.8 KiB
Ruby

class Tag < ApplicationRecord
COLORS = %w[#e99537 #4da568 #6471eb #db5a54 #df4e92 #c44fe9 #eb5429 #61c9ea #805dee #6ad28a]
UNCATEGORIZED_COLOR = "#737373"
# Tag name key for i18n
UNTAGGED_NAME_KEY = "models.tag.untagged"
# Stable, non-localized filter value for the synthetic "Untagged" option.
# Using an opaque sentinel (rather than the translated display name) means a real
# tag can never collide with it, regardless of name or locale.
UNTAGGED_FILTER_VALUE = "__untagged__"
belongs_to :family
has_many :taggings, dependent: :destroy
has_many :transactions, through: :taggings, source: :taggable, source_type: "Transaction"
has_many :import_mappings, as: :mappable, dependent: :destroy, class_name: "Import::Mapping"
validates :name, presence: true, uniqueness: { scope: :family }
validates :name, exclusion: { in: [ UNTAGGED_FILTER_VALUE ] }
validates :color, format: { with: /\A#[0-9A-Fa-f]{6}\z/ }, allow_nil: true
scope :alphabetically, -> { order(:name, :id) }
class << self
def untagged
new(name: I18n.t(UNTAGGED_NAME_KEY), color: UNCATEGORIZED_COLOR)
end
# Helper to get the localized name for "Untagged"
def untagged_name
I18n.t(UNTAGGED_NAME_KEY)
end
end
# The value the transactions-filter checkbox submits for this tag: the
# persisted name for a real tag, or the stable sentinel for the synthetic
# "Untagged" pseudo-tag returned by .untagged.
def filter_value
persisted? ? name : UNTAGGED_FILTER_VALUE
end
def replace_and_destroy!(replacement)
transaction do
raise ActiveRecord::RecordInvalid, "Replacement tag cannot be the same as the tag being destroyed" if replacement == self
if replacement
taggings.update_all tag_id: replacement.id
end
destroy!
end
end
end