diff --git a/app/controllers/concerns/dashboard_concern.rb b/app/controllers/concerns/dashboard_concern.rb new file mode 100644 index 0000000..1b465f3 --- /dev/null +++ b/app/controllers/concerns/dashboard_concern.rb @@ -0,0 +1,15 @@ +module DashboardConcern + extend ActiveSupport::Concern + + included do + end + + def build_instance_variables + @incomes = Income.order_by_type + @fixed_expenses = FixedExpense.get_ordered + @totals = FixedExpense.total_costs + @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") + end +end diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 5c20825..8ac89e6 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -1,10 +1,6 @@ class DashboardController < ApplicationController + include DashboardConcern def index - @incomes = Income.order_by_type - @fixed_expenses = FixedExpense.get_ordered - @totals = FixedExpense.total_costs - @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_instance_variables end end diff --git a/app/controllers/fixed_expenses_controller.rb b/app/controllers/fixed_expenses_controller.rb index 08c7ba2..ec1c0ce 100644 --- a/app/controllers/fixed_expenses_controller.rb +++ b/app/controllers/fixed_expenses_controller.rb @@ -1,4 +1,5 @@ class FixedExpensesController < ApplicationController + include DashboardConcern before_action :set_fixed_expense, only: %i[show edit update destroy] # GET /fixed_expenses or /fixed_expenses.json @@ -25,10 +26,7 @@ def create respond_to do |format| if @fixed_expense.save - @totals = FixedExpense.total_costs - @fixed_expenses = FixedExpense.get_ordered - @salary_taxed = Income.tax_on_income(income_type: "Salary") - @hourly_taxed = Income.tax_on_income(income_type: "Hourly") + build_instance_variables format.html { redirect_to root_path, notice: "Fixed expense was successfully created." } format.turbo_stream else @@ -42,9 +40,7 @@ def create def update respond_to do |format| if @fixed_expense.update_from_dashboard(params: params[:fixed_expense]) - @totals = FixedExpense.total_costs - @salary_taxed = Income.tax_on_income(income_type: "Salary") - @hourly_taxed = Income.tax_on_income(income_type: "Hourly") + build_instance_variables format.html { redirect_to root_path, notice: "Fixed expense was successfully updated." } format.turbo_stream else @@ -57,10 +53,7 @@ def update # DELETE /fixed_expenses/1 or /fixed_expenses/1.json def destroy @fixed_expense.destroy - @totals = FixedExpense.total_costs - @fixed_expenses = FixedExpense.get_ordered - @salary_taxed = Income.tax_on_income(income_type: "Salary") - @hourly_taxed = Income.tax_on_income(income_type: "Hourly") + build_instance_variables 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 1e34b9d..80bfe29 100644 --- a/app/controllers/incomes_controller.rb +++ b/app/controllers/incomes_controller.rb @@ -1,4 +1,5 @@ class IncomesController < ApplicationController + include DashboardConcern before_action :set_income, only: %i[show edit update destroy] # GET /incomes or /incomes.json @@ -38,9 +39,7 @@ 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") - @totals = FixedExpense.total_costs + build_instance_variables format.html { redirect_to root_path, notice: "Income was successfully updated." } format.turbo_stream else