mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Add backend support for transaction categories (#524)
* Add backend support for transaction categories * Fix tests * Localize default category names * Add tests * Remove category icon and set default color
This commit is contained in:
parent
ad7136cb63
commit
90d0cc0c39
14 changed files with 156 additions and 3 deletions
|
@ -2,6 +2,7 @@ class Family < ApplicationRecord
|
|||
has_many :users, dependent: :destroy
|
||||
has_many :accounts, dependent: :destroy
|
||||
has_many :transactions, through: :accounts
|
||||
has_many :transaction_categories, dependent: :destroy, class_name: "Transaction::Category"
|
||||
|
||||
def net_worth
|
||||
accounts.active.sum("CASE WHEN classification = 'asset' THEN balance ELSE -balance END")
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
class Transaction < ApplicationRecord
|
||||
belongs_to :account
|
||||
belongs_to :category, optional: true
|
||||
|
||||
after_commit :sync_account
|
||||
|
||||
|
|
38
app/models/transaction/category.rb
Normal file
38
app/models/transaction/category.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
class Transaction::Category < ApplicationRecord
|
||||
has_many :transactions
|
||||
belongs_to :family
|
||||
|
||||
before_update :clear_internal_category, if: :name_changed?
|
||||
|
||||
DEFAULT_CATEGORIES = [
|
||||
{ internal_category: "income", color: "#fd7f6f" },
|
||||
{ internal_category: "food_and_drink", color: "#7eb0d5" },
|
||||
{ internal_category: "entertainment", color: "#b2e061" },
|
||||
{ internal_category: "personal_care", color: "#bd7ebe" },
|
||||
{ internal_category: "general_services", color: "#ffb55a" },
|
||||
{ internal_category: "auto_and_transport", color: "#ffee65" },
|
||||
{ internal_category: "rent_and_utilities", color: "#beb9db" },
|
||||
{ internal_category: "home_improvement", color: "#fdcce5" }
|
||||
]
|
||||
|
||||
def self.create_default_categories(family)
|
||||
if family.transaction_categories.size > 0
|
||||
raise ArgumentError, "Family already has some categories"
|
||||
end
|
||||
|
||||
family_id = family.id
|
||||
categories = self::DEFAULT_CATEGORIES.map { |c| {
|
||||
name: I18n.t("transaction.default_category.#{c[:internal_category]}"),
|
||||
internal_category: c[:internal_category],
|
||||
color: c[:color],
|
||||
family_id:
|
||||
} }
|
||||
self.insert_all(categories)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def clear_internal_category
|
||||
self.internal_category = nil
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue