Expenses scaffold

This commit is contained in:
Manuel Bustillo 2024-07-11 19:55:48 +02:00
parent 47ac0796b9
commit 98eb2e0473
13 changed files with 197 additions and 1 deletions

View 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(:name, :amount, :pricing_type)
end
end

View File

@ -0,0 +1,2 @@
module ExpensesHelper
end

2
app/models/expense.rb Normal file
View File

@ -0,0 +1,2 @@
class Expense < ApplicationRecord
end

View File

@ -0,0 +1,17 @@
<div id="<%= dom_id expense %>">
<p>
<strong>Name:</strong>
<%= expense.name %>
</p>
<p>
<strong>Amount:</strong>
<%= expense.amount %>
</p>
<p>
<strong>Pricing type:</strong>
<%= expense.pricing_type %>
</p>
</div>

View File

@ -0,0 +1,32 @@
<%= form_with(model: expense) do |form| %>
<% if expense.errors.any? %>
<div style="color: red">
<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>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
</div>
<div>
<%= form.label :amount, style: "display: block" %>
<%= form.text_field :amount %>
</div>
<div>
<%= form.label :pricing_type, style: "display: block" %>
<%= form.text_field :pricing_type %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>

View File

@ -0,0 +1,10 @@
<h1>Editing expense</h1>
<%= render "form", expense: @expense %>
<br>
<div>
<%= link_to "Show this expense", @expense %> |
<%= link_to "Back to expenses", expenses_path %>
</div>

View File

@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>
<h1>Expenses</h1>
<div id="expenses">
<% @expenses.each do |expense| %>
<%= render expense %>
<p>
<%= link_to "Show this expense", expense %>
</p>
<% end %>
</div>
<%= link_to "New expense", new_expense_path %>

View File

@ -0,0 +1,9 @@
<h1>New expense</h1>
<%= render "form", expense: @expense %>
<br>
<div>
<%= link_to "Back to expenses", expenses_path %>
</div>

View File

@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>
<%= render @expense %>
<div>
<%= link_to "Edit this expense", edit_expense_path(@expense) %> |
<%= link_to "Back to expenses", expenses_path %>
<%= button_to "Destroy this expense", @expense, method: :delete %>
</div>

View File

@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :expenses
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.

View File

@ -0,0 +1,12 @@
class CreateExpenses < ActiveRecord::Migration[7.1]
def change
create_enum :pricing_types, ["fixed", "per_person"]
create_table :expenses, id: :uuid do |t|
t.string :name
t.decimal :amount
t.enum :pricing_type, enum_type: :pricing_types, default: "fixed", null: false
t.timestamps
end
end
end

14
db/schema.rb generated
View File

@ -10,8 +10,20 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 0) do
ActiveRecord::Schema[7.1].define(version: 2024_07_11_175425) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
# Custom types defined in this database.
# Note that some types may not work with other database engines. Be careful if changing database.
create_enum "pricing_types", ["fixed", "per_person"]
create_table "expenses", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name"
t.decimal "amount"
t.enum "pricing_type", default: "fixed", null: false, enum_type: "pricing_types"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Expense, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end