diff --git a/app/models/account/entry.rb b/app/models/account/entry.rb index 4e80319f..a72bb1ec 100644 --- a/app/models/account/entry.rb +++ b/app/models/account/entry.rb @@ -11,6 +11,7 @@ class Account::Entry < ApplicationRecord validates :date, :amount, :currency, presence: true validates :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 < ApplicationRecord end class << 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 currency select( diff --git a/app/views/account/entries/entryables/valuation/_form.html.erb b/app/views/account/entries/entryables/valuation/_form.html.erb index 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") %>
- <%= 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.erb index 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" %>
diff --git a/app/views/transactions/_form.html.erb b/app/views/transactions/_form.html.erb index 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 %>
diff --git a/test/models/account/entry_test.rb b/test/models/account/entry_test.rb index 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 :transaction end + 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" do existing_valuation = account_entries :valuation diff --git a/test/system/accounts_test.rb b/test/system/accounts_test.rb index e93f9658..f4504ab3 100644 --- a/test/system/accounts_test.rb +++ b/test/system/accounts_test.rb @@ -60,7 +60,7 @@ class AccountsTest < ApplicationSystemTestCase select "Chase", from: "Financial institution" fill_in "account[balance]", with: 100.99 check "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_date fill_in "Start balance (optional)", with: 95 click_button "Add #{humanized_accountable(accountable_type).downcase}"