Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

feat(transactions): add "No merchant"/"Untagged" filter options (#3135)

GFRe3220de2e4ae
diff --git a/app/models/merchant.rb b/app/models/merchant.rb
index f3a7bc95c6..ab6f2bed31 100644
--- a/app/models/merchant.rb
+++ b/app/models/merchant.rb
@@ -1,11 +1,38 @@
class Merchant < ApplicationRecord
TYPES = %w[FamilyMerchant ProviderMerchant].freeze
+ # Merchant name key for i18n
+ NO_MERCHANT_NAME_KEY = "models.merchant.no_merchant"
+
+ # Stable, non-localized filter value for the synthetic "No merchant" option.
+ # Using an opaque sentinel (rather than the translated display name) means a real
+ # merchant can never collide with it, regardless of name or locale.
+ NO_MERCHANT_FILTER_VALUE = "__no_merchant__"
+
has_many :transactions, dependent: :nullify
has_many :recurring_transactions, dependent: :destroy
validates :name, presence: true
+ validates :name, exclusion: { in: [ NO_MERCHANT_FILTER_VALUE ] }
validates :type, inclusion: { in: TYPES }
scope :alphabetically, -> { order(:name) }
+
+ class << self
+ def no_merchant
+ new(name: I18n.t(NO_MERCHANT_NAME_KEY))
+ end
+
+ # Helper to get the localized name for "No merchant"
+ def no_merchant_name
+ I18n.t(NO_MERCHANT_NAME_KEY)
+ end
+ end
+
+ # The value the transactions-filter checkbox submits for this merchant: the
+ # persisted name for a real merchant, or the stable sentinel for the
+ # synthetic "No merchant" pseudo-merchant returned by .no_merchant.
+ def filter_value
+ persisted? ? name : NO_MERCHANT_FILTER_VALUE
+ end
end
diff --git a/app/models/tag.rb b/app/models/tag.rb
index 388dbc9809..63c9c34066 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -1,17 +1,44 @@
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) }
- COLORS = %w[#e99537 #4da568 #6471eb #db5a54 #df4e92 #c44fe9 #eb5429 #61c9ea #805dee #6ad28a]
+ class << self
+ def untagged
+ new(name: I18n.t(UNTAGGED_NAME_KEY), color: UNCATEGORIZED_COLOR)
+ end
- UNCATEGORIZED_COLOR = "#737373"
+ # 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
diff --git a/app/models/transaction/search.rb b/app/models/transaction/search.rb
index 6887579572..e61acfcfb8 100644
--- a/app/models/transaction/search.rb
+++ b/app/models/transaction/search.rb
@@ -184,7 +184,15 @@ class Transaction::Search
def apply_merchant_filter(query, merchants)
return query unless merchants.present?
- query.joins(:merchant).where(merchants: { name: merchants })
+
+ include_no_merchant = merchants.include?(Merchant::NO_MERCHANT_FILTER_VALUE)
+ real_merchants = merchants - [ Merchant::NO_MERCHANT_FILTER_VALUE ]
+
+ if include_no_merchant
+ query.left_joins(:merchant).where("merchants.name IN (?) OR merchants.id IS NULL", real_merchants)
+ else
+ query.joins(:merchant).where(merchants: { name: real_merchants })
+ end
end
# Filter transactions by tag name, matching any transaction that carries
@@ -192,12 +200,24 @@ class Transaction::Search
def apply_tag_filter(query, tags)
return query unless tags.present?
- # Use a subquery instead of an INNER JOIN: `.joins(:tags)` fans out to
+ include_untagged = tags.include?(Tag::UNTAGGED_FILTER_VALUE)
+ real_tags = tags - [ Tag::UNTAGGED_FILTER_VALUE ]
+
+ # Use a subquery instead of an INNER/LEFT JOIN: `.joins(:tags)` fans out to
# one row per matching tag, so a transaction tagged with two of the
# filtered tags produces two rows and double-counts in the summary
- # box (COUNT / SUM) even though the list renders it once.
+ # box (COUNT / SUM) even though the list renders it once. A top-level
+ # `.distinct` doesn't work either, since PostgreSQL rejects DISTINCT
+ # combined with reverse_chronological's CASE-expression ORDER BY unless
+ # that expression is also in the select list (PG::InvalidColumnReference).
+ # `query` is already scoped to the current family, so the subquery
+ # inherits that scoping too.
# See https://github.com/we-promise/sure/issues/3174
- matching_ids = query.joins(:tags).where(tags: { name: tags }).distinct.select(:id)
+ matching_ids = if include_untagged
+ query.left_joins(:tags).where("tags.name IN (?) OR tags.id IS NULL", real_tags).distinct.select(:id)
+ else
+ query.joins(:tags).where(tags: { name: real_tags }).distinct.select(:id)
+ end
query.where(id: matching_ids)
end
diff --git a/app/views/transactions/searches/filters/_badge.html.erb b/app/views/transactions/searches/filters/_badge.html.erb
index a9276ce8be..feec464f1e 100644
--- a/app/views/transactions/searches/filters/_badge.html.erb
+++ b/app/views/transactions/searches/filters/_badge.html.erb
@@ -40,6 +40,14 @@
<%= icon(param_value.downcase == "pending" ? "clock" : "check", size: "sm") %>
<p><%= t(".#{param_value.downcase}") %></p>
</div>
+ <% elsif param_key == "merchants" && param_value == Merchant::NO_MERCHANT_FILTER_VALUE %>
+ <div class="flex items-center gap-2">
+ <p><%= Merchant.no_merchant_name %></p>
+ </div>
+ <% elsif param_key == "tags" && param_value == Tag::UNTAGGED_FILTER_VALUE %>
+ <div class="flex items-center gap-2">
+ <p><%= Tag.untagged_name %></p>
+ </div>
<% else %>
<div class="flex items-center gap-2">
<p><%= param_value %></p>
diff --git a/app/views/transactions/searches/filters/_merchant_filter.html.erb b/app/views/transactions/searches/filters/_merchant_filter.html.erb
index b1f08ff081..bbf2a40e1f 100644
--- a/app/views/transactions/searches/filters/_merchant_filter.html.erb
+++ b/app/views/transactions/searches/filters/_merchant_filter.html.erb
@@ -10,17 +10,17 @@
}
) %>
<div class="my-2" id="list" data-list-filter-target="list">
- <% Current.family.assigned_merchants_for(Current.user).alphabetically.each do |merchant| %>
+ <% ([ Merchant.no_merchant ] + Current.family.assigned_merchants_for(Current.user).alphabetically).each do |merchant| %>
<div class="filterable-item flex items-center gap-2 p-2" data-filter-name="<%= merchant.name %>">
<%= form.check_box :merchants,
{
multiple: true,
- checked: @q[:merchants]&.include?(merchant.name),
+ checked: @q[:merchants]&.include?(merchant.filter_value),
class: "checkbox checkbox--light"
},
- merchant.name,
+ merchant.filter_value,
nil %>
- <%= form.label :merchants, value: merchant.name, class: "text-sm text-primary flex items-center gap-2" do %>
+ <%= form.label :merchants, value: merchant.filter_value, class: "text-sm text-primary flex items-center gap-2" do %>
<div class="hidden lg:flex">
<% if merchant.logo_url.present? %>
<%= image_tag Setting.transform_brand_fetch_url(merchant.logo_url),
diff --git a/app/views/transactions/searches/filters/_tag_filter.html.erb b/app/views/transactions/searches/filters/_tag_filter.html.erb
index 06c9a1b95a..fd6b3d1048 100644
--- a/app/views/transactions/searches/filters/_tag_filter.html.erb
+++ b/app/views/transactions/searches/filters/_tag_filter.html.erb
@@ -10,17 +10,17 @@
}
) %>
<div class="my-2" id="list" data-list-filter-target="list">
- <% Current.family.tags.alphabetically.each do |tag| %>
+ <% ([ Tag.untagged ] + Current.family.tags.alphabetically).each do |tag| %>
<div class="filterable-item flex items-center gap-2 p-2" data-filter-name="<%= tag.name %>">
<%= form.check_box :tags,
{
multiple: true,
- checked: @q[:tags]&.include?(tag.name),
+ checked: @q[:tags]&.include?(tag.filter_value),
class: "checkbox checkbox--light"
},
- tag.name,
+ tag.filter_value,
nil %>
- <%= form.label :tags, value: tag.name, class: "text-sm text-primary flex items-center gap-2" do %>
+ <%= form.label :tags, value: tag.filter_value, class: "text-sm text-primary flex items-center gap-2" do %>
<% tag_color = tag.color.presence || Tag::UNCATEGORIZED_COLOR %>
<span class="border text-sm font-medium px-3 py-1 rounded-full inline-flex items-center gap-2"
style="
diff --git a/config/locales/models/merchant/en.yml b/config/locales/models/merchant/en.yml
new file mode 100644
index 0000000000..12e51031c1
--- /dev/null
+++ b/config/locales/models/merchant/en.yml
@@ -0,0 +1,5 @@
+---
+en:
+ models:
+ merchant:
+ no_merchant: No merchant
diff --git a/config/locales/models/tag/en.yml b/config/locales/models/tag/en.yml
new file mode 100644
index 0000000000..4a498048f7
--- /dev/null
+++ b/config/locales/models/tag/en.yml
@@ -0,0 +1,5 @@
+---
+en:
+ models:
+ tag:
+ untagged: Untagged
diff --git a/test/models/merchant_test.rb b/test/models/merchant_test.rb
new file mode 100644
index 0000000000..ebb8dbc223
--- /dev/null
+++ b/test/models/merchant_test.rb
@@ -0,0 +1,15 @@
+require "test_helper"
+
+class MerchantTest < ActiveSupport::TestCase
+ test "rejects the reserved No merchant filter sentinel as a name" do
+ merchant = FamilyMerchant.new(name: Merchant::NO_MERCHANT_FILTER_VALUE, family: families(:dylan_family))
+
+ assert_not merchant.valid?
+ assert_includes merchant.errors[:name], "is reserved"
+ end
+
+ test "filter_value returns the sentinel for the synthetic No merchant merchant and the name for real merchants" do
+ assert_equal Merchant::NO_MERCHANT_FILTER_VALUE, Merchant.no_merchant.filter_value
+ assert_equal merchants(:netflix).name, merchants(:netflix).filter_value
+ end
+end
diff --git a/test/models/tag_test.rb b/test/models/tag_test.rb
index 2d4cba2dd3..da19faf9f8 100644
--- a/test/models/tag_test.rb
+++ b/test/models/tag_test.rb
@@ -15,4 +15,16 @@ class TagTest < ActiveSupport::TestCase
assert_not_includes txn.tags, old_tag
end
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")
+
+ assert_not tag.valid?
+ assert_includes tag.errors[:name], "is reserved"
+ end
+
+ test "filter_value returns the sentinel for the synthetic Untagged tag and the name for real tags" do
+ assert_equal Tag::UNTAGGED_FILTER_VALUE, Tag.untagged.filter_value
+ assert_equal tags(:one).name, tags(:one).filter_value
+ end
end
diff --git a/test/models/transaction/search_test.rb b/test/models/transaction/search_test.rb
index dcdfe6b8e7..a742679d7d 100644
--- a/test/models/transaction/search_test.rb
+++ b/test/models/transaction/search_test.rb
@@ -626,6 +626,234 @@ class Transaction::SearchTest < ActiveSupport::TestCase
end
end
+ test "filtering for only No merchant returns only transactions without a merchant" do
+ with_merchant = create_transaction(
+ account: @checking_account,
+ amount: 100,
+ merchant: merchants(:netflix),
+ kind: "standard"
+ )
+
+ without_merchant = create_transaction(
+ account: @checking_account,
+ amount: 200,
+ kind: "standard"
+ )
+
+ results = Transaction::Search.new(@family, filters: { merchants: [ Merchant::NO_MERCHANT_FILTER_VALUE ] }).transactions_scope
+ result_ids = results.pluck(:id)
+
+ assert_includes result_ids, without_merchant.entryable.id
+ assert_not_includes result_ids, with_merchant.entryable.id
+ end
+
+ test "filtering for No merchant plus a real merchant returns both" do
+ with_netflix = create_transaction(
+ account: @checking_account,
+ amount: 100,
+ merchant: merchants(:netflix),
+ kind: "standard"
+ )
+
+ with_amazon = create_transaction(
+ account: @checking_account,
+ amount: 150,
+ merchant: merchants(:amazon),
+ kind: "standard"
+ )
+
+ without_merchant = create_transaction(
+ account: @checking_account,
+ amount: 200,
+ kind: "standard"
+ )
+
+ results = Transaction::Search.new(
+ @family,
+ filters: { merchants: [ merchants(:netflix).name, Merchant::NO_MERCHANT_FILTER_VALUE ] }
+ ).transactions_scope
+ result_ids = results.pluck(:id)
+
+ assert_includes result_ids, with_netflix.entryable.id
+ assert_includes result_ids, without_merchant.entryable.id
+ assert_not_includes result_ids, with_amazon.entryable.id
+ end
+
+ test "filtering excludes transactions without a merchant when No merchant is not in the filter" do
+ with_merchant = create_transaction(
+ account: @checking_account,
+ amount: 100,
+ merchant: merchants(:netflix),
+ kind: "standard"
+ )
+
+ without_merchant = create_transaction(
+ account: @checking_account,
+ amount: 200,
+ kind: "standard"
+ )
+
+ results = Transaction::Search.new(@family, filters: { merchants: [ merchants(:netflix).name ] }).transactions_scope
+ result_ids = results.pluck(:id)
+
+ assert_includes result_ids, with_merchant.entryable.id
+ assert_not_includes result_ids, without_merchant.entryable.id
+ end
+
+ test "filtering for only Untagged returns only transactions without tags" do
+ with_tag = create_transaction(
+ account: @checking_account,
+ amount: 100,
+ tags: [ tags(:one) ],
+ kind: "standard"
+ )
+
+ without_tag = create_transaction(
+ account: @checking_account,
+ amount: 200,
+ kind: "standard"
+ )
+
+ results = Transaction::Search.new(@family, filters: { tags: [ Tag::UNTAGGED_FILTER_VALUE ] }).transactions_scope
+ result_ids = results.pluck(:id)
+
+ assert_includes result_ids, without_tag.entryable.id
+ assert_not_includes result_ids, with_tag.entryable.id
+ end
+
+ test "filtering for Untagged plus a real tag returns both" do
+ with_trips_tag = create_transaction(
+ account: @checking_account,
+ amount: 100,
+ tags: [ tags(:one) ],
+ kind: "standard"
+ )
+
+ with_other_tag = create_transaction(
+ account: @checking_account,
+ amount: 150,
+ tags: [ tags(:two) ],
+ kind: "standard"
+ )
+
+ without_tag = create_transaction(
+ account: @checking_account,
+ amount: 200,
+ kind: "standard"
+ )
+
+ results = Transaction::Search.new(
+ @family,
+ filters: { tags: [ tags(:one).name, Tag::UNTAGGED_FILTER_VALUE ] }
+ ).transactions_scope
+ result_ids = results.pluck(:id)
+
+ assert_includes result_ids, with_trips_tag.entryable.id
+ assert_includes result_ids, without_tag.entryable.id
+ assert_not_includes result_ids, with_other_tag.entryable.id
+ end
+
+ test "filtering excludes transactions without tags when Untagged is not in the filter" do
+ with_tag = create_transaction(
+ account: @checking_account,
+ amount: 100,
+ tags: [ tags(:one) ],
+ kind: "standard"
+ )
+
+ without_tag = create_transaction(
+ account: @checking_account,
+ amount: 200,
+ kind: "standard"
+ )
+
+ results = Transaction::Search.new(@family, filters: { tags: [ tags(:one).name ] }).transactions_scope
+ result_ids = results.pluck(:id)
+
+ assert_includes result_ids, with_tag.entryable.id
+ assert_not_includes result_ids, without_tag.entryable.id
+ end
+
+ test "a real merchant literally named like the localized No merchant label remains selectable" do
+ # Regression test: filter values are opaque sentinels (Merchant::NO_MERCHANT_FILTER_VALUE), not the
+ # translated display name, so a merchant a user happens to name "No merchant" can't be misdetected
+ # as the synthetic filter option.
+ lookalike_merchant = @family.merchants.create!(
+ name: Merchant.no_merchant_name,
+ color: "#123456"
+ )
+
+ lookalike_transaction = create_transaction(
+ account: @checking_account,
+ amount: 100,
+ merchant: lookalike_merchant,
+ kind: "standard"
+ )
+
+ without_merchant = create_transaction(
+ account: @checking_account,
+ amount: 200,
+ kind: "standard"
+ )
+
+ results = Transaction::Search.new(@family, filters: { merchants: [ lookalike_merchant.name ] }).transactions_scope
+ result_ids = results.pluck(:id)
+
+ assert_includes result_ids, lookalike_transaction.entryable.id
+ assert_not_includes result_ids, without_merchant.entryable.id
+ end
+
+ test "a real tag literally named like the localized Untagged label remains selectable" do
+ lookalike_tag = @family.tags.create!(name: Tag.untagged_name)
+
+ lookalike_transaction = create_transaction(
+ account: @checking_account,
+ amount: 100,
+ tags: [ lookalike_tag ],
+ kind: "standard"
+ )
+
+ without_tag = create_transaction(
+ account: @checking_account,
+ amount: 200,
+ kind: "standard"
+ )
+
+ results = Transaction::Search.new(@family, filters: { tags: [ lookalike_tag.name ] }).transactions_scope
+ result_ids = results.pluck(:id)
+
+ assert_includes result_ids, lookalike_transaction.entryable.id
+ assert_not_includes result_ids, without_tag.entryable.id
+ end
+
+ test "Untagged filter works with controller-style reverse_chronological ordering" do
+ # Regression test for a PostgreSQL error: a top-level `.distinct` combined with `reverse_chronological`'s
+ # CASE-expression ORDER BY raises PG::InvalidColumnReference, since DISTINCT requires every ORDER BY
+ # expression to appear in the select list. apply_tag_filter must not add one.
+ with_tag = create_transaction(
+ account: @checking_account,
+ amount: 100,
+ tags: [ tags(:one), tags(:two) ],
+ kind: "standard"
+ )
+
+ without_tag = create_transaction(
+ account: @checking_account,
+ amount: 200,
+ kind: "standard"
+ )
+
+ results = Transaction::Search.new(@family, filters: { tags: [ Tag::UNTAGGED_FILTER_VALUE ] }).transactions_scope
+
+ assert_nothing_raised do
+ results.reverse_chronological.to_a
+ end
+
+ result_ids = results.pluck(:id)
+ assert_includes result_ids, without_tag.entryable.id
+ assert_not_includes result_ids, with_tag.entryable.id
+ end
+
test "empty accessible_account_ids yields no visible transactions" do
create_transaction(account: @checking_account, amount: 100)