Track incomes

This commit is contained in:
Andrew Tomaka 2022-11-20 16:02:41 -05:00
parent ab00344de0
commit e098ab48ca
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
19 changed files with 337 additions and 1 deletions

View file

@ -0,0 +1,70 @@
class IncomesController < ApplicationController
before_action :set_income, only: %i[ show edit update destroy ]
# GET /incomes or /incomes.json
def index
@incomes = Income.all
end
# GET /incomes/1 or /incomes/1.json
def show
end
# GET /incomes/new
def new
@income = Income.new
end
# GET /incomes/1/edit
def edit
end
# POST /incomes or /incomes.json
def create
@income = Income.new(income_params)
respond_to do |format|
if @income.save
format.html { redirect_to income_url(@income), notice: "Income was successfully created." }
format.json { render :show, status: :created, location: @income }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @income.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /incomes/1 or /incomes/1.json
def update
respond_to do |format|
if @income.update(income_params)
format.html { redirect_to income_url(@income), notice: "Income was successfully updated." }
format.json { render :show, status: :ok, location: @income }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @income.errors, status: :unprocessable_entity }
end
end
end
# DELETE /incomes/1 or /incomes/1.json
def destroy
@income.destroy
respond_to do |format|
format.html { redirect_to incomes_url, notice: "Income was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_income
@income = Income.find(params[:id])
end
# Only allow a list of trusted parameters through.
def income_params
params.require(:income).permit(:description, :included, :amount, :member_id)
end
end

View file

@ -0,0 +1,7 @@
module IncomesHelper
def members
Member
.all
.map { |member, value| [member.name, member.id] }
end
end

3
app/models/income.rb Normal file
View file

@ -0,0 +1,3 @@
class Income < ApplicationRecord
belongs_to :member
end

View file

@ -0,0 +1,42 @@
<%= form_with(model: income, class: "contents") do |form| %>
<% if income.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(income.errors.count, "error") %> prohibited this income from being saved:</h2>
<ul>
<% income.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 :included %>
<%= form.check_box :included, class: "block mt-2 h-5 w-5" %>
</div>
<div class="my-5">
<%= form.label :amount %>
<%= form.text_field :amount, 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 :member_id %>
<%=
form.select :member_id,
options_for_select(members, income.member_id),
{},
class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full"
%>
</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 %>

View file

@ -0,0 +1,27 @@
<div id="<%= dom_id income %>">
<p class="my-5">
<strong class="block font-medium mb-1">Description:</strong>
<%= income.description %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Included:</strong>
<%= income.included %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Amount:</strong>
<%= income.amount %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Member:</strong>
<%= income.member_id %>
</p>
<% if action_name != "show" %>
<%= link_to "Show this income", income, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to 'Edit this income', edit_income_path(income), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
<hr class="mt-6">
<% end %>
</div>

View file

@ -0,0 +1,2 @@
json.extract! income, :id, :description, :included, :amount, :member_id, :created_at, :updated_at
json.url income_url(income, format: :json)

View file

@ -0,0 +1,8 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing income</h1>
<%= render "form", income: @income %>
<%= link_to "Show this income", @income, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to incomes", incomes_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View 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">Incomes</h1>
<%= link_to 'New income', new_income_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="incomes" class="min-w-full">
<%= render @incomes %>
</div>
</div>

View file

@ -0,0 +1 @@
json.array! @incomes, partial: "incomes/income", as: :income

View file

@ -0,0 +1,7 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New income</h1>
<%= render "form", income: @income %>
<%= link_to 'Back to incomes', incomes_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View 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 @income %>
<%= link_to 'Edit this income', edit_income_path(@income), 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 income', income_path(@income), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
<%= link_to 'Back to incomes', incomes_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
</div>

View file

@ -0,0 +1 @@
json.partial! "incomes/income", income: @income

View file

@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :incomes
resources :members
resources :expenses
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

View file

@ -0,0 +1,12 @@
class CreateIncomes < ActiveRecord::Migration[7.0]
def change
create_table :incomes do |t|
t.string :description, default: "", null: false
t.boolean :included, default: true, null: false
t.decimal :amount, default: 0.00, null: false, precision: 9, scale: 2
t.references :member, null: false, foreign_key: true
t.timestamps
end
end
end

13
db/schema.rb generated
View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_11_20_204751) do
ActiveRecord::Schema[7.0].define(version: 2022_11_20_205511) 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
@ -21,6 +21,16 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_20_204751) do
t.datetime "updated_at", null: false
end
create_table "incomes", force: :cascade do |t|
t.string "description", default: "", null: false
t.boolean "included", default: true, null: false
t.decimal "amount", precision: 9, scale: 2, default: "0.0", null: false
t.integer "member_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["member_id"], name: "index_incomes_on_member_id"
end
create_table "members", force: :cascade do |t|
t.string "name", default: "", null: false
t.boolean "pays", default: true, null: false
@ -28,4 +38,5 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_20_204751) do
t.datetime "updated_at", null: false
end
add_foreign_key "incomes", "members"
end

View file

@ -0,0 +1,48 @@
require "test_helper"
class IncomesControllerTest < ActionDispatch::IntegrationTest
setup do
@income = incomes(:one)
end
test "should get index" do
get incomes_url
assert_response :success
end
test "should get new" do
get new_income_url
assert_response :success
end
test "should create income" do
assert_difference("Income.count") do
post incomes_url, params: { income: { amount: @income.amount, description: @income.description, included: @income.included, member_id: @income.member_id } }
end
assert_redirected_to income_url(Income.last)
end
test "should show income" do
get income_url(@income)
assert_response :success
end
test "should get edit" do
get edit_income_url(@income)
assert_response :success
end
test "should update income" do
patch income_url(@income), params: { income: { amount: @income.amount, description: @income.description, included: @income.included, member_id: @income.member_id } }
assert_redirected_to income_url(@income)
end
test "should destroy income" do
assert_difference("Income.count", -1) do
delete income_url(@income)
end
assert_redirected_to incomes_url
end
end

13
test/fixtures/incomes.yml vendored Normal file
View file

@ -0,0 +1,13 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
description: MyString
included: false
amount: 9.99
member: one
two:
description: MyString
included: false
amount: 9.99
member: two

View file

@ -0,0 +1,7 @@
require "test_helper"
class IncomeTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,47 @@
require "application_system_test_case"
class IncomesTest < ApplicationSystemTestCase
setup do
@income = incomes(:one)
end
test "visiting the index" do
visit incomes_url
assert_selector "h1", text: "Incomes"
end
test "should create income" do
visit incomes_url
click_on "New income"
fill_in "Amount", with: @income.amount
fill_in "Description", with: @income.description
check "Included" if @income.included
fill_in "Member", with: @income.member_id
click_on "Create Income"
assert_text "Income was successfully created"
click_on "Back"
end
test "should update Income" do
visit income_url(@income)
click_on "Edit this income", match: :first
fill_in "Amount", with: @income.amount
fill_in "Description", with: @income.description
check "Included" if @income.included
fill_in "Member", with: @income.member_id
click_on "Update Income"
assert_text "Income was successfully updated"
click_on "Back"
end
test "should destroy Income" do
visit income_url(@income)
click_on "Destroy this income", match: :first
assert_text "Income was successfully destroyed"
end
end