Skip to content

Commit

Permalink
Refactor building variables
Browse files Browse the repository at this point in the history
  • Loading branch information
neb417 committed Nov 17, 2023
1 parent 27504b5 commit ae86bcb
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
13 changes: 13 additions & 0 deletions app/controllers/concerns/dashboard_builder.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 2 additions & 6 deletions app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 5 additions & 9 deletions app/controllers/fixed_expenses_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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." }
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/incomes_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions app/models/fixed_expense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,8 +65,4 @@ def update_from_dashboard(params:)
false
end
end

def self.get_ordered
all.order(annual_cost_cents: :desc)
end
end
6 changes: 2 additions & 4 deletions app/models/income.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down

0 comments on commit ae86bcb

Please sign in to comment.