diff --git a/app/controllers/expenses_controller.rb b/app/controllers/expenses_controller.rb new file mode 100644 index 0000000..c88513d --- /dev/null +++ b/app/controllers/expenses_controller.rb @@ -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(:description, :payment, :period, :autopaid, :estimated) + end +end diff --git a/app/helpers/expenses_helper.rb b/app/helpers/expenses_helper.rb new file mode 100644 index 0000000..739166e --- /dev/null +++ b/app/helpers/expenses_helper.rb @@ -0,0 +1,7 @@ +module ExpensesHelper + def expense_periods + Expense + .periods + .map { |key, value| [key.titleize, Expense.periods.key(value)] } + end +end diff --git a/app/models/expense.rb b/app/models/expense.rb new file mode 100644 index 0000000..1925fe6 --- /dev/null +++ b/app/models/expense.rb @@ -0,0 +1,13 @@ +class Expense < ApplicationRecord + PERIOD_OCCURENCES = { + monthly: 12, + annually: 1, + weekly: 52, + quarterly: 3, + }.freeze + enum :period, PERIOD_OCCURENCES.keys + + def monthly + payment * PERIOD_OCCURENCES[period.to_sym] / 12 + end +end diff --git a/app/views/expenses/_expense.html.erb b/app/views/expenses/_expense.html.erb new file mode 100644 index 0000000..cb922b0 --- /dev/null +++ b/app/views/expenses/_expense.html.erb @@ -0,0 +1,32 @@ +
+

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

+ +

+ Payment: + <%= expense.payment %> +

+ +

+ Period: + <%= expense.period %> +

+ +

+ Autopaid: + <%= expense.autopaid %> +

+ +

+ Estimated: + <%= expense.estimated %> +

+ + <% if action_name != "show" %> + <%= link_to "Show this expense", expense, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> + <%= link_to 'Edit this expense', edit_expense_path(expense), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %> +
+ <% end %> +
diff --git a/app/views/expenses/_expense.json.jbuilder b/app/views/expenses/_expense.json.jbuilder new file mode 100644 index 0000000..216ad4e --- /dev/null +++ b/app/views/expenses/_expense.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! expense, :id, :description, :payment, :period, :autopaid, :estimated, :created_at, :updated_at +json.url expense_url(expense, format: :json) diff --git a/app/views/expenses/_form.html.erb b/app/views/expenses/_form.html.erb new file mode 100644 index 0000000..0630871 --- /dev/null +++ b/app/views/expenses/_form.html.erb @@ -0,0 +1,47 @@ +<%= form_with(model: expense, class: "contents") do |form| %> + <% if expense.errors.any? %> +
+

<%= pluralize(expense.errors.count, "error") %> prohibited this expense 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 :payment %> + <%= form.text_field :payment, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %> +
+ +
+ <%= form.label :period %> + <%= + form.select :period, + options_for_select(expense_periods, expense.period), + {}, + class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" + %> +
+ +
+ <%= form.label :autopaid %> + <%= form.check_box :autopaid, class: "block mt-2 h-5 w-5" %> +
+ +
+ <%= form.label :estimated %> + <%= form.check_box :estimated, 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/expenses/edit.html.erb b/app/views/expenses/edit.html.erb new file mode 100644 index 0000000..f5db8f0 --- /dev/null +++ b/app/views/expenses/edit.html.erb @@ -0,0 +1,8 @@ +
+

Editing expense

+ + <%= render "form", expense: @expense %> + + <%= link_to "Show this expense", @expense, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> + <%= link_to "Back to expenses", expenses_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +
diff --git a/app/views/expenses/index.html.erb b/app/views/expenses/index.html.erb new file mode 100644 index 0000000..3008610 --- /dev/null +++ b/app/views/expenses/index.html.erb @@ -0,0 +1,14 @@ +
+ <% if notice.present? %> +

<%= notice %>

+ <% end %> + +
+

Expenses

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

New expense

+ + <%= render "form", expense: @expense %> + + <%= link_to 'Back to expenses', expenses_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +
diff --git a/app/views/expenses/show.html.erb b/app/views/expenses/show.html.erb new file mode 100644 index 0000000..e4a6db3 --- /dev/null +++ b/app/views/expenses/show.html.erb @@ -0,0 +1,15 @@ +
+
+ <% if notice.present? %> +

<%= notice %>

+ <% end %> + + <%= render @expense %> + + <%= link_to 'Edit this expense', edit_expense_path(@expense), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +
+ <%= button_to 'Destroy this expense', expense_path(@expense), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %> +
+ <%= link_to 'Back to expenses', expenses_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +
+
diff --git a/app/views/expenses/show.json.jbuilder b/app/views/expenses/show.json.jbuilder new file mode 100644 index 0000000..3bb3176 --- /dev/null +++ b/app/views/expenses/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "expenses/expense", expense: @expense diff --git a/config/routes.rb b/config/routes.rb index 262ffd5..39f07ef 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 # Defines the root path route ("/") diff --git a/db/migrate/20221120201457_create_expenses.rb b/db/migrate/20221120201457_create_expenses.rb new file mode 100644 index 0000000..6deaf59 --- /dev/null +++ b/db/migrate/20221120201457_create_expenses.rb @@ -0,0 +1,13 @@ +class CreateExpenses < ActiveRecord::Migration[7.0] + def change + create_table :expenses do |t| + t.string :description, default: "", null: false + t.decimal :payment, default: 0.00, null: false, precision: 8, scale: 2 + t.integer :period, default: 0, null: false + t.boolean :autopaid, default: false, null: false + t.boolean :estimated, default: false, null: false + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..2199dbb --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,24 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# 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 + 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 + t.integer "period", default: 0, null: false + t.boolean "autopaid", default: false, null: false + t.boolean "estimated", default: false, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/controllers/expenses_controller_test.rb b/test/controllers/expenses_controller_test.rb new file mode 100644 index 0000000..1e5b44b --- /dev/null +++ b/test/controllers/expenses_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class ExpensesControllerTest < ActionDispatch::IntegrationTest + setup do + @expense = expenses(:one) + end + + test "should get index" do + get expenses_url + assert_response :success + end + + test "should get new" do + get new_expense_url + assert_response :success + end + + test "should create expense" do + assert_difference("Expense.count") do + post expenses_url, params: { expense: { autopaid: @expense.autopaid, description: @expense.description, estimated: @expense.estimated, payment: @expense.payment, period: @expense.period } } + end + + assert_redirected_to expense_url(Expense.last) + end + + test "should show expense" do + get expense_url(@expense) + assert_response :success + end + + test "should get edit" do + get edit_expense_url(@expense) + assert_response :success + end + + test "should update expense" do + patch expense_url(@expense), params: { expense: { autopaid: @expense.autopaid, description: @expense.description, estimated: @expense.estimated, payment: @expense.payment, period: @expense.period } } + assert_redirected_to expense_url(@expense) + end + + test "should destroy expense" do + assert_difference("Expense.count", -1) do + delete expense_url(@expense) + end + + assert_redirected_to expenses_url + end +end diff --git a/test/fixtures/expenses.yml b/test/fixtures/expenses.yml new file mode 100644 index 0000000..3eb6185 --- /dev/null +++ b/test/fixtures/expenses.yml @@ -0,0 +1,15 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + description: MyString + payment: 9.99 + period: 1 + autopaid: false + estimated: false + +two: + description: MyString + payment: 9.99 + period: 1 + autopaid: false + estimated: false diff --git a/test/models/expense_test.rb b/test/models/expense_test.rb new file mode 100644 index 0000000..67ac22a --- /dev/null +++ b/test/models/expense_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ExpenseTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/expenses_test.rb b/test/system/expenses_test.rb new file mode 100644 index 0000000..5778073 --- /dev/null +++ b/test/system/expenses_test.rb @@ -0,0 +1,49 @@ +require "application_system_test_case" + +class ExpensesTest < ApplicationSystemTestCase + setup do + @expense = expenses(:one) + end + + test "visiting the index" do + visit expenses_url + assert_selector "h1", text: "Expenses" + end + + test "should create expense" do + visit expenses_url + click_on "New expense" + + check "Autopaid" if @expense.autopaid + fill_in "Description", with: @expense.description + check "Estimated" if @expense.estimated + fill_in "Payment", with: @expense.payment + fill_in "Period", with: @expense.period + click_on "Create Expense" + + assert_text "Expense was successfully created" + click_on "Back" + end + + test "should update Expense" do + visit expense_url(@expense) + click_on "Edit this expense", match: :first + + check "Autopaid" if @expense.autopaid + fill_in "Description", with: @expense.description + check "Estimated" if @expense.estimated + fill_in "Payment", with: @expense.payment + fill_in "Period", with: @expense.period + click_on "Update Expense" + + assert_text "Expense was successfully updated" + click_on "Back" + end + + test "should destroy Expense" do + visit expense_url(@expense) + click_on "Destroy this expense", match: :first + + assert_text "Expense was successfully destroyed" + end +end