2025-02-21 11:57:59 -05:00
|
|
|
class AccountableSparklinesController < ApplicationController
|
|
|
|
def show
|
|
|
|
@accountable = Accountable.from_type(params[:accountable_type]&.classify)
|
|
|
|
|
2025-06-10 18:20:06 -04:00
|
|
|
etag_key = cache_key
|
|
|
|
|
|
|
|
# Use HTTP conditional GET so the client receives 304 Not Modified when possible.
|
|
|
|
if stale?(etag: etag_key, last_modified: family.latest_sync_completed_at)
|
|
|
|
@series = Rails.cache.fetch(etag_key, expires_in: 24.hours) do
|
|
|
|
builder = Balance::ChartSeriesBuilder.new(
|
|
|
|
account_ids: account_ids,
|
|
|
|
currency: family.currency,
|
|
|
|
period: Period.last_30_days,
|
|
|
|
favorable_direction: @accountable.favorable_direction,
|
|
|
|
interval: "1 day"
|
|
|
|
)
|
2025-02-21 11:57:59 -05:00
|
|
|
|
2025-06-10 18:20:06 -04:00
|
|
|
builder.balance_series
|
|
|
|
end
|
|
|
|
|
|
|
|
render layout: false
|
|
|
|
end
|
2025-02-21 11:57:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def family
|
|
|
|
Current.family
|
|
|
|
end
|
|
|
|
|
2025-06-10 18:20:06 -04:00
|
|
|
def accountable
|
|
|
|
Accountable.from_type(params[:accountable_type]&.classify)
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_ids
|
|
|
|
family.accounts.active.where(accountable_type: accountable.name).pluck(:id)
|
|
|
|
end
|
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
def cache_key
|
2025-06-10 18:20:06 -04:00
|
|
|
family.build_cache_key("#{@accountable.name}_sparkline", invalidate_on_data_updates: true)
|
2025-02-21 11:57:59 -05:00
|
|
|
end
|
|
|
|
end
|