The personal finance app for everyone
Set minimum supported date for account entries (#1023)
Zach Gollwitzer76dd5e57fb7f
diff --git a/app/models/account/entry.rb b/app/models/account/entry.rbindex 4e80319f..a72bb1ec 100644--- a/app/models/account/entry.rb+++ b/app/models/account/entry.rb@@ -11,6 +11,7 @@ class Account::Entry < ApplicationRecordvalidates :date, :amount, :currency, presence: truevalidates :date, uniqueness: { scope: [ :account_id, :entryable_type ] }, if: -> { account_valuation? }+ validates :date, comparison: { greater_than: -> { min_supported_date } }validate :trade_valid?, if: -> { account_trade? }scope :chronological, -> { order(:date, :created_at) }@@ -64,6 +65,11 @@ class Account::Entry < ApplicationRecordendclass << self+ # arbitrary cutoff date to avoid expensive sync operations+ def min_supported_date+ 10.years.ago.to_date+ end+def daily_totals(entries, currency, period: Period.last_30_days)# Sum spending and income for each day in the period with the given currencyselect(diff --git a/app/views/account/entries/entryables/valuation/_form.html.erb b/app/views/account/entries/entryables/valuation/_form.html.erbindex c7b3abff..3d3f1025 100644--- a/app/views/account/entries/entryables/valuation/_form.html.erb+++ b/app/views/account/entries/entryables/valuation/_form.html.erb@@ -8,7 +8,7 @@<%= lucide_icon("pencil-line", class: "w-4 h-4 text-gray-500") %></div><div class="w-full flex items-center justify-between gap-2">- <%= f.date_field :date, required: "required", max: Date.current, class: "border border-alpha-black-200 bg-white rounded-lg shadow-xs min-w-[200px] px-3 py-1.5 text-gray-900 text-sm" %>+ <%= f.date_field :date, required: "required", min: Account::Entry.min_supported_date, max: Date.current, class: "border border-alpha-black-200 bg-white rounded-lg shadow-xs min-w-[200px] px-3 py-1.5 text-gray-900 text-sm" %><%= f.number_field :amount, required: "required", placeholder: "0.00", step: "0.01", class: "bg-white border border-alpha-black-200 rounded-lg shadow-xs text-gray-900 text-sm px-3 py-1.5 text-right" %><%= f.hidden_field :currency, value: entry.account.currency %><%= f.hidden_field :entryable_type, value: entry.entryable_type %>diff --git a/app/views/accounts/new.html.erb b/app/views/accounts/new.html.erbindex ff011fc7..cdcf59bf 100644--- a/app/views/accounts/new.html.erb+++ b/app/views/accounts/new.html.erb@@ -86,7 +86,7 @@<%= label_tag :add_start_values, t(".optional_start_balance_message"), class: "pl-1 text-sm text-gray-500" %><div class="hidden peer-checked:flex items-center gap-2 mt-3 mb-6">- <div class="w-1/2"><%= f.date_field :start_date, label: t(".start_date"), max: Date.yesterday %></div>+ <div class="w-1/2"><%= f.date_field :start_date, label: t(".start_date"), max: Date.yesterday, min: Account::Entry.min_supported_date %></div><div class="w-1/2"><%= f.number_field :start_balance, label: t(".start_balance") %></div></div></div>diff --git a/app/views/transactions/_form.html.erb b/app/views/transactions/_form.html.erbindex c474e676..ac8a9ab4 100644--- a/app/views/transactions/_form.html.erb+++ b/app/views/transactions/_form.html.erb@@ -18,7 +18,7 @@<%= f.fields_for :entryable do |ef| %><%= ef.collection_select :category_id, Current.family.categories.alphabetically, :id, :name, { prompt: t(".category_prompt"), label: t(".category") } %><% end %>- <%= f.date_field :date, label: t(".date"), required: true, max: Date.today %>+ <%= f.date_field :date, label: t(".date"), required: true, min: Account::Entry.min_supported_date, max: Date.today %></section><section>diff --git a/test/models/account/entry_test.rb b/test/models/account/entry_test.rbindex 0beab68b..a6387936 100644--- a/test/models/account/entry_test.rb+++ b/test/models/account/entry_test.rb@@ -7,6 +7,12 @@ class Account::EntryTest < ActiveSupport::TestCase@entry = account_entries :transactionend+ test "entry cannot be older than 10 years ago" do+ assert_raises ActiveRecord::RecordInvalid do+ @entry.update! date: 50.years.ago.to_date+ end+ end+test "valuations cannot have more than one entry per day" doexisting_valuation = account_entries :valuationdiff --git a/test/system/accounts_test.rb b/test/system/accounts_test.rbindex e93f9658..f4504ab3 100644--- a/test/system/accounts_test.rb+++ b/test/system/accounts_test.rb@@ -60,7 +60,7 @@ class AccountsTest < ApplicationSystemTestCaseselect "Chase", from: "Financial institution"fill_in "account[balance]", with: 100.99check "Add a start balance for this account"- fill_in "Start date (optional)", with: 10.days.ago.to_date.to_s+ fill_in "Start date (optional)", with: 10.days.ago.to_datefill_in "Start balance (optional)", with: 95click_button "Add #{humanized_accountable(accountable_type).downcase}"