The personal finance app for everyone (by everyone)
Ruby
class UI::AccountPage < ApplicationComponent
attr_reader :account, :chart_view, :chart_period, :loan_chart, :as_of, :statement_coverage, :statements,
:reconciliation_statuses, :reconciliation_indicator, :can_manage_statements
renders_one :activity_feed, ->(feed_data:, pagy:, q:) { UI::Account::ActivityFeed.new(feed_data: feed_data, pagy: pagy, q: q) }
# `loan_chart` is the Loan::PayoffChart payload the controller built for a
# loan account, nil for every other type and for a loan with no schedule.
# `as_of` is the page's one reference date, captured by the controller.
# `loan_projection` is the projection the controller already built for the
# chart, so the Schedule tab's forecast card does not simulate it again.
# `extra_repayment` is the Extra repayments tab's Loan::ExtraRepayment, and
# `loan_extra_projection` the projection with it applied, on the same
# `as_of`; nil when no extra applies.
def initialize(account:, chart_view: nil, chart_period: nil, loan_chart: nil, as_of: Date.current, active_tab: nil,
statement_coverage: nil, statements: [], reconciliation_statuses: {}, reconciliation_indicator: nil,
can_manage_statements: false, loan_projection: nil, loan_extra_projection: nil, extra_repayment: nil)
@account = account
@chart_view = chart_view
@chart_period = chart_period
@loan_chart = loan_chart
@loan_projection = loan_projection
@loan_extra_projection = loan_extra_projection
@extra_repayment = extra_repayment
@as_of = as_of
@active_tab = active_tab
@statement_coverage = statement_coverage
@statements = statements
@reconciliation_statuses = reconciliation_statuses
@reconciliation_indicator = reconciliation_indicator
@can_manage_statements = can_manage_statements
end
def id
dom_id(account, :container)
end
def broadcast_channel
account
end
def broadcast_refresh!
Turbo::StreamsChannel.broadcast_replace_to(broadcast_channel, target: id, renderable: self, layout: false)
end
def title
account.name
end
def subtitle
return nil unless account.property?
account.property.address
end
def active_tab
tabs.find { |tab| tab == @active_tab&.to_sym } || tabs.first
end
def tabs
base_tabs = case account.accountable_type
when "Investment", "Crypto"
[ :activity, :holdings ]
when "Loan"
account.loan.amortizable? ? [ :activity, :overview, :schedule, :extra_repayments ] : [ :activity, :overview ]
when "CreditCard"
[ :activity, :overview ]
when "Property", "Vehicle"
[ :activity, :overview ]
else
[ :activity ]
end
base_tabs + [ :statements ]
end
def fx_coverage_start_date
return @fx_coverage_start_date if defined?(@fx_coverage_start_date)
result = nil
if account.family.present? && account.currency != account.family.currency
pair = ExchangeRatePair.for_pair(from: account.currency, to: account.family.currency)
if pair.first_provider_rate_on.present?
oldest_entry = account.entries.minimum(:date)
if oldest_entry.present? && oldest_entry < pair.first_provider_rate_on
result = pair.first_provider_rate_on
end
end
end
@fx_coverage_start_date = result
end
# The controller's projection when it built one; otherwise built here, once
# per render, for callers that construct the page without it.
def loan_projection
@loan_projection ||= account.loan.payoff_projection(as_of: as_of)
end
# The controller's when it built one; otherwise no extra, as before anything
# is entered.
def extra_repayment
@extra_repayment ||= Loan::ExtraRepayment.new(amount: nil, currency: account.currency)
end
# The activity feed's filters. The feed follows a range dragged on the
# chart, so the chart needs them to carry through a period pick.
def activity_filters
activity_feed? ? activity_feed.q : {}
end
def tab_content_for(tab)
case tab
when :activity
activity_feed
when :overview
# Accountable is responsible for implementing the partial in the correct
# folder. These tabs show date-sensitive figures and take the page's one
# reference date, like the Schedule tab does.
locals = { account: account }
locals[:as_of] = as_of if %w[Loan CreditCard].include?(account.accountable_type)
render "#{accountable_view_folder}/tabs/#{tab}", **locals
when :holdings
# Accountable is responsible for implementing the partial in the correct folder
render "#{accountable_view_folder}/tabs/#{tab}", account: account
when :schedule
render "loans/tabs/schedule", account: account, as_of: as_of, projection: loan_projection
when :extra_repayments
render "loans/tabs/extra_repayments",
account: account, projection: loan_projection, extra_projection: @loan_extra_projection,
extra_repayment: extra_repayment
when :statements
render_statement_tab
end
end
def render_statement_tab
return render "accounts/show/statements_frame", **statement_tab_locals if statement_tab_loaded?
turbo_frame_tag statement_tab_frame_id, src: helpers.account_path(account, tab: "statements"), loading: :lazy
end
def statement_tab_loaded?
statement_coverage.present?
end
def statement_tab_frame_id
dom_id(account, :statements_tab)
end
def statement_tab_locals
{
account: account,
coverage: statement_coverage,
statements: statements,
reconciliation_statuses: reconciliation_statuses,
can_manage_statements: can_manage_statements
}
end
private
# The app/views folder an accountable's own tab partials live in.
#
# Not `accountable_type.downcase.pluralize`, which reads a two-word class
# name as one word: "CreditCard" became `creditcards/tabs/overview` and
# there is no such folder. The accountable names its own folder, and a
# multi-word one is underscored.
def accountable_view_folder
account.accountable_type.underscore.pluralize
end
end