2024-02-02 09:05:04 -06:00
|
|
|
module ApplicationHelper
|
2024-03-08 15:11:58 -05:00
|
|
|
include Pagy::Frontend
|
|
|
|
|
2024-02-02 09:05:04 -06:00
|
|
|
def title(page_title)
|
|
|
|
content_for(:title) { page_title }
|
|
|
|
end
|
|
|
|
|
|
|
|
def header_title(page_title)
|
|
|
|
content_for(:header_title) { page_title }
|
|
|
|
end
|
2024-02-02 23:41:58 +00:00
|
|
|
|
|
|
|
def permitted_accountable_partial(name)
|
|
|
|
name.underscore
|
|
|
|
end
|
2024-02-08 10:46:05 -06:00
|
|
|
|
2024-02-13 15:19:11 +00:00
|
|
|
def notification(text, **options, &block)
|
|
|
|
content = tag.p(text)
|
|
|
|
content = capture &block if block_given?
|
|
|
|
|
|
|
|
render partial: "shared/notification", locals: { type: options[:type], content: content }
|
|
|
|
end
|
|
|
|
|
2024-02-08 10:46:05 -06:00
|
|
|
# Wrap view with <%= modal do %> ... <% end %> to have it open in a modal
|
|
|
|
# Make sure to add data-turbo-frame="modal" to the link/button that opens the modal
|
|
|
|
def modal(&block)
|
2024-02-09 14:25:59 +00:00
|
|
|
content = capture &block
|
|
|
|
render partial: "shared/modal", locals: { content: content }
|
2024-02-08 10:46:05 -06:00
|
|
|
end
|
2024-02-10 16:18:56 -06:00
|
|
|
|
2024-03-21 13:39:10 -04:00
|
|
|
def account_groups
|
|
|
|
assets, liabilities = Current.family.accounts.by_group(currency: Current.family.currency, period: Period.last_30_days).values_at(:assets, :liabilities)
|
|
|
|
[ assets.children, liabilities.children ].flatten
|
|
|
|
end
|
2024-03-19 09:10:40 -04:00
|
|
|
|
2024-03-10 21:38:31 +00:00
|
|
|
def sidebar_modal(&block)
|
|
|
|
content = capture &block
|
|
|
|
render partial: "shared/sidebar_modal", locals: { content: content }
|
|
|
|
end
|
|
|
|
|
2024-02-11 18:47:58 +00:00
|
|
|
def sidebar_link_to(name, path, options = {})
|
2024-02-11 12:56:23 -06:00
|
|
|
base_class_names = [ "block", "border", "border-transparent", "rounded-xl", "-ml-2", "p-2", "text-sm", "font-medium", "text-gray-500", "flex", "items-center" ]
|
2024-02-12 17:20:50 -05:00
|
|
|
hover_class_names = [ "hover:bg-white", "hover:border-alpha-black-50", "hover:text-gray-900", "hover:shadow-xs" ]
|
|
|
|
current_page_class_names = [ "bg-white", "border-alpha-black-50", "text-gray-900", "shadow-xs" ]
|
2024-02-11 12:56:23 -06:00
|
|
|
|
2024-02-14 12:55:23 +00:00
|
|
|
link_class_names = if current_page?(path) || (request.path.start_with?(path) && path != "/")
|
2024-02-11 12:56:23 -06:00
|
|
|
base_class_names.delete("border-transparent")
|
|
|
|
base_class_names + hover_class_names + current_page_class_names
|
|
|
|
else
|
|
|
|
base_class_names + hover_class_names
|
|
|
|
end
|
2024-02-11 18:47:58 +00:00
|
|
|
|
2024-02-11 12:56:23 -06:00
|
|
|
merged_options = options.reverse_merge(class: link_class_names.join(" ")).except(:icon)
|
2024-02-11 18:47:58 +00:00
|
|
|
|
|
|
|
link_to path, merged_options do
|
|
|
|
lucide_icon(options[:icon], class: "w-5 h-5 mr-2") + name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-01 17:33:54 -05:00
|
|
|
def trend_styles(trend)
|
2024-03-06 09:56:59 -05:00
|
|
|
fallback = { bg_class: "bg-gray-500/5", text_class: "text-gray-500", symbol: "", icon: "minus" }
|
|
|
|
return fallback if trend.nil? || trend.direction == "flat"
|
|
|
|
|
2024-02-22 11:35:06 -05:00
|
|
|
bg_class, text_class, symbol, icon = case trend.direction
|
2024-02-20 09:07:55 -05:00
|
|
|
when "up"
|
2024-03-04 08:31:22 -05:00
|
|
|
trend.type == "liability" ? [ "bg-red-500/5", "text-red-500", "+", "arrow-up" ] : [ "bg-green-500/5", "text-green-500", "+", "arrow-up" ]
|
2024-02-20 09:07:55 -05:00
|
|
|
when "down"
|
2024-03-04 08:31:22 -05:00
|
|
|
trend.type == "liability" ? [ "bg-green-500/5", "text-green-500", "-", "arrow-down" ] : [ "bg-red-500/5", "text-red-500", "-", "arrow-down" ]
|
2024-02-20 09:07:55 -05:00
|
|
|
when "flat"
|
|
|
|
[ "bg-gray-500/5", "text-gray-500", "", "minus" ]
|
|
|
|
else
|
2024-02-22 11:35:06 -05:00
|
|
|
raise ArgumentError, "Invalid trend direction: #{trend.direction}"
|
2024-02-20 09:07:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
{ bg_class: bg_class, text_class: text_class, symbol: symbol, icon: icon }
|
|
|
|
end
|
|
|
|
|
2024-03-01 17:33:54 -05:00
|
|
|
def period_label(period)
|
2024-03-21 13:39:10 -04:00
|
|
|
return "since account creation" if period.date_range.begin.nil?
|
2024-02-22 11:35:06 -05:00
|
|
|
start_date, end_date = period.date_range.first, period.date_range.last
|
|
|
|
|
|
|
|
return "Starting from #{start_date.strftime('%b %d, %Y')}" if end_date.nil?
|
|
|
|
return "Ending at #{end_date.strftime('%b %d, %Y')}" if start_date.nil?
|
|
|
|
|
2024-02-20 09:07:55 -05:00
|
|
|
days_apart = (end_date - start_date).to_i
|
|
|
|
|
|
|
|
case days_apart
|
|
|
|
when 1
|
|
|
|
"vs. yesterday"
|
|
|
|
when 7
|
|
|
|
"vs. last week"
|
|
|
|
when 30, 31
|
|
|
|
"vs. last month"
|
|
|
|
when 365, 366
|
|
|
|
"vs. last year"
|
|
|
|
else
|
|
|
|
"from #{start_date.strftime('%b %d, %Y')} to #{end_date.strftime('%b %d, %Y')}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-18 11:21:00 -04:00
|
|
|
def format_money(number_or_money, options = {})
|
|
|
|
money = Money.new(number_or_money)
|
|
|
|
options.reverse_merge!(money.default_format_options)
|
|
|
|
number_to_currency(money.amount, options)
|
|
|
|
end
|
2024-02-10 16:18:56 -06:00
|
|
|
|
2024-03-18 11:21:00 -04:00
|
|
|
def format_money_without_symbol(number_or_money, options = {})
|
|
|
|
money = Money.new(number_or_money)
|
|
|
|
options.reverse_merge!(money.default_format_options)
|
|
|
|
ActiveSupport::NumberHelper.number_to_delimited(money.amount.round(options[:precision] || 0), { delimiter: options[:delimiter], separator: options[:separator] })
|
2024-02-10 16:18:56 -06:00
|
|
|
end
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|