diff --git a/app/controllers/concerns/dashboard_builder.rb b/app/controllers/concerns/dashboard_builder.rb index 64e48c1..eaa0644 100644 --- a/app/controllers/concerns/dashboard_builder.rb +++ b/app/controllers/concerns/dashboard_builder.rb @@ -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 diff --git a/app/controllers/concerns/stream.rb b/app/controllers/concerns/stream.rb new file mode 100644 index 0000000..1497ad8 --- /dev/null +++ b/app/controllers/concerns/stream.rb @@ -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 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/controllers/incomes_controller.rb b/app/controllers/incomes_controller.rb index 1f9b53d..def3700 100644 --- a/app/controllers/incomes_controller.rb +++ b/app/controllers/incomes_controller.rb @@ -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] @@ -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 @@ -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 diff --git a/app/controllers/savings_rates_controller.rb b/app/controllers/savings_rates_controller.rb index be14377..d461a2d 100644 --- a/app/controllers/savings_rates_controller.rb +++ b/app/controllers/savings_rates_controller.rb @@ -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 diff --git a/app/services/total_cost_calculator.rb b/app/services/total_cost_calculator.rb new file mode 100644 index 0000000..c40065e --- /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 91f9f81..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 %> \ No newline at end of file +<% 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 8a9a854..eb1d77a 100644 --- a/app/views/fixed_expenses/create.turbo_stream.erb +++ b/app/views/fixed_expenses/create.turbo_stream.erb @@ -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 %> diff --git a/app/views/fixed_expenses/destroy.turbo_stream.erb b/app/views/fixed_expenses/destroy.turbo_stream.erb index 44ec110..083204b 100644 --- a/app/views/fixed_expenses/destroy.turbo_stream.erb +++ b/app/views/fixed_expenses/destroy.turbo_stream.erb @@ -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 %> 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 } %>