From ca8eb98437eeb2a0e23a797cf796c8713d2f29ea Mon Sep 17 00:00:00 2001 From: "Benjamin Randolphgit config --global user.email neb417@gmail.comgit config --global init.defaultBranch maingit config --global core.editor atom" Date: Wed, 22 Nov 2023 15:57:33 -0700 Subject: [PATCH] Refactor total cost to service --- app/controllers/concerns/dashboard_builder.rb | 4 +- app/controllers/concerns/total_cost.rb | 9 ++-- app/services/total_cost_calculator.rb | 45 +++++++++++++++++++ app/views/budget/_hourly_budget.html.erb | 7 ++- app/views/budget/_salary_budget.html.erb | 4 +- app/views/dashboard/index.html.erb | 11 ++--- .../fixed_expenses/create.turbo_stream.erb | 16 ++----- .../fixed_expenses/destroy.turbo_stream.erb | 16 ++----- .../fixed_expenses/update.turbo_stream.erb | 16 ++----- app/views/incomes/update.turbo_stream.erb | 8 +--- .../savings_rates/update.turbo_stream.erb | 8 +--- app/views/shared/_budget.html.erb | 28 ++++++------ app/views/shared/_total_costs.html.erb | 6 +-- 13 files changed, 89 insertions(+), 89 deletions(-) create mode 100644 app/services/total_cost_calculator.rb diff --git a/app/controllers/concerns/dashboard_builder.rb b/app/controllers/concerns/dashboard_builder.rb index 80cef16..eaa0644 100644 --- a/app/controllers/concerns/dashboard_builder.rb +++ b/app/controllers/concerns/dashboard_builder.rb @@ -21,10 +21,8 @@ def build_dashboard_variables! def build_locals(taxed_income) income = taxed_income.income { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost, income: taxed_income, + total_cost: @total_cost, investing_amount: income.is_hourly? ? @hourly_invest : @salary_invest, savings_amount: income.is_hourly? ? @hourly_saving : @salary_saving, guilt_free: income.is_hourly? ? @guilt_free_hourly : @guilt_free_salary diff --git a/app/controllers/concerns/total_cost.rb b/app/controllers/concerns/total_cost.rb index 003e2f9..38cbfb1 100644 --- a/app/controllers/concerns/total_cost.rb +++ b/app/controllers/concerns/total_cost.rb @@ -13,10 +13,13 @@ def total_bi_weekly_cost Money.new(get_sum(:bi_weekly_cost_cents)) end + def total_cost + cost = FixedExpense.sum(&:annual_cost) + TotalCostCalculator.new(cost: cost) + end + def build_total_cost_vars! - @total_annual_cost = total_annual_cost - @total_monthly_cost = total_monthly_cost - @total_bi_weekly_cost = total_bi_weekly_cost + @total_cost = total_cost end private diff --git a/app/services/total_cost_calculator.rb b/app/services/total_cost_calculator.rb new file mode 100644 index 0000000..f15bdfd --- /dev/null +++ b/app/services/total_cost_calculator.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +class TotalCostCalculator + attr_reader :annual_cost, + :biannual_cost, + :quarterly_cost, + :monthly_cost, + :bi_weekly_cost, + :weekly_cost, + :daily_cost + + def initialize(cost:) + @annual_cost = cost + @biannual_cost = calculate_biannual_cost + @quarterly_cost = calculate_quarterly_cost + @monthly_cost = calculate_monthly_cost + @bi_weekly_cost = calculate_bi_weekly_cost + @weekly_cost = calculate_weekly_cost + @daily_cost = calculate_daily_cost + end + + def calculate_biannual_cost + @annual_cost / 2 + end + + def calculate_quarterly_cost + @annual_cost / 4 + end + + def calculate_monthly_cost + @annual_cost / 12 + end + + def calculate_bi_weekly_cost + @annual_cost / 26 + end + + def calculate_weekly_cost + @annual_cost / 52 + end + + def calculate_daily_cost + @annual_cost / 365 + end +end diff --git a/app/views/budget/_hourly_budget.html.erb b/app/views/budget/_hourly_budget.html.erb index 5f6eb77..1f0e91f 100644 --- a/app/views/budget/_hourly_budget.html.erb +++ b/app/views/budget/_hourly_budget.html.erb @@ -5,13 +5,12 @@ <%= render partial: "budget/budget_headings" %> <%= render partial: "shared/budget", locals: { - total_annual_cost: total_annual_cost, - total_monthly_cost: total_monthly_cost, - total_bi_weekly_cost: total_bi_weekly_cost, income: income, + total_cost: total_cost, investing_amount: investing_amount, savings_amount: savings_amount, guilt_free: guilt_free - } %> + } + %> <% end %> diff --git a/app/views/budget/_salary_budget.html.erb b/app/views/budget/_salary_budget.html.erb index 1517746..8ace22f 100644 --- a/app/views/budget/_salary_budget.html.erb +++ b/app/views/budget/_salary_budget.html.erb @@ -4,10 +4,8 @@ <%= render partial: "budget/budget_headings" %> <%= render partial: "shared/budget", locals: { - total_annual_cost: total_annual_cost, - total_monthly_cost: total_monthly_cost, - total_bi_weekly_cost: total_bi_weekly_cost, income: income, + total_cost: total_cost, investing_amount: investing_amount, savings_amount: savings_amount, guilt_free: guilt_free diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb index 8d6d111..9b181dc 100644 --- a/app/views/dashboard/index.html.erb +++ b/app/views/dashboard/index.html.erb @@ -39,9 +39,8 @@
<%= render partial: "budget/salary_budget", locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost, income: @salary_taxed, + income: @salary_taxed, + total_cost: @total_cost, investing_amount: @salary_invest, savings_amount: @salary_saving, guilt_free: @guilt_free_salary @@ -85,11 +84,7 @@ <% end %> <%= turbo_frame_tag "total_costs" do %> - <%= render partial: "shared/total_costs", - locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost } %> + <%= render partial: "shared/total_costs", locals: { total_cost: @total_cost } %> <% end %>
diff --git a/app/views/fixed_expenses/create.turbo_stream.erb b/app/views/fixed_expenses/create.turbo_stream.erb index d18719b..eb1d77a 100644 --- a/app/views/fixed_expenses/create.turbo_stream.erb +++ b/app/views/fixed_expenses/create.turbo_stream.erb @@ -3,22 +3,14 @@ <% end %> <%= turbo_stream.replace "total_costs" do %> - <%= render partial: "shared/total_costs", - locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost - } - %> + <%= render partial: "shared/total_costs", locals: { total_cost: @total_cost } %> <% end %> <%= turbo_stream.replace "salary_budget" do %> <%= render partial: "budget/salary_budget", locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost, income: @salary_taxed, + total_cost: @total_cost, investing_amount: @salary_invest, savings_amount: @salary_saving, guilt_free: @guilt_free_salary @@ -29,10 +21,8 @@ <%= turbo_stream.replace "hourly_budget" do %> <%= render partial: "budget/hourly_budget", locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost, income: @hourly_taxed, + total_cost: @total_cost, investing_amount: @hourly_invest, savings_amount: @hourly_saving, guilt_free: @guilt_free_hourly diff --git a/app/views/fixed_expenses/destroy.turbo_stream.erb b/app/views/fixed_expenses/destroy.turbo_stream.erb index 178dc65..083204b 100644 --- a/app/views/fixed_expenses/destroy.turbo_stream.erb +++ b/app/views/fixed_expenses/destroy.turbo_stream.erb @@ -1,22 +1,14 @@ <%= turbo_stream.remove @fixed_expense %> <%= turbo_stream.replace "total_costs" do %> - <%= render partial: "shared/total_costs", - locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost - } - %> + <%= render partial: "shared/total_costs", locals: { total_cost: @total_cost } %> <% end %> <%= turbo_stream.replace "salary_budget" do %> <%= render partial: "budget/salary_budget", locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost, income: @salary_taxed, + total_cost: @total_cost, investing_amount: @salary_invest, savings_amount: @salary_saving, guilt_free: @guilt_free_salary @@ -27,10 +19,8 @@ <%= turbo_stream.replace "hourly_budget" do %> <%= render partial: "budget/hourly_budget", locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost, income: @hourly_taxed, + total_cost: @total_cost, investing_amount: @hourly_invest, savings_amount: @hourly_saving, guilt_free: @guilt_free_hourly diff --git a/app/views/fixed_expenses/update.turbo_stream.erb b/app/views/fixed_expenses/update.turbo_stream.erb index db59b34..695ceee 100644 --- a/app/views/fixed_expenses/update.turbo_stream.erb +++ b/app/views/fixed_expenses/update.turbo_stream.erb @@ -1,22 +1,14 @@ <%= turbo_stream.update @fixed_expense %> <%= turbo_stream.replace "total_costs" do %> - <%= render partial: "shared/total_costs", - locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost - } - %> + <%= render partial: "shared/total_costs", locals: { total_cost: @total_cost } %> <% end %> <%= turbo_stream.replace "salary_budget" do %> <%= render partial: "budget/salary_budget", locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost, income: @salary_taxed, + total_cost: @total_cost, investing_amount: @salary_invest, savings_amount: @salary_saving, guilt_free: @guilt_free_salary @@ -27,10 +19,8 @@ <%= turbo_stream.replace "hourly_budget" do %> <%= render partial: "budget/hourly_budget", locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost, income: @hourly_taxed, + total_cost: @total_cost, investing_amount: @hourly_invest, savings_amount: @hourly_saving, guilt_free: @guilt_free_hourly diff --git a/app/views/incomes/update.turbo_stream.erb b/app/views/incomes/update.turbo_stream.erb index 05eca50..e0124c8 100644 --- a/app/views/incomes/update.turbo_stream.erb +++ b/app/views/incomes/update.turbo_stream.erb @@ -12,10 +12,8 @@ <%= turbo_stream.replace "salary_budget" do %> <%= render partial: "budget/salary_budget", locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost, income: @salary_taxed, + total_cost: @total_cost, investing_amount: @salary_invest, savings_amount: @salary_saving, guilt_free: @guilt_free_salary @@ -26,10 +24,8 @@ <%= turbo_stream.replace "hourly_budget" do %> <%= render partial: "budget/hourly_budget", locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost, income: @hourly_taxed, + total_cost: @total_cost, investing_amount: @hourly_invest, savings_amount: @hourly_saving, guilt_free: @guilt_free_hourly diff --git a/app/views/savings_rates/update.turbo_stream.erb b/app/views/savings_rates/update.turbo_stream.erb index dd214b8..c279cfb 100644 --- a/app/views/savings_rates/update.turbo_stream.erb +++ b/app/views/savings_rates/update.turbo_stream.erb @@ -3,10 +3,8 @@ <%= turbo_stream.replace "salary_budget" do %> <%= render partial: "budget/salary_budget", locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost, income: @salary_taxed, + total_cost: @total_cost, investing_amount: @salary_invest, savings_amount: @salary_saving, guilt_free: @guilt_free_salary @@ -17,10 +15,8 @@ <%= turbo_stream.replace "hourly_budget" do %> <%= render partial: "budget/hourly_budget", locals: { - total_annual_cost: @total_annual_cost, - total_monthly_cost: @total_monthly_cost, - total_bi_weekly_cost: @total_bi_weekly_cost, income: @hourly_taxed, + total_cost: @total_cost, investing_amount: @hourly_invest, savings_amount: @hourly_saving, guilt_free: @guilt_free_hourly diff --git a/app/views/shared/_budget.html.erb b/app/views/shared/_budget.html.erb index 47ae1b3..2e3c377 100644 --- a/app/views/shared/_budget.html.erb +++ b/app/views/shared/_budget.html.erb @@ -3,10 +3,10 @@ <%= render partial: "budget/display_budget", locals: { - total_cost: total_annual_cost/365, + total_cost: total_cost.daily_cost, savings_amount: savings_amount.daily_saving, investing_amount: investing_amount.daily_saving, - guilt_free: guilt_free.daily_guilt_free - total_annual_cost/365 + guilt_free: guilt_free.daily_guilt_free - total_cost.daily_cost } %> @@ -16,10 +16,10 @@ <%= render partial: "budget/display_budget", locals: { - total_cost: total_annual_cost/52, + total_cost: total_cost.weekly_cost, savings_amount: savings_amount.weekly_saving, investing_amount: investing_amount.weekly_saving, - guilt_free: guilt_free.weekly_guilt_free - total_annual_cost/52 + guilt_free: guilt_free.weekly_guilt_free - total_cost.weekly_cost } %> @@ -29,10 +29,10 @@ <%= render partial: "budget/display_budget", locals: { - total_cost: total_bi_weekly_cost, + total_cost: total_cost.bi_weekly_cost, savings_amount: savings_amount.bi_weekly_saving, investing_amount: investing_amount.bi_weekly_saving, - guilt_free: guilt_free.bi_weekly_guilt_free - total_bi_weekly_cost + guilt_free: guilt_free.bi_weekly_guilt_free - total_cost.bi_weekly_cost } %> @@ -43,10 +43,10 @@ <%= render partial: "budget/display_budget", locals: { - total_cost: total_monthly_cost, + total_cost: total_cost.monthly_cost, savings_amount: savings_amount.monthly_saving, investing_amount: investing_amount.monthly_saving, - guilt_free: guilt_free.monthly_guilt_free - total_monthly_cost + guilt_free: guilt_free.monthly_guilt_free - total_cost.monthly_cost } %> @@ -56,10 +56,10 @@ <%= render partial: "budget/display_budget", locals: { - total_cost: total_annual_cost/4, + total_cost: total_cost.quarterly_cost, savings_amount: savings_amount.quarterly_saving, investing_amount: investing_amount.quarterly_saving, - guilt_free: guilt_free.quarterly_guilt_free - total_annual_cost / 4 + guilt_free: guilt_free.quarterly_guilt_free - total_cost.quarterly_cost } %> @@ -69,10 +69,10 @@ <%= render partial: "budget/display_budget", locals: { - total_cost: total_annual_cost/2, + total_cost: total_cost.biannual_cost, savings_amount: savings_amount.biannual_saving, investing_amount: investing_amount.biannual_saving, - guilt_free: guilt_free.biannual_guilt_free - total_annual_cost / 2 + guilt_free: guilt_free.biannual_guilt_free - total_cost.biannual_cost } %> @@ -82,10 +82,10 @@ <%= render partial: "budget/display_budget", locals: { - total_cost: total_annual_cost, + total_cost: total_cost.annual_cost, savings_amount: savings_amount.annual_saving, investing_amount: investing_amount.annual_saving, - guilt_free: guilt_free.annual_guilt_free - total_annual_cost + guilt_free: guilt_free.annual_guilt_free - total_cost.annual_cost } %> diff --git a/app/views/shared/_total_costs.html.erb b/app/views/shared/_total_costs.html.erb index 7ba865a..a89dffe 100644 --- a/app/views/shared/_total_costs.html.erb +++ b/app/views/shared/_total_costs.html.erb @@ -5,19 +5,19 @@
- <%= render partial: "shared/total", locals: { total: total_annual_cost } %> + <%= render partial: "shared/total", locals: { total: total_cost.annual_cost } %>
- <%= render partial: "shared/total", locals: { total: total_monthly_cost } %> + <%= render partial: "shared/total", locals: { total: total_cost.monthly_cost } %>
- <%= render partial: "shared/total", locals: { total: total_bi_weekly_cost } %> + <%= render partial: "shared/total", locals: { total: total_cost.bi_weekly_cost } %>