From ab00344de0b9f44c12f58be41ddf68f3a5536c79 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sun, 20 Nov 2022 15:52:18 -0500 Subject: [PATCH] Add family members --- app/controllers/members_controller.rb | 70 +++++++++++++++++++++ app/helpers/members_helper.rb | 2 + app/models/member.rb | 2 + app/views/members/_form.html.erb | 27 ++++++++ app/views/members/_member.html.erb | 17 +++++ app/views/members/_member.json.jbuilder | 2 + app/views/members/edit.html.erb | 8 +++ app/views/members/index.html.erb | 14 +++++ app/views/members/index.json.jbuilder | 1 + app/views/members/new.html.erb | 7 +++ app/views/members/show.html.erb | 15 +++++ app/views/members/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20221120204751_create_members.rb | 10 +++ db/schema.rb | 9 ++- test/controllers/members_controller_test.rb | 48 ++++++++++++++ test/fixtures/members.yml | 9 +++ test/models/member_test.rb | 7 +++ test/system/members_test.rb | 43 +++++++++++++ 19 files changed, 292 insertions(+), 1 deletion(-) create mode 100644 app/controllers/members_controller.rb create mode 100644 app/helpers/members_helper.rb create mode 100644 app/models/member.rb create mode 100644 app/views/members/_form.html.erb create mode 100644 app/views/members/_member.html.erb create mode 100644 app/views/members/_member.json.jbuilder create mode 100644 app/views/members/edit.html.erb create mode 100644 app/views/members/index.html.erb create mode 100644 app/views/members/index.json.jbuilder create mode 100644 app/views/members/new.html.erb create mode 100644 app/views/members/show.html.erb create mode 100644 app/views/members/show.json.jbuilder create mode 100644 db/migrate/20221120204751_create_members.rb create mode 100644 test/controllers/members_controller_test.rb create mode 100644 test/fixtures/members.yml create mode 100644 test/models/member_test.rb create mode 100644 test/system/members_test.rb diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb new file mode 100644 index 0000000..6052ca8 --- /dev/null +++ b/app/controllers/members_controller.rb @@ -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 diff --git a/app/helpers/members_helper.rb b/app/helpers/members_helper.rb new file mode 100644 index 0000000..e3fa623 --- /dev/null +++ b/app/helpers/members_helper.rb @@ -0,0 +1,2 @@ +module MembersHelper +end diff --git a/app/models/member.rb b/app/models/member.rb new file mode 100644 index 0000000..8b44e8f --- /dev/null +++ b/app/models/member.rb @@ -0,0 +1,2 @@ +class Member < ApplicationRecord +end diff --git a/app/views/members/_form.html.erb b/app/views/members/_form.html.erb new file mode 100644 index 0000000..80b6a55 --- /dev/null +++ b/app/views/members/_form.html.erb @@ -0,0 +1,27 @@ +<%= form_with(model: member, class: "contents") do |form| %> + <% if member.errors.any? %> +
+

<%= pluralize(member.errors.count, "error") %> prohibited this member from being saved:

+ + +
+ <% end %> + +
+ <%= 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" %> +
+ +
+ <%= form.label :pays %> + <%= form.check_box :pays, class: "block mt-2 h-5 w-5" %> +
+ +
+ <%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %> +
+<% end %> diff --git a/app/views/members/_member.html.erb b/app/views/members/_member.html.erb new file mode 100644 index 0000000..080783d --- /dev/null +++ b/app/views/members/_member.html.erb @@ -0,0 +1,17 @@ +
+

+ Name: + <%= member.name %> +

+ +

+ Pays: + <%= member.pays %> +

+ + <% 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" %> +
+ <% end %> +
diff --git a/app/views/members/_member.json.jbuilder b/app/views/members/_member.json.jbuilder new file mode 100644 index 0000000..2250a52 --- /dev/null +++ b/app/views/members/_member.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! member, :id, :name, :pays, :created_at, :updated_at +json.url member_url(member, format: :json) diff --git a/app/views/members/edit.html.erb b/app/views/members/edit.html.erb new file mode 100644 index 0000000..27ef21d --- /dev/null +++ b/app/views/members/edit.html.erb @@ -0,0 +1,8 @@ +
+

Editing member

+ + <%= 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" %> +
diff --git a/app/views/members/index.html.erb b/app/views/members/index.html.erb new file mode 100644 index 0000000..b66a805 --- /dev/null +++ b/app/views/members/index.html.erb @@ -0,0 +1,14 @@ +
+ <% if notice.present? %> +

<%= notice %>

+ <% end %> + +
+

Members

+ <%= link_to 'New member', new_member_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %> +
+ +
+ <%= render @members %> +
+
diff --git a/app/views/members/index.json.jbuilder b/app/views/members/index.json.jbuilder new file mode 100644 index 0000000..1620f70 --- /dev/null +++ b/app/views/members/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @members, partial: "members/member", as: :member diff --git a/app/views/members/new.html.erb b/app/views/members/new.html.erb new file mode 100644 index 0000000..2ab9003 --- /dev/null +++ b/app/views/members/new.html.erb @@ -0,0 +1,7 @@ +
+

New member

+ + <%= 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" %> +
diff --git a/app/views/members/show.html.erb b/app/views/members/show.html.erb new file mode 100644 index 0000000..f925386 --- /dev/null +++ b/app/views/members/show.html.erb @@ -0,0 +1,15 @@ +
+
+ <% if notice.present? %> +

<%= notice %>

+ <% 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" %> +
+ <%= button_to 'Destroy this member', member_path(@member), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 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" %> +
+
diff --git a/app/views/members/show.json.jbuilder b/app/views/members/show.json.jbuilder new file mode 100644 index 0000000..8de70d4 --- /dev/null +++ b/app/views/members/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "members/member", member: @member diff --git a/config/routes.rb b/config/routes.rb index 39f07ef..bc94092 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20221120204751_create_members.rb b/db/migrate/20221120204751_create_members.rb new file mode 100644 index 0000000..a3dc795 --- /dev/null +++ b/db/migrate/20221120204751_create_members.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 2199dbb..854d81f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/test/controllers/members_controller_test.rb b/test/controllers/members_controller_test.rb new file mode 100644 index 0000000..ac2b771 --- /dev/null +++ b/test/controllers/members_controller_test.rb @@ -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 diff --git a/test/fixtures/members.yml b/test/fixtures/members.yml new file mode 100644 index 0000000..956805a --- /dev/null +++ b/test/fixtures/members.yml @@ -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 diff --git a/test/models/member_test.rb b/test/models/member_test.rb new file mode 100644 index 0000000..4fec230 --- /dev/null +++ b/test/models/member_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class MemberTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/members_test.rb b/test/system/members_test.rb new file mode 100644 index 0000000..2b491ad --- /dev/null +++ b/test/system/members_test.rb @@ -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