From 9bf37c26b23c8e8853b4a13028f4844aaaef323c 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: Thu, 16 Nov 2023 09:33:58 -0700 Subject: [PATCH 1/6] Refactor to rid of OpenStruct --- app/controllers/dashboard_controller.rb | 4 +++- app/models/fixed_expense.rb | 16 +++++++++----- app/views/budget/_hourly_budget.html.erb | 2 +- app/views/budget/_salary_budget.html.erb | 2 +- app/views/dashboard/index.html.erb | 4 ++-- app/views/shared/_budget.html.erb | 28 ++++++++++++------------ app/views/shared/_total_costs.html.erb | 6 ++--- 7 files changed, 34 insertions(+), 28 deletions(-) diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 5c20825..b44486c 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -2,7 +2,9 @@ class DashboardController < ApplicationController def index @incomes = Income.order_by_type @fixed_expenses = FixedExpense.get_ordered - @totals = FixedExpense.total_costs + @total_annual_cost = FixedExpense.total_annual_cost + @total_monthly_cost = FixedExpense.total_monthly_cost + @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost @federal_tax_brackets = FederalTaxBracket.order_by_range @salary_taxed = Income.tax_on_income(income_type: "Salary") @hourly_taxed = Income.tax_on_income(income_type: "Hourly") diff --git a/app/models/fixed_expense.rb b/app/models/fixed_expense.rb index cf1cbfc..866d535 100644 --- a/app/models/fixed_expense.rb +++ b/app/models/fixed_expense.rb @@ -64,12 +64,16 @@ def update_from_dashboard(params:) end end - def self.total_costs - OpenStruct.new( - total_annual_cost: sum(&:annual_cost_cents).to_f / 100, - total_monthly_cost: sum(&:monthly_cost_cents).to_f / 100, - total_bi_weekly_cost: sum(&:bi_weekly_cost_cents).to_f / 100 - ) + def self.total_bi_weekly_cost + Money.new(sum(&:bi_weekly_cost_cents)) + end + + def self.total_monthly_cost + Money.new(sum(&:monthly_cost_cents)) + end + + def self.total_annual_cost + Money.new(sum(&:annual_cost_cents)) end def self.get_ordered diff --git a/app/views/budget/_hourly_budget.html.erb b/app/views/budget/_hourly_budget.html.erb index f369823..9d9007d 100644 --- a/app/views/budget/_hourly_budget.html.erb +++ b/app/views/budget/_hourly_budget.html.erb @@ -2,6 +2,6 @@
Hourly <%= render partial: "budget/budget_headings" %> - <%= render partial: "shared/budget", locals: {totals: totals, income: income} %> + <%= 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 } %>
<% end %> \ No newline at end of file diff --git a/app/views/budget/_salary_budget.html.erb b/app/views/budget/_salary_budget.html.erb index 1f4ee0f..939cc68 100644 --- a/app/views/budget/_salary_budget.html.erb +++ b/app/views/budget/_salary_budget.html.erb @@ -2,6 +2,6 @@
Salary <%= render partial: "budget/budget_headings" %> - <%= render partial: "shared/budget", locals: {totals: totals, income: income} %> + <%= 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 } %>
<% end %> diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb index b5014d5..cf1aac1 100644 --- a/app/views/dashboard/index.html.erb +++ b/app/views/dashboard/index.html.erb @@ -12,7 +12,7 @@ <%= render partial: "components/income_switch" %>
- <%= render partial: "budget/salary_budget", locals: {totals: @totals, income: @salary_taxed} %> + <%= 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 } %>
@@ -50,7 +50,7 @@ <% end %> <%= turbo_frame_tag "total_costs" do %> - <%= render partial: "shared/total_costs", locals: { totals: @totals } %> + <%= 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 } %> <% end %> diff --git a/app/views/shared/_budget.html.erb b/app/views/shared/_budget.html.erb index 96fb5b9..7a1eb2a 100644 --- a/app/views/shared/_budget.html.erb +++ b/app/views/shared/_budget.html.erb @@ -1,70 +1,70 @@
Daily
- <%= render partial: "shared/total", locals: { total: totals.total_annual_cost / 365 } %> + <%= render partial: "shared/total", locals: { total: total_annual_cost / 365 } %>
0
0
- <%= humanized_money_with_symbol((income.daily_income.fractional / 100) - totals.total_annual_cost / 365) %> + <%= humanized_money_with_symbol((income.daily_income) - total_annual_cost / 365) %>
Weekly
- <%= render partial: "shared/total", locals: { total: totals.total_bi_weekly_cost / 2 } %> + <%= render partial: "shared/total", locals: { total: total_annual_cost / 52 } %>
0
0
- <%= humanized_money_with_symbol((income.weekly_income.fractional / 100) - totals.total_bi_weekly_cost / 2) %> + <%= humanized_money_with_symbol((income.weekly_income) - total_annual_cost / 52) %>
Bi-Weekly
- <%= render partial: "shared/total", locals: { total: totals.total_bi_weekly_cost } %> + <%= render partial: "shared/total", locals: { total: total_bi_weekly_cost } %>
0
0
- <%= humanized_money_with_symbol((income.bi_weekly_net_income.fractional / 100) - totals.total_bi_weekly_cost) %> + <%= humanized_money_with_symbol((income.bi_weekly_net_income) - total_bi_weekly_cost) %>
Monthly
- <%= render partial: "shared/total", locals: { total: totals.total_monthly_cost } %> + <%= render partial: "shared/total", locals: { total: total_monthly_cost } %>
0
0
- <%= humanized_money_with_symbol((income.monthly_income.fractional / 100) - totals.total_monthly_cost) %> + <%= humanized_money_with_symbol((income.monthly_income) - total_monthly_cost) %>
Quarterly
- <%= render partial: "shared/total", locals: { total: totals.total_annual_cost / 4 } %> + <%= render partial: "shared/total", locals: { total: total_annual_cost / 4 } %>
0
0
- <%= humanized_money_with_symbol((income.quarterly_income.fractional / 100.0) - totals.total_annual_cost / 4) %> + <%= humanized_money_with_symbol((income.quarterly_income) - total_annual_cost / 4) %>
Biannual
- <%= render partial: "shared/total", locals: { total: totals.total_annual_cost / 4 } %> + <%= render partial: "shared/total", locals: { total: total_annual_cost / 2 } %>
0
0
- <%= humanized_money_with_symbol((income.biannual_income.fractional / 100.0) - totals.total_annual_cost / 4) %> + <%= humanized_money_with_symbol((income.biannual_income) - total_annual_cost / 2) %>
Annually
- <%= render partial: "shared/total", locals: { total: totals.total_annual_cost } %> + <%= render partial: "shared/total", locals: { total: total_annual_cost } %>
0
0
- <%= humanized_money_with_symbol(income.total_net_income.fractional.to_f / 100 - totals.total_annual_cost) %> + <%= humanized_money_with_symbol(income.total_net_income - total_annual_cost ) %>
diff --git a/app/views/shared/_total_costs.html.erb b/app/views/shared/_total_costs.html.erb index 95b24aa..7ba865a 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: @totals.total_annual_cost } %> + <%= render partial: "shared/total", locals: { total: total_annual_cost } %>
- <%= render partial: "shared/total", locals: { total: @totals.total_monthly_cost } %> + <%= render partial: "shared/total", locals: { total: total_monthly_cost } %>
- <%= render partial: "shared/total", locals: { total: @totals.total_bi_weekly_cost } %> + <%= render partial: "shared/total", locals: { total: total_bi_weekly_cost } %>
From 25a482aff5190d91b710c293f07816fed3ed4204 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: Thu, 16 Nov 2023 09:34:46 -0700 Subject: [PATCH 2/6] Refactor user of services --- app/controllers/incomes_controller.rb | 34 ++++++++++------ app/models/income.rb | 4 +- app/services/income_fixed_expense_service.rb | 11 +++++ .../income_tax_calculator_service.rb} | 40 ++++++------------- 4 files changed, 46 insertions(+), 43 deletions(-) create mode 100644 app/services/income_fixed_expense_service.rb rename app/{poros/income_tax_calculator.rb => services/income_tax_calculator_service.rb} (55%) diff --git a/app/controllers/incomes_controller.rb b/app/controllers/incomes_controller.rb index 28c4486..87d3890 100644 --- a/app/controllers/incomes_controller.rb +++ b/app/controllers/incomes_controller.rb @@ -61,31 +61,20 @@ def destroy end def income_switch - totals = FixedExpense.total_costs if params[:enabled] == "0" - salary = Income.tax_on_income(income_type: "Salary") - respond_to do |format| format.turbo_stream { render turbo_stream: turbo_stream.replace("hourly_budget", partial: "budget/salary_budget", - locals: { - totals: totals, - income: salary - }) + locals: build_locals(salary)) } end else - hourly = Income.tax_on_income(income_type: "Hourly") - respond_to do |format| format.turbo_stream { render turbo_stream: turbo_stream.replace("salary_budget", partial: "budget/hourly_budget", - locals: { - totals: totals, - income: hourly - }) + locals: build_locals(hourly)) } end end @@ -102,4 +91,23 @@ def set_income def income_params params.require(:income).permit(:income_type, :rate, :hours, :weekly_income) end + + def build_locals(income) + income_fixed_expense = IncomeFixedExpenseService.new + + { + total_annual_cost: income_fixed_expense.total_annual_cost, + total_monthly_cost: income_fixed_expense.total_monthly_cost, + total_bi_weekly_cost: income_fixed_expense.total_bi_weekly_cost, + income: income + } + end + + def hourly + Income.tax_on_income(income_type: "Hourly") + end + + def salary + Income.tax_on_income(income_type: "Salary") + end end diff --git a/app/models/income.rb b/app/models/income.rb index fa69c73..bd3155c 100644 --- a/app/models/income.rb +++ b/app/models/income.rb @@ -54,8 +54,6 @@ def is_salary? def self.tax_on_income(income_type:) income = Income.find_by(income_type: income_type) - taxable_income = IncomeTaxCalculator.new(income: income) - taxable_income.calculate_taxes - taxable_income + IncomeTaxCalculatorService.new(income: income) end end diff --git a/app/services/income_fixed_expense_service.rb b/app/services/income_fixed_expense_service.rb new file mode 100644 index 0000000..ac67c3c --- /dev/null +++ b/app/services/income_fixed_expense_service.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class IncomeFixedExpenseService + attr_reader :total_annual_cost, :total_monthly_cost, :total_bi_weekly_cost + + def initialize + @total_annual_cost = FixedExpense.total_annual_cost + @total_monthly_cost = FixedExpense.total_monthly_cost + @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost + end +end diff --git a/app/poros/income_tax_calculator.rb b/app/services/income_tax_calculator_service.rb similarity index 55% rename from app/poros/income_tax_calculator.rb rename to app/services/income_tax_calculator_service.rb index 0a1f112..e98be81 100644 --- a/app/poros/income_tax_calculator.rb +++ b/app/services/income_tax_calculator_service.rb @@ -1,4 +1,4 @@ -class IncomeTaxCalculator +class IncomeTaxCalculatorService attr_reader :income, :annual_income, :federal_tax, @@ -14,20 +14,7 @@ class IncomeTaxCalculator def initialize(income:) @income = income - @annual_income = income.weekly_income_cents * 52 - @federal_tax = nil - @net_after_fed_tax = nil - @state_tax = nil - @total_net_income = nil - @bi_weekly_net_income = nil - @daily_income = nil - @weekly_income = nil - @monthly_income = nil - @quarterly_income = nil - @biannual_income = nil - end - - def calculate_taxes + @annual_income = income.weekly_income * 52 @federal_tax = calculate_fed_tax @net_after_fed_tax = calculate_net_after_fed_tax @state_tax = calculate_state_tax @@ -43,45 +30,44 @@ def calculate_taxes private def calculate_fed_tax - bracket = FederalTaxBracket.where("bottom_range_cents <= ?", @annual_income).order(:bottom_range_cents).last + bracket = FederalTaxBracket.where("bottom_range_cents <= ?", @annual_income.cents).order(:bottom_range_cents).last rated = bracket.rate * @annual_income - Money.new(rated + bracket.cumulative_cents, "USD") + rated + bracket.cumulative end def calculate_state_tax - state_tax = (@net_after_fed_tax.fractional * 0.0463).to_i - Money.new(state_tax, "USD") + @net_after_fed_tax * 0.0463 end def calculate_net_after_fed_tax - Money.new(@annual_income - @federal_tax.fractional, "USD") + @annual_income - @federal_tax end def calculate_total_net_income - Money.new(@net_after_fed_tax.fractional - @state_tax.fractional, "USD") + @net_after_fed_tax - @state_tax end def calculate_bi_weekly_income - Money.new(@total_net_income.fractional / 26) + @total_net_income / 26 end def calculate_daily_income - Money.new(@total_net_income.fractional / 365) + @total_net_income / 365 end def calculate_weekly_income - Money.new(@total_net_income.fractional / 52) + @total_net_income / 52 end def calculate_monthly_income - Money.new(@total_net_income.fractional / 12) + @total_net_income / 12 end def calculate_quarterly_income - Money.new(@total_net_income.fractional / 4) + @total_net_income / 4 end def calculate_biannual_income - Money.new(@total_net_income.fractional / 2) + @total_net_income / 2 end end From 35aa50adb195828e2abcc90503628502cc3c0be7 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: Thu, 16 Nov 2023 09:47:36 -0700 Subject: [PATCH 3/6] Find and replace all @totals --- app/controllers/fixed_expenses_controller.rb | 13 ++++++++++--- app/controllers/incomes_controller.rb | 5 ++++- app/views/fixed_expenses/create.turbo_stream.erb | 6 +++--- app/views/fixed_expenses/destroy.turbo_stream.erb | 6 +++--- app/views/fixed_expenses/update.turbo_stream.erb | 6 +++--- app/views/incomes/update.turbo_stream.erb | 4 ++-- 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/app/controllers/fixed_expenses_controller.rb b/app/controllers/fixed_expenses_controller.rb index 08c7ba2..859af58 100644 --- a/app/controllers/fixed_expenses_controller.rb +++ b/app/controllers/fixed_expenses_controller.rb @@ -25,7 +25,10 @@ def create respond_to do |format| if @fixed_expense.save - @totals = FixedExpense.total_costs + @fixed_expenses = FixedExpense.get_ordered + @total_annual_cost = FixedExpense.total_annual_cost + @total_monthly_cost = FixedExpense.total_monthly_cost + @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost @fixed_expenses = FixedExpense.get_ordered @salary_taxed = Income.tax_on_income(income_type: "Salary") @hourly_taxed = Income.tax_on_income(income_type: "Hourly") @@ -42,7 +45,9 @@ def create def update respond_to do |format| if @fixed_expense.update_from_dashboard(params: params[:fixed_expense]) - @totals = FixedExpense.total_costs + @total_annual_cost = FixedExpense.total_annual_cost + @total_monthly_cost = FixedExpense.total_monthly_cost + @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost @salary_taxed = Income.tax_on_income(income_type: "Salary") @hourly_taxed = Income.tax_on_income(income_type: "Hourly") format.html { redirect_to root_path, notice: "Fixed expense was successfully updated." } @@ -57,7 +62,9 @@ def update # DELETE /fixed_expenses/1 or /fixed_expenses/1.json def destroy @fixed_expense.destroy - @totals = FixedExpense.total_costs + @total_annual_cost = FixedExpense.total_annual_cost + @total_monthly_cost = FixedExpense.total_monthly_cost + @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost @fixed_expenses = FixedExpense.get_ordered @salary_taxed = Income.tax_on_income(income_type: "Salary") @hourly_taxed = Income.tax_on_income(income_type: "Hourly") diff --git a/app/controllers/incomes_controller.rb b/app/controllers/incomes_controller.rb index 87d3890..4fb2747 100644 --- a/app/controllers/incomes_controller.rb +++ b/app/controllers/incomes_controller.rb @@ -40,7 +40,10 @@ def update if @income.update_from_dashboard(params: params) @salary_taxed = Income.tax_on_income(income_type: "Salary") @hourly_taxed = Income.tax_on_income(income_type: "Hourly") - @totals = FixedExpense.total_costs + @fixed_expenses = FixedExpense.get_ordered + @total_annual_cost = FixedExpense.total_annual_cost + @total_monthly_cost = FixedExpense.total_monthly_cost + @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost format.html { redirect_to root_path, notice: "Income was successfully updated." } format.turbo_stream else diff --git a/app/views/fixed_expenses/create.turbo_stream.erb b/app/views/fixed_expenses/create.turbo_stream.erb index 65e874d..8a9a854 100644 --- a/app/views/fixed_expenses/create.turbo_stream.erb +++ b/app/views/fixed_expenses/create.turbo_stream.erb @@ -3,13 +3,13 @@ <% end %> <%= turbo_stream.replace "total_costs" do %> - <%= render partial: "shared/total_costs", locals: { totals: @totals } %> + <%= 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 } %> <% end %> <%= turbo_stream.replace "salary_budget" do %> - <%= render partial: "budget/salary_budget", locals: {totals: @totals, income: @salary_taxed} %> + <%= 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} %> <% end %> <%= turbo_stream.replace "hourly_budget" do %> - <%= render partial: "budget/hourly_budget", locals: {totals: @totals, income: @hourly_taxed} %> + <%= 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} %> <% end %> diff --git a/app/views/fixed_expenses/destroy.turbo_stream.erb b/app/views/fixed_expenses/destroy.turbo_stream.erb index 5562d82..44ec110 100644 --- a/app/views/fixed_expenses/destroy.turbo_stream.erb +++ b/app/views/fixed_expenses/destroy.turbo_stream.erb @@ -1,13 +1,13 @@ <%= turbo_stream.remove @fixed_expense %> <%= turbo_stream.replace "total_costs" do %> - <%= render partial: "shared/total_costs", locals: { totals: @totals } %> + <%= 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 } %> <% end %> <%= turbo_stream.replace "salary_budget" do %> - <%= render partial: "budget/salary_budget", locals: {totals: @totals, income: @salary_taxed} %> + <%= 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} %> <% end %> <%= turbo_stream.replace "hourly_budget" do %> - <%= render partial: "budget/hourly_budget", locals: {totals: @totals, income: @hourly_taxed} %> + <%= 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} %> <% end %> diff --git a/app/views/fixed_expenses/update.turbo_stream.erb b/app/views/fixed_expenses/update.turbo_stream.erb index e9db543..b431b7a 100644 --- a/app/views/fixed_expenses/update.turbo_stream.erb +++ b/app/views/fixed_expenses/update.turbo_stream.erb @@ -1,13 +1,13 @@ <%= turbo_stream.update @fixed_expense %> <%= turbo_stream.replace "total_costs" do %> - <%= render partial: "shared/total_costs", locals: { totals: @totals } %> + <%= 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 } %> <% end %> <%= turbo_stream.replace "salary_budget" do %> - <%= render partial: "budget/salary_budget", locals: {totals: @totals, income: @salary_taxed} %> + <%= 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} %> <% end %> <%= turbo_stream.replace "hourly_budget" do %> - <%= render partial: "budget/hourly_budget", locals: {totals: @totals, income: @hourly_taxed} %> + <%= 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} %> <% end %> diff --git a/app/views/incomes/update.turbo_stream.erb b/app/views/incomes/update.turbo_stream.erb index d10fb4c..435496e 100644 --- a/app/views/incomes/update.turbo_stream.erb +++ b/app/views/incomes/update.turbo_stream.erb @@ -5,9 +5,9 @@ <% end %> <%= turbo_stream.replace "salary_budget" do %> - <%= render partial: "budget/salary_budget", locals: {totals: @totals, income: @salary_taxed} %> + <%= 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} %> <% end %> <%= turbo_stream.replace "hourly_budget" do %> - <%= render partial: "budget/hourly_budget", locals: {totals: @totals, income: @hourly_taxed} %> + <%= 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} %> <% end %> \ No newline at end of file From 13eec432f8b86b88703416ed42fce72faaa238db 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: Thu, 16 Nov 2023 11:53:21 -0700 Subject: [PATCH 4/6] Extract methods to concerns --- app/controllers/concerns/taxed_income.rb | 19 +++++++++++++ app/controllers/concerns/total_cost.rb | 27 ++++++++++++++++++ app/controllers/dashboard_controller.rb | 11 ++++--- app/controllers/fixed_expenses_controller.rb | 24 ++++++---------- app/controllers/incomes_controller.rb | 30 +++++++------------- app/models/fixed_expense.rb | 12 -------- app/models/income.rb | 5 ---- app/services/income_fixed_expense_service.rb | 11 ------- 8 files changed, 69 insertions(+), 70 deletions(-) create mode 100644 app/controllers/concerns/taxed_income.rb create mode 100644 app/controllers/concerns/total_cost.rb delete mode 100644 app/services/income_fixed_expense_service.rb diff --git a/app/controllers/concerns/taxed_income.rb b/app/controllers/concerns/taxed_income.rb new file mode 100644 index 0000000..cab2c8a --- /dev/null +++ b/app/controllers/concerns/taxed_income.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module TaxedIncome + def tax_on_salary + income = Income.find_by(income_type: "Salary") + Rails.logger.debug "**** Tax Salary: #{income.inspect}" + IncomeTaxCalculatorService.new(income: income) + end + + def tax_on_hourly + income = Income.find_by(income_type: "Hourly") + IncomeTaxCalculatorService.new(income: income) + end + + def build_taxed_income_vars! + @salary_taxed = tax_on_salary + @hourly_taxed = tax_on_hourly + end +end diff --git a/app/controllers/concerns/total_cost.rb b/app/controllers/concerns/total_cost.rb new file mode 100644 index 0000000..003e2f9 --- /dev/null +++ b/app/controllers/concerns/total_cost.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module TotalCost + def total_annual_cost + Money.new(get_sum(:annual_cost_cents)) + end + + def total_monthly_cost + Money.new(get_sum(:monthly_cost_cents)) + end + + def total_bi_weekly_cost + Money.new(get_sum(:bi_weekly_cost_cents)) + 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 + end + + private + + def get_sum(time_period) + FixedExpense.sum(&time_period) + end +end diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index b44486c..397b31f 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -1,12 +1,11 @@ class DashboardController < ApplicationController + include TotalCost + include TaxedIncome + def index @incomes = Income.order_by_type @fixed_expenses = FixedExpense.get_ordered - @total_annual_cost = FixedExpense.total_annual_cost - @total_monthly_cost = FixedExpense.total_monthly_cost - @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost - @federal_tax_brackets = FederalTaxBracket.order_by_range - @salary_taxed = Income.tax_on_income(income_type: "Salary") - @hourly_taxed = Income.tax_on_income(income_type: "Hourly") + build_taxed_income_vars! + build_total_cost_vars! end end diff --git a/app/controllers/fixed_expenses_controller.rb b/app/controllers/fixed_expenses_controller.rb index 859af58..c17a388 100644 --- a/app/controllers/fixed_expenses_controller.rb +++ b/app/controllers/fixed_expenses_controller.rb @@ -1,4 +1,6 @@ class FixedExpensesController < ApplicationController + include TotalCost + include TaxedIncome before_action :set_fixed_expense, only: %i[show edit update destroy] # GET /fixed_expenses or /fixed_expenses.json @@ -26,12 +28,8 @@ def create respond_to do |format| if @fixed_expense.save @fixed_expenses = FixedExpense.get_ordered - @total_annual_cost = FixedExpense.total_annual_cost - @total_monthly_cost = FixedExpense.total_monthly_cost - @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost - @fixed_expenses = FixedExpense.get_ordered - @salary_taxed = Income.tax_on_income(income_type: "Salary") - @hourly_taxed = Income.tax_on_income(income_type: "Hourly") + build_taxed_income_vars! + build_total_cost_vars! format.html { redirect_to root_path, notice: "Fixed expense was successfully created." } format.turbo_stream else @@ -45,11 +43,8 @@ def create def update respond_to do |format| if @fixed_expense.update_from_dashboard(params: params[:fixed_expense]) - @total_annual_cost = FixedExpense.total_annual_cost - @total_monthly_cost = FixedExpense.total_monthly_cost - @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost - @salary_taxed = Income.tax_on_income(income_type: "Salary") - @hourly_taxed = Income.tax_on_income(income_type: "Hourly") + build_taxed_income_vars! + build_total_cost_vars! format.html { redirect_to root_path, notice: "Fixed expense was successfully updated." } format.turbo_stream else @@ -62,12 +57,9 @@ def update # DELETE /fixed_expenses/1 or /fixed_expenses/1.json def destroy @fixed_expense.destroy - @total_annual_cost = FixedExpense.total_annual_cost - @total_monthly_cost = FixedExpense.total_monthly_cost - @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost + build_taxed_income_vars! + build_total_cost_vars! @fixed_expenses = FixedExpense.get_ordered - @salary_taxed = Income.tax_on_income(income_type: "Salary") - @hourly_taxed = Income.tax_on_income(income_type: "Hourly") respond_to do |format| format.html { redirect_to fixed_expenses_path, notice: "Fixed expense was successfully destroyed." } format.turbo_stream diff --git a/app/controllers/incomes_controller.rb b/app/controllers/incomes_controller.rb index 4fb2747..01ef703 100644 --- a/app/controllers/incomes_controller.rb +++ b/app/controllers/incomes_controller.rb @@ -1,4 +1,6 @@ class IncomesController < ApplicationController + include TotalCost + include TaxedIncome before_action :set_income, only: %i[show edit update destroy] # GET /incomes or /incomes.json @@ -38,12 +40,9 @@ def create def update respond_to do |format| if @income.update_from_dashboard(params: params) - @salary_taxed = Income.tax_on_income(income_type: "Salary") - @hourly_taxed = Income.tax_on_income(income_type: "Hourly") @fixed_expenses = FixedExpense.get_ordered - @total_annual_cost = FixedExpense.total_annual_cost - @total_monthly_cost = FixedExpense.total_monthly_cost - @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost + build_taxed_income_vars! + build_total_cost_vars! format.html { redirect_to root_path, notice: "Income was successfully updated." } format.turbo_stream else @@ -69,7 +68,7 @@ def income_switch format.turbo_stream { render turbo_stream: turbo_stream.replace("hourly_budget", partial: "budget/salary_budget", - locals: build_locals(salary)) + locals: build_locals(tax_on_salary)) } end else @@ -77,7 +76,7 @@ def income_switch format.turbo_stream { render turbo_stream: turbo_stream.replace("salary_budget", partial: "budget/hourly_budget", - locals: build_locals(hourly)) + locals: build_locals(tax_on_hourly)) } end end @@ -96,21 +95,12 @@ def income_params end def build_locals(income) - income_fixed_expense = IncomeFixedExpenseService.new - + build_total_cost_vars! { - total_annual_cost: income_fixed_expense.total_annual_cost, - total_monthly_cost: income_fixed_expense.total_monthly_cost, - total_bi_weekly_cost: income_fixed_expense.total_bi_weekly_cost, + total_annual_cost: @total_annual_cost, + total_monthly_cost: @total_monthly_cost, + total_bi_weekly_cost: @total_bi_weekly_cost, income: income } end - - def hourly - Income.tax_on_income(income_type: "Hourly") - end - - def salary - Income.tax_on_income(income_type: "Salary") - end end diff --git a/app/models/fixed_expense.rb b/app/models/fixed_expense.rb index 866d535..6762d6f 100644 --- a/app/models/fixed_expense.rb +++ b/app/models/fixed_expense.rb @@ -64,18 +64,6 @@ def update_from_dashboard(params:) end end - def self.total_bi_weekly_cost - Money.new(sum(&:bi_weekly_cost_cents)) - end - - def self.total_monthly_cost - Money.new(sum(&:monthly_cost_cents)) - end - - def self.total_annual_cost - Money.new(sum(&:annual_cost_cents)) - end - def self.get_ordered all.order(annual_cost_cents: :desc) end diff --git a/app/models/income.rb b/app/models/income.rb index bd3155c..c3bab69 100644 --- a/app/models/income.rb +++ b/app/models/income.rb @@ -51,9 +51,4 @@ def is_hourly? def is_salary? income_type == "Salary" end - - def self.tax_on_income(income_type:) - income = Income.find_by(income_type: income_type) - IncomeTaxCalculatorService.new(income: income) - end end diff --git a/app/services/income_fixed_expense_service.rb b/app/services/income_fixed_expense_service.rb deleted file mode 100644 index ac67c3c..0000000 --- a/app/services/income_fixed_expense_service.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -class IncomeFixedExpenseService - attr_reader :total_annual_cost, :total_monthly_cost, :total_bi_weekly_cost - - def initialize - @total_annual_cost = FixedExpense.total_annual_cost - @total_monthly_cost = FixedExpense.total_monthly_cost - @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost - end -end From 27504b52251848e81a2f6af6fb2adb03b7a94d0f 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: Thu, 16 Nov 2023 17:03:49 -0700 Subject: [PATCH 5/6] Style buttons --- app/assets/stylesheets/application.tailwind.css | 4 ++++ app/views/federal_tax_brackets/_form.html.erb | 4 ++-- app/views/fixed_expenses/_form.html.erb | 4 ++-- app/views/incomes/_form.html.erb | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/assets/stylesheets/application.tailwind.css b/app/assets/stylesheets/application.tailwind.css index 2d19c26..3f7b012 100644 --- a/app/assets/stylesheets/application.tailwind.css +++ b/app/assets/stylesheets/application.tailwind.css @@ -11,6 +11,10 @@ @apply rounded-lg px-4 bg-red-600 inline-block font-medium text-white; } + .btn-secondary { + @apply rounded-lg px-4 bg-blue-600 inline-block font-medium text-white; + } + h1 { @apply text-3xl font-bold tracking-tight text-gray-800 } diff --git a/app/views/federal_tax_brackets/_form.html.erb b/app/views/federal_tax_brackets/_form.html.erb index 48ed016..eea5af6 100644 --- a/app/views/federal_tax_brackets/_form.html.erb +++ b/app/views/federal_tax_brackets/_form.html.erb @@ -37,10 +37,10 @@
- <%= form.submit class: "rounded-lg py-1 px-4 bg-gray-100 inline-block font-medium" %> + <%= form.submit class: "btn btn-primary" %>
- <%= link_to "Cancel", fixed_expenses_path, class: "rounded-lg py-1 px-4 bg-gray-100 inline-block font-medium" %> + <%= link_to "Cancel", fixed_expenses_path, class: "btn btn-secondary" %>
<% end %> diff --git a/app/views/fixed_expenses/_form.html.erb b/app/views/fixed_expenses/_form.html.erb index 1bbf74f..c9646ef 100644 --- a/app/views/fixed_expenses/_form.html.erb +++ b/app/views/fixed_expenses/_form.html.erb @@ -35,10 +35,10 @@
- <%= form.submit class: "rounded-lg py-1 px-4 bg-gray-100 inline-block font-medium" %> + <%= form.submit class: "btn btn-primary" %>
- <%= link_to "Cancel", root_path, class: "rounded-lg py-1 px-4 bg-gray-100 inline-block font-medium", data:{ turbo_frame: :fixed_expenses } %> + <%= link_to "Cancel", root_path, class: "btn btn-secondary", data:{ turbo_frame: :fixed_expenses } %>
diff --git a/app/views/incomes/_form.html.erb b/app/views/incomes/_form.html.erb index 9c53ff6..9d0da47 100644 --- a/app/views/incomes/_form.html.erb +++ b/app/views/incomes/_form.html.erb @@ -34,10 +34,10 @@
- <%= form.submit class: "rounded-lg px-2 bg-gray-100 inline-block font-medium" %> + <%= form.submit class: "btn btn-primary" %>
- <%= link_to "Cancel", root_path, class: "rounded-lg px-2 bg-gray-100 inline-block font-medium" %> + <%= link_to "Cancel", root_path, class: "btn btn-secondary" %>
From ae86bcb168204971ec72ad5625330630151201cc 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: Thu, 16 Nov 2023 17:13:15 -0700 Subject: [PATCH 6/6] Refactor building variables --- app/controllers/concerns/dashboard_builder.rb | 13 +++++++++++++ app/controllers/dashboard_controller.rb | 8 ++------ app/controllers/fixed_expenses_controller.rb | 14 +++++--------- app/controllers/incomes_controller.rb | 7 +++---- app/models/fixed_expense.rb | 6 ++---- app/models/income.rb | 6 ++---- 6 files changed, 27 insertions(+), 27 deletions(-) create mode 100644 app/controllers/concerns/dashboard_builder.rb diff --git a/app/controllers/concerns/dashboard_builder.rb b/app/controllers/concerns/dashboard_builder.rb new file mode 100644 index 0000000..b387e7f --- /dev/null +++ b/app/controllers/concerns/dashboard_builder.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module DashboardBuilder + include TaxedIncome + include TotalCost + + def build_dashboard_variables! + @incomes = Income.order_by_type + @fixed_expenses = FixedExpense.get_ordered + build_taxed_income_vars! + build_total_cost_vars! + end +end diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 397b31f..d069d72 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -1,11 +1,7 @@ class DashboardController < ApplicationController - include TotalCost - include TaxedIncome + include DashboardBuilder def index - @incomes = Income.order_by_type - @fixed_expenses = FixedExpense.get_ordered - build_taxed_income_vars! - build_total_cost_vars! + build_dashboard_variables! end end diff --git a/app/controllers/fixed_expenses_controller.rb b/app/controllers/fixed_expenses_controller.rb index c17a388..37e7367 100644 --- a/app/controllers/fixed_expenses_controller.rb +++ b/app/controllers/fixed_expenses_controller.rb @@ -1,6 +1,6 @@ class FixedExpensesController < ApplicationController - include TotalCost - include TaxedIncome + include DashboardBuilder + before_action :set_fixed_expense, only: %i[show edit update destroy] # GET /fixed_expenses or /fixed_expenses.json @@ -27,9 +27,7 @@ def create respond_to do |format| if @fixed_expense.save - @fixed_expenses = FixedExpense.get_ordered - build_taxed_income_vars! - build_total_cost_vars! + build_dashboard_variables! format.html { redirect_to root_path, notice: "Fixed expense was successfully created." } format.turbo_stream else @@ -43,8 +41,7 @@ def create def update respond_to do |format| if @fixed_expense.update_from_dashboard(params: params[:fixed_expense]) - build_taxed_income_vars! - build_total_cost_vars! + build_dashboard_variables! format.html { redirect_to root_path, notice: "Fixed expense was successfully updated." } format.turbo_stream else @@ -57,8 +54,7 @@ def update # DELETE /fixed_expenses/1 or /fixed_expenses/1.json def destroy @fixed_expense.destroy - build_taxed_income_vars! - build_total_cost_vars! + build_dashboard_variables! @fixed_expenses = FixedExpense.get_ordered respond_to do |format| format.html { redirect_to fixed_expenses_path, notice: "Fixed expense was successfully destroyed." } diff --git a/app/controllers/incomes_controller.rb b/app/controllers/incomes_controller.rb index 01ef703..aab5e66 100644 --- a/app/controllers/incomes_controller.rb +++ b/app/controllers/incomes_controller.rb @@ -1,6 +1,7 @@ class IncomesController < ApplicationController + include DashboardBuilder include TotalCost - include TaxedIncome + before_action :set_income, only: %i[show edit update destroy] # GET /incomes or /incomes.json @@ -40,9 +41,7 @@ def create def update respond_to do |format| if @income.update_from_dashboard(params: params) - @fixed_expenses = FixedExpense.get_ordered - build_taxed_income_vars! - build_total_cost_vars! + build_dashboard_variables! format.html { redirect_to root_path, notice: "Income was successfully updated." } format.turbo_stream else diff --git a/app/models/fixed_expense.rb b/app/models/fixed_expense.rb index 6762d6f..4527a37 100644 --- a/app/models/fixed_expense.rb +++ b/app/models/fixed_expense.rb @@ -18,6 +18,8 @@ class FixedExpense < ApplicationRecord monetize :monthly_cost_cents monetize :bi_weekly_cost_cents + scope :get_ordered, -> { order(annual_cost_cents: :desc) } + def self.new_from_dashboard(params:) expense = params[:fixed_expense] amount = (expense[:amount].to_f * 100).to_i @@ -63,8 +65,4 @@ def update_from_dashboard(params:) false end end - - def self.get_ordered - all.order(annual_cost_cents: :desc) - end end diff --git a/app/models/income.rb b/app/models/income.rb index c3bab69..ecc52dd 100644 --- a/app/models/income.rb +++ b/app/models/income.rb @@ -16,6 +16,8 @@ class Income < ApplicationRecord monetize :rate_cents monetize :weekly_income_cents + scope :order_by_type, -> { order(income_type: :desc) } + def update_from_dashboard(params:) income = params[:income] income_type_passed = income[:income_type] @@ -40,10 +42,6 @@ def update_from_dashboard(params:) end end - def self.order_by_type - Income.all.order(income_type: :desc) - end - def is_hourly? income_type == "Hourly" end