Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Add presence validations for required fields (#545)

Dave Corson-Knowlese5750d1a135a
diff --git a/app/models/account.rb b/app/models/account.rb
index a882cf5334..5c77a16c33 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 024cc30d27..9915ddde73 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 6250c6bdc1..5f5827c4d6 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 b3ecf6b417..43ea648030 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 042e4edd78..c6c53f2b53 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 34bbeb530d..70c520bf53 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) }