diff --git a/app/controllers/incomes_controller.rb b/app/controllers/incomes_controller.rb new file mode 100644 index 0000000..2623267 --- /dev/null +++ b/app/controllers/incomes_controller.rb @@ -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 diff --git a/app/helpers/incomes_helper.rb b/app/helpers/incomes_helper.rb new file mode 100644 index 0000000..9d6b84f --- /dev/null +++ b/app/helpers/incomes_helper.rb @@ -0,0 +1,7 @@ +module IncomesHelper + def members + Member + .all + .map { |member, value| [member.name, member.id] } + end +end diff --git a/app/models/income.rb b/app/models/income.rb new file mode 100644 index 0000000..2053c6c --- /dev/null +++ b/app/models/income.rb @@ -0,0 +1,3 @@ +class Income < ApplicationRecord + belongs_to :member +end diff --git a/app/views/incomes/_form.html.erb b/app/views/incomes/_form.html.erb new file mode 100644 index 0000000..ae19b6d --- /dev/null +++ b/app/views/incomes/_form.html.erb @@ -0,0 +1,42 @@ +<%= form_with(model: income, class: "contents") do |form| %> + <% if income.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= 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" %> +
+ +
+ <%= form.label :included %> + <%= form.check_box :included, class: "block mt-2 h-5 w-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" %> +
+ +
+ <%= 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" + %> +
+ +
+ <%= 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/incomes/_income.html.erb b/app/views/incomes/_income.html.erb new file mode 100644 index 0000000..e283a1f --- /dev/null +++ b/app/views/incomes/_income.html.erb @@ -0,0 +1,27 @@ +
+

+ Description: + <%= income.description %> +

+ +

+ Included: + <%= income.included %> +

+ +

+ Amount: + <%= income.amount %> +

+ +

+ Member: + <%= income.member_id %> +

+ + <% 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" %> +
+ <% end %> +
diff --git a/app/views/incomes/_income.json.jbuilder b/app/views/incomes/_income.json.jbuilder new file mode 100644 index 0000000..b191e62 --- /dev/null +++ b/app/views/incomes/_income.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! income, :id, :description, :included, :amount, :member_id, :created_at, :updated_at +json.url income_url(income, format: :json) diff --git a/app/views/incomes/edit.html.erb b/app/views/incomes/edit.html.erb new file mode 100644 index 0000000..237e85f --- /dev/null +++ b/app/views/incomes/edit.html.erb @@ -0,0 +1,8 @@ +
+

Editing income

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

<%= notice %>

+ <% end %> + +
+

Incomes

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

New income

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

<%= notice %>

+ <% 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" %> +
+ <%= button_to 'Destroy this income', income_path(@income), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 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" %> +
+
diff --git a/app/views/incomes/show.json.jbuilder b/app/views/incomes/show.json.jbuilder new file mode 100644 index 0000000..f2b0001 --- /dev/null +++ b/app/views/incomes/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "incomes/income", income: @income diff --git a/config/routes.rb b/config/routes.rb index bc94092..1b25320 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20221120205511_create_incomes.rb b/db/migrate/20221120205511_create_incomes.rb new file mode 100644 index 0000000..9e10552 --- /dev/null +++ b/db/migrate/20221120205511_create_incomes.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 854d81f..11a339c 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_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 diff --git a/test/controllers/incomes_controller_test.rb b/test/controllers/incomes_controller_test.rb new file mode 100644 index 0000000..cb07fbd --- /dev/null +++ b/test/controllers/incomes_controller_test.rb @@ -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 diff --git a/test/fixtures/incomes.yml b/test/fixtures/incomes.yml new file mode 100644 index 0000000..7bf8b25 --- /dev/null +++ b/test/fixtures/incomes.yml @@ -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 diff --git a/test/models/income_test.rb b/test/models/income_test.rb new file mode 100644 index 0000000..0b7dc86 --- /dev/null +++ b/test/models/income_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class IncomeTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/incomes_test.rb b/test/system/incomes_test.rb new file mode 100644 index 0000000..bcce38d --- /dev/null +++ b/test/system/incomes_test.rb @@ -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