Files
sure/app/helpers/transactions_helper.rb
CrossDrain 0b7fa732ae feat(splits): add exclusion support for splits and improve rendering (#1661)
* feat(splits): add excluded attribute support for split children and improve rendering of split transactions

* address coderabbitai suggestions to improve code quality

* Fix split excluded coercion, DRY helpers, and clean up view partials

Fix boolean coercion bug where string "false" from form params was
truthy in Ruby, causing all split children to be marked excluded.
Use ActiveModel::Type::Boolean for explicit casting in Entry#split!.

Additional changes addressing code review feedback:

- Extract duplicated in_split_group logic from TransactionsController
  and TransactionCategoriesController into TransactionsHelper
- Remove redundant local_assigns.fetch calls in partials that already
  declare defaults via the Rails 7.1 locals: magic comment
- Simplify ternary in _transaction.html.erb to pass grouped directly
- Guard hidden_field_tag :grouped to only emit when value is "true"
- Add model tests for excluded on split children (boolean and string)
- Add controller test for excluded param through full HTTP stack
- Add test confirming excluded children are dropped from balance queries

* fix(splits): simplify excluded attribute boolean check

* refactor(splits): extract truthy values constant for excluded check

Extract the array of truthy values used for excluded attribute check
into a private constant to improve code maintainability and avoid
duplication of the magic array.

* refactor: simplify split grouping link generation and add test coverage for excluded split parameters
2026-05-09 12:36:41 +02:00

84 lines
2.8 KiB
Ruby

module TransactionsHelper
def transaction_search_filters
[
{ key: "account_filter", label: t("transactions.search.filters.account"), icon: "layers" },
{ key: "date_filter", label: t("transactions.search.filters.date"), icon: "calendar" },
{ key: "type_filter", label: t("transactions.search.filters.type"), icon: "tag" },
{ key: "status_filter", label: t("transactions.search.filters.status"), icon: "clock" },
{ key: "amount_filter", label: t("transactions.search.filters.amount"), icon: "hash" },
{ key: "category_filter", label: t("transactions.search.filters.category"), icon: "shapes" },
{ key: "tag_filter", label: t("transactions.search.filters.tag"), icon: "tags" },
{ key: "merchant_filter", label: t("transactions.search.filters.merchant"), icon: "store" }
]
end
def get_transaction_search_filter_partial_path(filter)
"transactions/searches/filters/#{filter[:key]}"
end
def get_default_transaction_search_filter
transaction_search_filters[0]
end
def in_split_group?(entry, params_grouped)
entry.split_child? && Current.user.show_split_grouped? && params_grouped == "true"
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).
# Structure:
# {
# kind: :simplefin | :raw,
# simplefin: { payee:, description:, memo: },
# provider_extras: [ { key:, value:, title: } ],
# raw: String (pretty JSON or string)
# }
def build_transaction_extra_details(obj)
tx = obj.respond_to?(:transaction) ? obj.transaction : obj
return nil unless tx.respond_to?(:extra) && tx.extra.present?
extra = tx.extra
if extra.is_a?(Hash) && extra["simplefin"].present?
sf = extra["simplefin"]
simple = {
payee: sf.is_a?(Hash) ? sf["payee"].presence : nil,
description: sf.is_a?(Hash) ? sf["description"].presence : nil,
memo: sf.is_a?(Hash) ? sf["memo"].presence : nil
}.compact
extras = []
if sf.is_a?(Hash) && sf["extra"].is_a?(Hash) && sf["extra"].present?
sf["extra"].each do |k, v|
display = (v.is_a?(Hash) || v.is_a?(Array)) ? v.to_json : v
extras << {
key: k.to_s.humanize,
value: display,
title: (v.is_a?(String) ? v : display.to_s)
}
end
end
{
kind: :simplefin,
simplefin: simple,
provider_extras: extras,
raw: nil
}
else
pretty = begin
JSON.pretty_generate(extra)
rescue StandardError
extra.to_s
end
{
kind: :raw,
simplefin: {},
provider_extras: [],
raw: pretty
}
end
end
end