Track expenses
This commit is contained in:
parent
7521ebaa45
commit
e588ab3db0
19 changed files with 374 additions and 0 deletions
70
app/controllers/expenses_controller.rb
Normal file
70
app/controllers/expenses_controller.rb
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
class ExpensesController < ApplicationController
|
||||||
|
before_action :set_expense, only: %i[ show edit update destroy ]
|
||||||
|
|
||||||
|
# GET /expenses or /expenses.json
|
||||||
|
def index
|
||||||
|
@expenses = Expense.all
|
||||||
|
end
|
||||||
|
|
||||||
|
# GET /expenses/1 or /expenses/1.json
|
||||||
|
def show
|
||||||
|
end
|
||||||
|
|
||||||
|
# GET /expenses/new
|
||||||
|
def new
|
||||||
|
@expense = Expense.new
|
||||||
|
end
|
||||||
|
|
||||||
|
# GET /expenses/1/edit
|
||||||
|
def edit
|
||||||
|
end
|
||||||
|
|
||||||
|
# POST /expenses or /expenses.json
|
||||||
|
def create
|
||||||
|
@expense = Expense.new(expense_params)
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
if @expense.save
|
||||||
|
format.html { redirect_to expense_url(@expense), notice: "Expense was successfully created." }
|
||||||
|
format.json { render :show, status: :created, location: @expense }
|
||||||
|
else
|
||||||
|
format.html { render :new, status: :unprocessable_entity }
|
||||||
|
format.json { render json: @expense.errors, status: :unprocessable_entity }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# PATCH/PUT /expenses/1 or /expenses/1.json
|
||||||
|
def update
|
||||||
|
respond_to do |format|
|
||||||
|
if @expense.update(expense_params)
|
||||||
|
format.html { redirect_to expense_url(@expense), notice: "Expense was successfully updated." }
|
||||||
|
format.json { render :show, status: :ok, location: @expense }
|
||||||
|
else
|
||||||
|
format.html { render :edit, status: :unprocessable_entity }
|
||||||
|
format.json { render json: @expense.errors, status: :unprocessable_entity }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# DELETE /expenses/1 or /expenses/1.json
|
||||||
|
def destroy
|
||||||
|
@expense.destroy
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.html { redirect_to expenses_url, notice: "Expense was successfully destroyed." }
|
||||||
|
format.json { head :no_content }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
# Use callbacks to share common setup or constraints between actions.
|
||||||
|
def set_expense
|
||||||
|
@expense = Expense.find(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
# Only allow a list of trusted parameters through.
|
||||||
|
def expense_params
|
||||||
|
params.require(:expense).permit(:description, :payment, :period, :autopaid, :estimated)
|
||||||
|
end
|
||||||
|
end
|
7
app/helpers/expenses_helper.rb
Normal file
7
app/helpers/expenses_helper.rb
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
module ExpensesHelper
|
||||||
|
def expense_periods
|
||||||
|
Expense
|
||||||
|
.periods
|
||||||
|
.map { |key, value| [key.titleize, Expense.periods.key(value)] }
|
||||||
|
end
|
||||||
|
end
|
13
app/models/expense.rb
Normal file
13
app/models/expense.rb
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
class Expense < ApplicationRecord
|
||||||
|
PERIOD_OCCURENCES = {
|
||||||
|
monthly: 12,
|
||||||
|
annually: 1,
|
||||||
|
weekly: 52,
|
||||||
|
quarterly: 3,
|
||||||
|
}.freeze
|
||||||
|
enum :period, PERIOD_OCCURENCES.keys
|
||||||
|
|
||||||
|
def monthly
|
||||||
|
payment * PERIOD_OCCURENCES[period.to_sym] / 12
|
||||||
|
end
|
||||||
|
end
|
32
app/views/expenses/_expense.html.erb
Normal file
32
app/views/expenses/_expense.html.erb
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
<div id="<%= dom_id expense %>">
|
||||||
|
<p class="my-5">
|
||||||
|
<strong class="block font-medium mb-1">Description:</strong>
|
||||||
|
<%= expense.description %>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p class="my-5">
|
||||||
|
<strong class="block font-medium mb-1">Payment:</strong>
|
||||||
|
<%= expense.payment %>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p class="my-5">
|
||||||
|
<strong class="block font-medium mb-1">Period:</strong>
|
||||||
|
<%= expense.period %>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p class="my-5">
|
||||||
|
<strong class="block font-medium mb-1">Autopaid:</strong>
|
||||||
|
<%= expense.autopaid %>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p class="my-5">
|
||||||
|
<strong class="block font-medium mb-1">Estimated:</strong>
|
||||||
|
<%= expense.estimated %>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<% if action_name != "show" %>
|
||||||
|
<%= link_to "Show this expense", expense, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
|
||||||
|
<%= link_to 'Edit this expense', edit_expense_path(expense), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
|
||||||
|
<hr class="mt-6">
|
||||||
|
<% end %>
|
||||||
|
</div>
|
2
app/views/expenses/_expense.json.jbuilder
Normal file
2
app/views/expenses/_expense.json.jbuilder
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
json.extract! expense, :id, :description, :payment, :period, :autopaid, :estimated, :created_at, :updated_at
|
||||||
|
json.url expense_url(expense, format: :json)
|
47
app/views/expenses/_form.html.erb
Normal file
47
app/views/expenses/_form.html.erb
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
<%= form_with(model: expense, class: "contents") do |form| %>
|
||||||
|
<% if expense.errors.any? %>
|
||||||
|
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
|
||||||
|
<h2><%= pluralize(expense.errors.count, "error") %> prohibited this expense from being saved:</h2>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<% expense.errors.each do |error| %>
|
||||||
|
<li><%= error.full_message %></li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<div class="my-5">
|
||||||
|
<%= form.label :description %>
|
||||||
|
<%= form.text_field :description, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="my-5">
|
||||||
|
<%= form.label :payment %>
|
||||||
|
<%= form.text_field :payment, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="my-5">
|
||||||
|
<%= form.label :period %>
|
||||||
|
<%=
|
||||||
|
form.select :period,
|
||||||
|
options_for_select(expense_periods, expense.period),
|
||||||
|
{},
|
||||||
|
class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full"
|
||||||
|
%>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="my-5">
|
||||||
|
<%= form.label :autopaid %>
|
||||||
|
<%= form.check_box :autopaid, class: "block mt-2 h-5 w-5" %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="my-5">
|
||||||
|
<%= form.label :estimated %>
|
||||||
|
<%= form.check_box :estimated, class: "block mt-2 h-5 w-5" %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="inline">
|
||||||
|
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
8
app/views/expenses/edit.html.erb
Normal file
8
app/views/expenses/edit.html.erb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<div class="mx-auto md:w-2/3 w-full">
|
||||||
|
<h1 class="font-bold text-4xl">Editing expense</h1>
|
||||||
|
|
||||||
|
<%= render "form", expense: @expense %>
|
||||||
|
|
||||||
|
<%= link_to "Show this expense", @expense, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
|
||||||
|
<%= link_to "Back to expenses", expenses_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
|
||||||
|
</div>
|
14
app/views/expenses/index.html.erb
Normal file
14
app/views/expenses/index.html.erb
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<div class="w-full">
|
||||||
|
<% if notice.present? %>
|
||||||
|
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<div class="flex justify-between items-center">
|
||||||
|
<h1 class="font-bold text-4xl">Expenses</h1>
|
||||||
|
<%= link_to 'New expense', new_expense_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="expenses" class="min-w-full">
|
||||||
|
<%= render @expenses %>
|
||||||
|
</div>
|
||||||
|
</div>
|
1
app/views/expenses/index.json.jbuilder
Normal file
1
app/views/expenses/index.json.jbuilder
Normal file
|
@ -0,0 +1 @@
|
||||||
|
json.array! @expenses, partial: "expenses/expense", as: :expense
|
7
app/views/expenses/new.html.erb
Normal file
7
app/views/expenses/new.html.erb
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<div class="mx-auto md:w-2/3 w-full">
|
||||||
|
<h1 class="font-bold text-4xl">New expense</h1>
|
||||||
|
|
||||||
|
<%= render "form", expense: @expense %>
|
||||||
|
|
||||||
|
<%= link_to 'Back to expenses', expenses_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
|
||||||
|
</div>
|
15
app/views/expenses/show.html.erb
Normal file
15
app/views/expenses/show.html.erb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<div class="mx-auto md:w-2/3 w-full flex">
|
||||||
|
<div class="mx-auto">
|
||||||
|
<% if notice.present? %>
|
||||||
|
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<%= render @expense %>
|
||||||
|
|
||||||
|
<%= link_to 'Edit this expense', edit_expense_path(@expense), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
|
||||||
|
<div class="inline-block ml-2">
|
||||||
|
<%= button_to 'Destroy this expense', expense_path(@expense), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
|
||||||
|
</div>
|
||||||
|
<%= link_to 'Back to expenses', expenses_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
|
||||||
|
</div>
|
||||||
|
</div>
|
1
app/views/expenses/show.json.jbuilder
Normal file
1
app/views/expenses/show.json.jbuilder
Normal file
|
@ -0,0 +1 @@
|
||||||
|
json.partial! "expenses/expense", expense: @expense
|
|
@ -1,4 +1,5 @@
|
||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
|
resources :expenses
|
||||||
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
||||||
|
|
||||||
# Defines the root path route ("/")
|
# Defines the root path route ("/")
|
||||||
|
|
13
db/migrate/20221120201457_create_expenses.rb
Normal file
13
db/migrate/20221120201457_create_expenses.rb
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
class CreateExpenses < ActiveRecord::Migration[7.0]
|
||||||
|
def change
|
||||||
|
create_table :expenses do |t|
|
||||||
|
t.string :description, default: "", null: false
|
||||||
|
t.decimal :payment, default: 0.00, null: false, precision: 8, scale: 2
|
||||||
|
t.integer :period, default: 0, null: false
|
||||||
|
t.boolean :autopaid, default: false, null: false
|
||||||
|
t.boolean :estimated, default: false, null: false
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
24
db/schema.rb
generated
Normal file
24
db/schema.rb
generated
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# This file is auto-generated from the current state of the database. Instead
|
||||||
|
# of editing this file, please use the migrations feature of Active Record to
|
||||||
|
# incrementally modify your database, and then regenerate this schema definition.
|
||||||
|
#
|
||||||
|
# This file is the source Rails uses to define your schema when running `bin/rails
|
||||||
|
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
|
||||||
|
# be faster and is potentially less error prone than running all of your
|
||||||
|
# migrations from scratch. Old migrations may fail to apply correctly if those
|
||||||
|
# migrations use external dependencies or application code.
|
||||||
|
#
|
||||||
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
|
ActiveRecord::Schema[7.0].define(version: 2022_11_20_201457) do
|
||||||
|
create_table "expenses", force: :cascade do |t|
|
||||||
|
t.string "description", default: "", null: false
|
||||||
|
t.decimal "payment", precision: 8, scale: 2, default: "0.0", null: false
|
||||||
|
t.integer "period", default: 0, null: false
|
||||||
|
t.boolean "autopaid", default: false, null: false
|
||||||
|
t.boolean "estimated", default: false, null: false
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
48
test/controllers/expenses_controller_test.rb
Normal file
48
test/controllers/expenses_controller_test.rb
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class ExpensesControllerTest < ActionDispatch::IntegrationTest
|
||||||
|
setup do
|
||||||
|
@expense = expenses(:one)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should get index" do
|
||||||
|
get expenses_url
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should get new" do
|
||||||
|
get new_expense_url
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should create expense" do
|
||||||
|
assert_difference("Expense.count") do
|
||||||
|
post expenses_url, params: { expense: { autopaid: @expense.autopaid, description: @expense.description, estimated: @expense.estimated, payment: @expense.payment, period: @expense.period } }
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_redirected_to expense_url(Expense.last)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should show expense" do
|
||||||
|
get expense_url(@expense)
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should get edit" do
|
||||||
|
get edit_expense_url(@expense)
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should update expense" do
|
||||||
|
patch expense_url(@expense), params: { expense: { autopaid: @expense.autopaid, description: @expense.description, estimated: @expense.estimated, payment: @expense.payment, period: @expense.period } }
|
||||||
|
assert_redirected_to expense_url(@expense)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should destroy expense" do
|
||||||
|
assert_difference("Expense.count", -1) do
|
||||||
|
delete expense_url(@expense)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_redirected_to expenses_url
|
||||||
|
end
|
||||||
|
end
|
15
test/fixtures/expenses.yml
vendored
Normal file
15
test/fixtures/expenses.yml
vendored
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||||
|
|
||||||
|
one:
|
||||||
|
description: MyString
|
||||||
|
payment: 9.99
|
||||||
|
period: 1
|
||||||
|
autopaid: false
|
||||||
|
estimated: false
|
||||||
|
|
||||||
|
two:
|
||||||
|
description: MyString
|
||||||
|
payment: 9.99
|
||||||
|
period: 1
|
||||||
|
autopaid: false
|
||||||
|
estimated: false
|
7
test/models/expense_test.rb
Normal file
7
test/models/expense_test.rb
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class ExpenseTest < ActiveSupport::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
49
test/system/expenses_test.rb
Normal file
49
test/system/expenses_test.rb
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
require "application_system_test_case"
|
||||||
|
|
||||||
|
class ExpensesTest < ApplicationSystemTestCase
|
||||||
|
setup do
|
||||||
|
@expense = expenses(:one)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "visiting the index" do
|
||||||
|
visit expenses_url
|
||||||
|
assert_selector "h1", text: "Expenses"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should create expense" do
|
||||||
|
visit expenses_url
|
||||||
|
click_on "New expense"
|
||||||
|
|
||||||
|
check "Autopaid" if @expense.autopaid
|
||||||
|
fill_in "Description", with: @expense.description
|
||||||
|
check "Estimated" if @expense.estimated
|
||||||
|
fill_in "Payment", with: @expense.payment
|
||||||
|
fill_in "Period", with: @expense.period
|
||||||
|
click_on "Create Expense"
|
||||||
|
|
||||||
|
assert_text "Expense was successfully created"
|
||||||
|
click_on "Back"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should update Expense" do
|
||||||
|
visit expense_url(@expense)
|
||||||
|
click_on "Edit this expense", match: :first
|
||||||
|
|
||||||
|
check "Autopaid" if @expense.autopaid
|
||||||
|
fill_in "Description", with: @expense.description
|
||||||
|
check "Estimated" if @expense.estimated
|
||||||
|
fill_in "Payment", with: @expense.payment
|
||||||
|
fill_in "Period", with: @expense.period
|
||||||
|
click_on "Update Expense"
|
||||||
|
|
||||||
|
assert_text "Expense was successfully updated"
|
||||||
|
click_on "Back"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should destroy Expense" do
|
||||||
|
visit expense_url(@expense)
|
||||||
|
click_on "Destroy this expense", match: :first
|
||||||
|
|
||||||
|
assert_text "Expense was successfully destroyed"
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue