Add family members

This commit is contained in:
Andrew Tomaka 2022-11-20 15:52:18 -05:00
parent e588ab3db0
commit ab00344de0
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
19 changed files with 292 additions and 1 deletions

View file

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

View file

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

2
app/models/member.rb Normal file
View file

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

View file

@ -0,0 +1,27 @@
<%= form_with(model: member, class: "contents") do |form| %>
<% if member.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(member.errors.count, "error") %> prohibited this member from being saved:</h2>
<ul>
<% member.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<%= form.label :name %>
<%= form.text_field :name, 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 :pays %>
<%= form.check_box :pays, 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 %>

View file

@ -0,0 +1,17 @@
<div id="<%= dom_id member %>">
<p class="my-5">
<strong class="block font-medium mb-1">Name:</strong>
<%= member.name %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Pays:</strong>
<%= member.pays %>
</p>
<% if action_name != "show" %>
<%= link_to "Show this member", member, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to 'Edit this member', edit_member_path(member), 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! member, :id, :name, :pays, :created_at, :updated_at
json.url member_url(member, 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 member</h1>
<%= render "form", member: @member %>
<%= link_to "Show this member", @member, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to members", members_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">Members</h1>
<%= link_to 'New member', new_member_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="members" class="min-w-full">
<%= render @members %>
</div>
</div>

View file

@ -0,0 +1 @@
json.array! @members, partial: "members/member", as: :member

View file

@ -0,0 +1,7 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New member</h1>
<%= render "form", member: @member %>
<%= link_to 'Back to members', members_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 @member %>
<%= link_to 'Edit this member', edit_member_path(@member), 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 member', member_path(@member), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
<%= link_to 'Back to members', members_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! "members/member", member: @member

View file

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

View file

@ -0,0 +1,10 @@
class CreateMembers < ActiveRecord::Migration[7.0]
def change
create_table :members do |t|
t.string :name, default: "", null: false
t.boolean :pays, default: true, null: false
t.timestamps
end
end
end

9
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_201457) do
ActiveRecord::Schema[7.0].define(version: 2022_11_20_204751) 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,4 +21,11 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_20_201457) do
t.datetime "updated_at", null: false
end
create_table "members", force: :cascade do |t|
t.string "name", default: "", null: false
t.boolean "pays", default: true, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end

View file

@ -0,0 +1,48 @@
require "test_helper"
class MembersControllerTest < ActionDispatch::IntegrationTest
setup do
@member = members(:one)
end
test "should get index" do
get members_url
assert_response :success
end
test "should get new" do
get new_member_url
assert_response :success
end
test "should create member" do
assert_difference("Member.count") do
post members_url, params: { member: { name: @member.name, pays: @member.pays } }
end
assert_redirected_to member_url(Member.last)
end
test "should show member" do
get member_url(@member)
assert_response :success
end
test "should get edit" do
get edit_member_url(@member)
assert_response :success
end
test "should update member" do
patch member_url(@member), params: { member: { name: @member.name, pays: @member.pays } }
assert_redirected_to member_url(@member)
end
test "should destroy member" do
assert_difference("Member.count", -1) do
delete member_url(@member)
end
assert_redirected_to members_url
end
end

9
test/fixtures/members.yml vendored Normal file
View file

@ -0,0 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
pays: false
two:
name: MyString
pays: false

View file

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

View file

@ -0,0 +1,43 @@
require "application_system_test_case"
class MembersTest < ApplicationSystemTestCase
setup do
@member = members(:one)
end
test "visiting the index" do
visit members_url
assert_selector "h1", text: "Members"
end
test "should create member" do
visit members_url
click_on "New member"
fill_in "Name", with: @member.name
check "Pays" if @member.pays
click_on "Create Member"
assert_text "Member was successfully created"
click_on "Back"
end
test "should update Member" do
visit member_url(@member)
click_on "Edit this member", match: :first
fill_in "Name", with: @member.name
check "Pays" if @member.pays
click_on "Update Member"
assert_text "Member was successfully updated"
click_on "Back"
end
test "should destroy Member" do
visit member_url(@member)
click_on "Destroy this member", match: :first
assert_text "Member was successfully destroyed"
end
end