1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-29 18:19:39 +02:00

Set minimum supported date for account entries (#1023)

* Set minimum supported date for account entries

* Fix validation proc

* Fix date input in system tests
This commit is contained in:
Zach Gollwitzer 2024-07-26 10:47:27 -04:00 committed by GitHub
parent 701e17829d
commit 76dd5e57fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 16 additions and 4 deletions

View file

@ -11,6 +11,7 @@ class Account::Entry < ApplicationRecord
validates :date, :amount, :currency, presence: true validates :date, :amount, :currency, presence: true
validates :date, uniqueness: { scope: [ :account_id, :entryable_type ] }, if: -> { account_valuation? } 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? } validate :trade_valid?, if: -> { account_trade? }
scope :chronological, -> { order(:date, :created_at) } scope :chronological, -> { order(:date, :created_at) }
@ -64,6 +65,11 @@ class Account::Entry < ApplicationRecord
end end
class << self 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) def daily_totals(entries, currency, period: Period.last_30_days)
# Sum spending and income for each day in the period with the given currency # Sum spending and income for each day in the period with the given currency
select( select(

View file

@ -8,7 +8,7 @@
<%= lucide_icon("pencil-line", class: "w-4 h-4 text-gray-500") %> <%= lucide_icon("pencil-line", class: "w-4 h-4 text-gray-500") %>
</div> </div>
<div class="w-full flex items-center justify-between gap-2"> <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.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 :currency, value: entry.account.currency %>
<%= f.hidden_field :entryable_type, value: entry.entryable_type %> <%= f.hidden_field :entryable_type, value: entry.entryable_type %>

View file

@ -86,7 +86,7 @@
<%= label_tag :add_start_values, t(".optional_start_balance_message"), class: "pl-1 text-sm text-gray-500" %> <%= 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="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 class="w-1/2"><%= f.number_field :start_balance, label: t(".start_balance") %></div>
</div> </div>
</div> </div>

View file

@ -18,7 +18,7 @@
<%= f.fields_for :entryable do |ef| %> <%= f.fields_for :entryable do |ef| %>
<%= ef.collection_select :category_id, Current.family.categories.alphabetically, :id, :name, { prompt: t(".category_prompt"), label: t(".category") } %> <%= ef.collection_select :category_id, Current.family.categories.alphabetically, :id, :name, { prompt: t(".category_prompt"), label: t(".category") } %>
<% end %> <% 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>
<section> <section>

View file

@ -7,6 +7,12 @@ class Account::EntryTest < ActiveSupport::TestCase
@entry = account_entries :transaction @entry = account_entries :transaction
end 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 test "valuations cannot have more than one entry per day" do
existing_valuation = account_entries :valuation existing_valuation = account_entries :valuation

View file

@ -60,7 +60,7 @@ class AccountsTest < ApplicationSystemTestCase
select "Chase", from: "Financial institution" select "Chase", from: "Financial institution"
fill_in "account[balance]", with: 100.99 fill_in "account[balance]", with: 100.99
check "Add a start balance for this account" 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 fill_in "Start balance (optional)", with: 95
click_button "Add #{humanized_accountable(accountable_type).downcase}" click_button "Add #{humanized_accountable(accountable_type).downcase}"