From e5750d1a135ac3e66f3195b6404019d5b17f09f0 Mon Sep 17 00:00:00 2001 From: Dave Corson-Knowles Date: Fri, 15 Mar 2024 12:21:59 -0700 Subject: [PATCH] Add presence validations for required fields (#545) * Adds basic validations for required fields Also deletes a few extraneous .keep files Does not add the family_id required field for user, since that breaks the basic test setup * Restore keep files to this branch * Remove Credit model and validate models behind ids * Restore concerns .keep --- app/models/account.rb | 2 ++ app/models/account_balance.rb | 2 ++ app/models/exchange_rate.rb | 2 ++ app/models/transaction.rb | 2 ++ app/models/transaction/category.rb | 2 ++ app/models/valuation.rb | 2 ++ 6 files changed, 12 insertions(+) diff --git a/app/models/account.rb b/app/models/account.rb index a882cf53..5c77a16c 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,6 +1,8 @@ class Account < ApplicationRecord include Syncable + validates :family, presence: true + broadcasts_refreshes belongs_to :family has_many :balances, class_name: "AccountBalance" diff --git a/app/models/account_balance.rb b/app/models/account_balance.rb index 024cc30d..9915ddde 100644 --- a/app/models/account_balance.rb +++ b/app/models/account_balance.rb @@ -1,6 +1,8 @@ class AccountBalance < ApplicationRecord belongs_to :account + validates :account, :date, :balance, presence: true + scope :in_period, ->(period) { period.date_range.nil? ? all : where(date: period.date_range) } def self.to_series(account, period = Period.all) diff --git a/app/models/exchange_rate.rb b/app/models/exchange_rate.rb index 6250c6bd..5f5827c4 100644 --- a/app/models/exchange_rate.rb +++ b/app/models/exchange_rate.rb @@ -1,4 +1,6 @@ class ExchangeRate < ApplicationRecord + validates :base_currency, :converted_currency, presence: true + def self.convert(from, to, amount) return amount unless EXCHANGE_RATE_ENABLED diff --git a/app/models/transaction.rb b/app/models/transaction.rb index b3ecf6b4..43ea6480 100644 --- a/app/models/transaction.rb +++ b/app/models/transaction.rb @@ -2,6 +2,8 @@ class Transaction < ApplicationRecord belongs_to :account belongs_to :category, optional: true + validates :name, :date, :amount, :account, presence: true + after_commit :sync_account def self.ransackable_attributes(auth_object = nil) diff --git a/app/models/transaction/category.rb b/app/models/transaction/category.rb index 042e4edd..c6c53f2b 100644 --- a/app/models/transaction/category.rb +++ b/app/models/transaction/category.rb @@ -2,6 +2,8 @@ class Transaction::Category < ApplicationRecord has_many :transactions belongs_to :family + validates :name, :color, :family, presence: true + before_update :clear_internal_category, if: :name_changed? DEFAULT_CATEGORIES = [ diff --git a/app/models/valuation.rb b/app/models/valuation.rb index 34bbeb53..70c520bf 100644 --- a/app/models/valuation.rb +++ b/app/models/valuation.rb @@ -1,6 +1,8 @@ class Valuation < ApplicationRecord belongs_to :account + validates :account, :date, :value, presence: true + after_commit :sync_account scope :in_period, ->(period) { period.date_range.nil? ? all : where(date: period.date_range) }