-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add route, controller, and form for saving types * Migrate savings rate * Migrate savings rate * Add savings/investing and render on dashboard * lint * Update factory and test suite * Lint
- Loading branch information
Showing
36 changed files
with
778 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module DashboardBuilder | ||
extend ActiveSupport::Concern | ||
include TaxedIncome | ||
include TotalCost | ||
include SaveIncome | ||
|
||
def build_dashboard_variables! | ||
@incomes = Income.order_by_type | ||
@fixed_expenses = FixedExpense.get_ordered | ||
@savings_rate = SavingsRate.savings | ||
@investing_rate = SavingsRate.investing | ||
build_taxed_income_vars! | ||
build_savings_vars! | ||
build_total_cost_vars! | ||
Rails.logger.debug "\n *** Building Vars!!\n " | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module SaveIncome | ||
extend ActiveSupport::Concern | ||
include TaxedIncome | ||
|
||
def build_savings_vars! | ||
@hourly_saving = hourly_saving | ||
@hourly_invest = hourly_investing | ||
@salary_saving = salary_saving | ||
@salary_invest = salary_investing | ||
end | ||
|
||
def salary_investing | ||
SavingsCalculator.new(tax_on_salary, set_invest_rate) | ||
end | ||
|
||
def salary_saving | ||
SavingsCalculator.new(tax_on_salary, set_save_rate) | ||
end | ||
|
||
def hourly_investing | ||
SavingsCalculator.new(tax_on_hourly, set_invest_rate) | ||
end | ||
|
||
def hourly_saving | ||
SavingsCalculator.new(tax_on_hourly, set_save_rate) | ||
end | ||
|
||
private | ||
|
||
def set_save_rate | ||
SavingsRate.savings.rate | ||
end | ||
|
||
def set_invest_rate | ||
SavingsRate.investing.rate | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
class SavingsRatesController < ApplicationController | ||
include DashboardBuilder | ||
|
||
before_action :set_savings_rate, only: %i[show edit update destroy] | ||
|
||
# GET /savings_rates or /savings_rates.json | ||
def index | ||
@savings_rates = SavingsRate.all | ||
end | ||
|
||
# GET /savings_rates/1 or /savings_rates/1.json | ||
def show | ||
end | ||
|
||
# GET /savings_rates/new | ||
def new | ||
@savings_rate = SavingsRate.new | ||
end | ||
|
||
# GET /savings_rates/1/edit | ||
def edit | ||
end | ||
|
||
# POST /savings_rates or /savings_rates.json | ||
def create | ||
@savings_rate = SavingsRate.new(savings_rate_params) | ||
|
||
respond_to do |format| | ||
if @savings_rate.save | ||
format.html { redirect_to savings_rate_url(@savings_rate), notice: "Savings rate was successfully created." } | ||
format.json { render :show, status: :created, location: @savings_rate } | ||
else | ||
format.html { render :new, status: :unprocessable_entity } | ||
format.json { render json: @savings_rate.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /savings_rates/1 or /savings_rates/1.json | ||
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 | ||
format.html { render :edit, status: :unprocessable_entity } | ||
format.json { render json: @savings_rate.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /savings_rates/1 or /savings_rates/1.json | ||
def destroy | ||
@savings_rate.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to savings_rates_url, notice: "Savings rate was successfully destroyed." } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
|
||
# Use callbacks to share common setup or constraints between actions. | ||
def set_savings_rate | ||
@savings_rate = SavingsRate.find(params[:id]) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def savings_rate_params | ||
params.require(:savings_rate).permit(:name, :rate) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module SavingsRatesHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# == Schema Information | ||
# | ||
# Table name: savings_rates | ||
# | ||
# id :bigint not null, primary key | ||
# name :string | ||
# rate :float | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class SavingsRate < ApplicationRecord | ||
SAVING_TYPES = %w[savings investing] | ||
validates :name, presence: true, inclusion: SAVING_TYPES | ||
|
||
def self.savings | ||
find_by(name: "savings") | ||
end | ||
|
||
def self.investing | ||
find_by(name: "investing") | ||
end | ||
|
||
def display_rate | ||
(rate * 100) | ||
end | ||
|
||
def update_from_dashboard(params:) | ||
rate = params[:rate].to_f / 100 | ||
update(rate: rate) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
class SavingsCalculator | ||
attr_reader :saving_amount, | ||
:annual_saving, | ||
:biannual_saving, | ||
:quarterly_saving, | ||
:monthly_saving, | ||
:bi_weekly_saving, | ||
:weekly_saving, | ||
:daily_saving | ||
|
||
def initialize(income_type, saving_rate) | ||
@annual_income = income_type.annual_income | ||
@saving_rate = saving_rate | ||
@annual_saving = calculate_savings | ||
@bi_weekly_saving = calculate_bi_weekly_saving | ||
@daily_saving = calculate_daily_saving | ||
@weekly_saving = calculate_weekly_saving | ||
@monthly_saving = calculate_monthly_saving | ||
@quarterly_saving = calculate_quarterly_saving | ||
@biannual_saving = calculate_biannual_saving | ||
end | ||
|
||
def calculate_savings | ||
@annual_income * @saving_rate | ||
end | ||
|
||
def calculate_daily_saving | ||
@annual_saving / 365 | ||
end | ||
|
||
def calculate_weekly_saving | ||
@annual_saving / 52 | ||
end | ||
|
||
def calculate_bi_weekly_saving | ||
@annual_saving / 26 | ||
end | ||
|
||
def calculate_monthly_saving | ||
@annual_saving / 12 | ||
end | ||
|
||
def calculate_quarterly_saving | ||
@annual_saving / 4 | ||
end | ||
|
||
def calculate_biannual_saving | ||
@annual_saving / 2 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
<%= turbo_frame_tag "hourly_budget" do %> | ||
<div id="hourly_budget" class="py-4"> | ||
|
||
<strong>Hourly</strong> | ||
<%= 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 } %> | ||
<%= 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, | ||
investing_amount: investing_amount, | ||
savings_amount: savings_amount | ||
} %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.