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