2024-03-07 19:15:50 +01:00
|
|
|
class Transaction::Category < ApplicationRecord
|
2024-04-04 17:29:50 -04:00
|
|
|
has_many :transactions, dependent: :nullify
|
2024-03-07 19:15:50 +01:00
|
|
|
belongs_to :family
|
|
|
|
|
2024-03-15 12:21:59 -07:00
|
|
|
validates :name, :color, :family, presence: true
|
|
|
|
|
2024-03-07 19:15:50 +01:00
|
|
|
before_update :clear_internal_category, if: :name_changed?
|
|
|
|
|
2024-04-04 17:29:50 -04:00
|
|
|
COLORS = %w[#e99537 #4da568 #6471eb #db5a54 #df4e92 #c44fe9 #eb5429 #61c9ea #805dee #6ad28a]
|
|
|
|
|
|
|
|
UNCATEGORIZED_COLOR = "#737373"
|
|
|
|
|
2024-03-07 19:15:50 +01:00
|
|
|
DEFAULT_CATEGORIES = [
|
2024-04-04 17:29:50 -04:00
|
|
|
{ internal_category: "income", color: COLORS[0] },
|
|
|
|
{ internal_category: "food_and_drink", color: COLORS[1] },
|
|
|
|
{ internal_category: "entertainment", color: COLORS[2] },
|
|
|
|
{ internal_category: "personal_care", color: COLORS[3] },
|
|
|
|
{ internal_category: "general_services", color: COLORS[4] },
|
|
|
|
{ internal_category: "auto_and_transport", color: COLORS[5] },
|
|
|
|
{ internal_category: "rent_and_utilities", color: COLORS[6] },
|
|
|
|
{ internal_category: "home_improvement", color: COLORS[7] }
|
2024-03-07 19:15:50 +01:00
|
|
|
]
|
|
|
|
|
2024-03-11 14:51:16 +02:00
|
|
|
def self.ransackable_attributes(auth_object = nil)
|
2024-04-02 18:17:26 +02:00
|
|
|
%w[name id]
|
2024-03-11 14:51:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.ransackable_associations(auth_object = nil)
|
|
|
|
%w[]
|
|
|
|
end
|
|
|
|
|
2024-03-07 19:15:50 +01:00
|
|
|
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
|