Skip to content

Commit

Permalink
Refactor total cost to service
Browse files Browse the repository at this point in the history
  • Loading branch information
neb417 committed Nov 22, 2023
1 parent 9533cbf commit ca8eb98
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 89 deletions.
4 changes: 1 addition & 3 deletions app/controllers/concerns/dashboard_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions app/controllers/concerns/total_cost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions app/services/total_cost_calculator.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 3 additions & 4 deletions app/views/budget/_hourly_budget.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
} %>
}
%>
</div>
<% end %>
4 changes: 1 addition & 3 deletions app/views/budget/_salary_budget.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions app/views/dashboard/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@
<div id="final_income">
<%= 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
Expand Down Expand Up @@ -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 %>
</div>
</div>
Expand Down
16 changes: 3 additions & 13 deletions app/views/fixed_expenses/create.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 3 additions & 13 deletions app/views/fixed_expenses/destroy.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
16 changes: 3 additions & 13 deletions app/views/fixed_expenses/update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions app/views/incomes/update.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions app/views/savings_rates/update.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
28 changes: 14 additions & 14 deletions app/views/shared/_budget.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
%>
</div>
Expand All @@ -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
}
%>
</div>
Expand All @@ -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
}
%>

Expand All @@ -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
}
%>
</div>
Expand All @@ -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
}
%>
</div>
Expand All @@ -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
}
%>
</div>
Expand All @@ -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
}
%>
</div>
Loading

0 comments on commit ca8eb98

Please sign in to comment.