mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-18 20:59:39 +02:00
Since the very first 0.1.0-alpha.1 release, we've been moving quickly to add new features to the Maybe app. In doing so, some parts of the codebase have become outdated, unnecessary, or overly-complex as a natural result of this feature prioritization. Now that "core" Maybe is complete, we're moving into a second phase of development where we'll be working hard to improve the accuracy of existing features and build additional features on top of "core". This PR is a quick overhaul of the existing codebase aimed to: - Establish the brand new and simplified dashboard view (pictured above) - Establish and move towards the conventions introduced in Cursor rules and project design overview #1788 - Consolidate layouts and improve the performance of layout queries - Organize the core models of the Maybe domain (i.e. Account::Entry, Account::Transaction, etc.) and break out specific traits of each model into dedicated concerns for better readability - Remove stale / dead code from codebase - Remove overly complex code paths in favor of simpler ones
86 lines
2.3 KiB
Ruby
86 lines
2.3 KiB
Ruby
class Account::Entry < ApplicationRecord
|
|
include Monetizable
|
|
|
|
monetize :amount
|
|
|
|
belongs_to :account
|
|
belongs_to :transfer, optional: true
|
|
belongs_to :import, optional: true
|
|
|
|
delegated_type :entryable, types: Account::Entryable::TYPES, dependent: :destroy
|
|
accepts_nested_attributes_for :entryable
|
|
|
|
validates :date, :name, :amount, :currency, presence: true
|
|
validates :date, uniqueness: { scope: [ :account_id, :entryable_type ] }, if: -> { account_valuation? }
|
|
validates :date, comparison: { greater_than: -> { min_supported_date } }
|
|
|
|
scope :active, -> {
|
|
joins(:account).where(accounts: { is_active: true })
|
|
}
|
|
|
|
scope :chronological, -> {
|
|
order(
|
|
date: :asc,
|
|
Arel.sql("CASE WHEN account_entries.entryable_type = 'Account::Valuation' THEN 1 ELSE 0 END") => :asc,
|
|
created_at: :asc
|
|
)
|
|
}
|
|
|
|
scope :reverse_chronological, -> {
|
|
order(
|
|
date: :desc,
|
|
Arel.sql("CASE WHEN account_entries.entryable_type = 'Account::Valuation' THEN 1 ELSE 0 END") => :desc,
|
|
created_at: :desc
|
|
)
|
|
}
|
|
|
|
def sync_account_later
|
|
sync_start_date = [ date_previously_was, date ].compact.min unless destroyed?
|
|
account.sync_later(start_date: sync_start_date)
|
|
end
|
|
|
|
def entryable_name_short
|
|
entryable_type.demodulize.underscore
|
|
end
|
|
|
|
def balance_trend(entries, balances)
|
|
Account::BalanceTrendCalculator.new(self, entries, balances).trend
|
|
end
|
|
|
|
def display_name
|
|
enriched_name.presence || name
|
|
end
|
|
|
|
class << self
|
|
def search(params)
|
|
Account::EntrySearch.new(params).build_query(all)
|
|
end
|
|
|
|
# arbitrary cutoff date to avoid expensive sync operations
|
|
def min_supported_date
|
|
30.years.ago.to_date
|
|
end
|
|
|
|
def bulk_update!(bulk_update_params)
|
|
bulk_attributes = {
|
|
date: bulk_update_params[:date],
|
|
notes: bulk_update_params[:notes],
|
|
entryable_attributes: {
|
|
category_id: bulk_update_params[:category_id],
|
|
merchant_id: bulk_update_params[:merchant_id]
|
|
}.compact_blank
|
|
}.compact_blank
|
|
|
|
return 0 if bulk_attributes.blank?
|
|
|
|
transaction do
|
|
all.each do |entry|
|
|
bulk_attributes[:entryable_attributes][:id] = entry.entryable_id if bulk_attributes[:entryable_attributes].present?
|
|
entry.update! bulk_attributes
|
|
end
|
|
end
|
|
|
|
all.size
|
|
end
|
|
end
|
|
end
|