Skip to content

Commit

Permalink
Refactor costs (#22)
Browse files Browse the repository at this point in the history
* Update streaming

* Refactor total cost to service

* Lint
  • Loading branch information
neb417 authored Nov 22, 2023
1 parent d50d558 commit dbb461d
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 96 deletions.
12 changes: 11 additions & 1 deletion app/controllers/concerns/dashboard_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ def build_dashboard_variables!
build_savings_vars!
build_guilt_free_vars!
build_total_cost_vars!
Rails.logger.debug "\n *** Building Vars!!\n "
end

def build_locals(taxed_income)
income = taxed_income.income
{
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
}
end
end
54 changes: 54 additions & 0 deletions app/controllers/concerns/stream.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

module Stream
include DashboardBuilder
include TaxedIncome

def render_respond_to(turbo_replace)
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_replace
end
end
end

def switch_to_hourly
[
turbo_stream.replace("salary_budget",
partial: "budget/hourly_budget",
locals: build_locals(tax_on_hourly))
]
end

def switch_to_salary
[
turbo_stream.replace("hourly_budget",
partial: "budget/salary_budget",
locals: build_locals(tax_on_salary))
]
end

def render_dashboard
[
turbo_stream.replace("hourly_budget",
partial: "budget/salary_budget",
locals: build_locals(tax_on_salary)),
turbo_stream.replace("salary_budget",
partial: "budget/hourly_budget",
locals: build_locals(tax_on_hourly)),
turbo_stream.replace("total_costs",
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
}),
turbo_stream.replace("taxed_incomes",
partial: "shared/taxed_incomes",
locals: {
salary_taxed: @salary_taxed,
hourly_taxed: @hourly_taxed
})
]
end
end
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
44 changes: 17 additions & 27 deletions app/controllers/incomes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class IncomesController < ApplicationController
include TotalCost
include SaveIncome
include GuiltFree
include Stream

before_action :set_income, only: %i[show edit update destroy]

Expand Down Expand Up @@ -63,22 +64,11 @@ def destroy
end

def income_switch
build_dashboard_variables!
if params[:enabled] == "0"
respond_to do |format|
format.turbo_stream {
render turbo_stream: turbo_stream.replace("hourly_budget",
partial: "budget/salary_budget",
locals: build_locals(tax_on_salary))
}
end
render_respond_to(switch_to_salary)
else
respond_to do |format|
format.turbo_stream {
render turbo_stream: turbo_stream.replace("salary_budget",
partial: "budget/hourly_budget",
locals: build_locals(tax_on_hourly))
}
end
render_respond_to(switch_to_hourly)
end
end

Expand All @@ -94,17 +84,17 @@ def income_params
params.require(:income).permit(:income_type, :rate, :hours, :weekly_income)
end

def build_locals(taxed_income)
income = taxed_income.income
build_dashboard_variables!
{
total_annual_cost: @total_annual_cost,
total_monthly_cost: @total_monthly_cost,
total_bi_weekly_cost: @total_bi_weekly_cost,
income: taxed_income,
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
}
end
# def build_locals(taxed_income)
# income = taxed_income.income
# build_dashboard_variables!
# {
# total_annual_cost: @total_annual_cost,
# total_monthly_cost: @total_monthly_cost,
# total_bi_weekly_cost: @total_bi_weekly_cost,
# income: taxed_income,
# 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
# }
# end
end
1 change: 0 additions & 1 deletion app/controllers/savings_rates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def create
def update
respond_to do |format|
if @savings_rate.update_from_dashboard(params: savings_rate_params)
# calculate new savings amounts for both hourly and salary and new guilt free
build_dashboard_variables!
format.turbo_stream
else
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
9 changes: 4 additions & 5 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 %>
<% 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
22 changes: 19 additions & 3 deletions app/views/fixed_expenses/create.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@
<% 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} %>
<%= render partial: "budget/salary_budget",
locals: {
income: @salary_taxed,
total_cost: @total_cost,
investing_amount: @salary_invest,
savings_amount: @salary_saving,
guilt_free: @guilt_free_salary
}
%>
<% end %>

<%= 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} %>
<%= render partial: "budget/hourly_budget",
locals: {
income: @hourly_taxed,
total_cost: @total_cost,
investing_amount: @hourly_invest,
savings_amount: @hourly_saving,
guilt_free: @guilt_free_hourly
}
%>
<% end %>
22 changes: 19 additions & 3 deletions app/views/fixed_expenses/destroy.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
<%= 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} %>
<%= render partial: "budget/salary_budget",
locals: {
income: @salary_taxed,
total_cost: @total_cost,
investing_amount: @salary_invest,
savings_amount: @salary_saving,
guilt_free: @guilt_free_salary
}
%>
<% end %>

<%= 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} %>
<%= render partial: "budget/hourly_budget",
locals: {
income: @hourly_taxed,
total_cost: @total_cost,
investing_amount: @hourly_invest,
savings_amount: @hourly_saving,
guilt_free: @guilt_free_hourly
}
%>
<% end %>
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
Loading

0 comments on commit dbb461d

Please sign in to comment.