Skip to content

Commit

Permalink
Merge pull request #14 from ITurres/feature/transaction-form-page
Browse files Browse the repository at this point in the history
Issue #8 🎫:  transaction form page
  • Loading branch information
ITurres authored Jan 11, 2024
2 parents 5395007 + e861200 commit 960beec
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 147 deletions.
5 changes: 5 additions & 0 deletions app/assets/stylesheets/form.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ form {
margin-left: 25%;
}
}

select {
font-size: 1.2rem;
padding: 0.3rem 0.1rem 0.3rem 0.2rem;
}
4 changes: 3 additions & 1 deletion app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ class CategoriesController < ApplicationController

# GET /categories or /categories.json
def index
@categories = Category.all
@categories = Category.includes(category_transaction_details: [:transaction_detail]).where(user_id: current_user.id)

@categories
end

# GET /categories/new
Expand Down
69 changes: 25 additions & 44 deletions app/controllers/transaction_details_controller.rb
Original file line number Diff line number Diff line change
@@ -1,65 +1,46 @@
class TransactionDetailsController < ApplicationController
before_action :authenticate_user!
before_action :set_transaction_detail, only: %i[show edit update destroy]

# GET /transaction_details or /transaction_details.json
def index
@transaction_details = TransactionDetail.all
end

# GET /transaction_details/1 or /transaction_details/1.json
def show; end
before_action :set_transaction_detail, only: %i[destroy], except: %i[create]

# GET /transaction_details/new
def new
@transaction_detail = TransactionDetail.new
end

# GET /transaction_details/1/edit
def edit; end
@categories = Category.all
end

# POST /transaction_details or /transaction_details.json
def create
@transaction_detail = TransactionDetail.new(transaction_detail_params)

respond_to do |format|
if @transaction_detail.save
format.html do
redirect_to transaction_detail_url(@transaction_detail),
notice: 'Transaction detail was successfully created.'
end
format.json { render :show, status: :created, location: @transaction_detail }
@transaction_detail = TransactionDetail.new(name: transaction_detail_params[:name],
amount: transaction_detail_params[:amount],
user_id: current_user.id)

@transaction_detail.user = current_user

if @transaction_detail.save
# rubocop:disable Layout/LineLength
@category_transaction_details = CategoryTransactionDetail.new(category_id: transaction_detail_params[:category_id],
transaction_detail_id: @transaction_detail.id)
# rubocop:enable Layout/LineLength
if @category_transaction_details.save
flash[:success] = 'Transaction created!'
redirect_to category_path(transaction_detail_params[:category_id])
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @transaction_detail.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /transaction_details/1 or /transaction_details/1.json
def update
respond_to do |format|
if @transaction_detail.update(transaction_detail_params)
format.html do
redirect_to transaction_detail_url(@transaction_detail),
notice: 'Transaction detail was successfully updated.'
end
format.json { render :show, status: :ok, location: @transaction_detail }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @transaction_detail.errors, status: :unprocessable_entity }
flash[:danger] = 'Sorry, Try again!'
redirect_to new_transaction_detail_path
end
else
flash[:danger] = 'Sorry, Try again!'
redirect_to new_transaction_detail_path
end
end

# DELETE /transaction_details/1 or /transaction_details/1.json
def destroy
@transaction_detail.destroy!

respond_to do |format|
format.html { redirect_to transaction_details_url, notice: 'Transaction detail was successfully destroyed.' }
format.json { head :no_content }
end
flash[:info] = 'Transaction deleted!'
redirect_to transaction_details_path
end

private
Expand All @@ -71,6 +52,6 @@ def set_transaction_detail

# Only allow a list of trusted parameters through.
def transaction_detail_params
params.require(:transaction_detail).permit(:name, :amount, :user_id)
params.require(:transaction_detail).permit(:name, :amount, :category_id)
end
end
36 changes: 25 additions & 11 deletions app/views/transaction_details/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,36 @@
</div>
<% end %>

<div>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
<div class="field">
<%= form.text_field :name,
placeholder: "Name - max 40 characters",
aria: { label: "Name" },
required: true,
maxlength: 40,
class: "my-form-control" %>

</div>

<hr class="my-form__hr"> <!-- ! horizontal line -->
<div>
<%= form.label :amount, style: "display: block" %>
<%= form.text_field :amount %>
<%= form.number_field :amount,
placeholder: "$ Amount expended",
aria: { label: "Amount" },
required: true,
min: 0,
class: "my-form-control" %>
</div>

<div>
<%= form.label :user_id, style: "display: block" %>
<%= form.text_field :user_id %>
<div class="field btn-second--container p-3 mt-4 text-left">
<%= form.collection_select :category_id,
categories || [],
:id,
:name,
{ prompt: "Select a category" },
{ required: true,
class: "form-select btn-second-color" } %>
</div>

<div>
<%= form.submit %>
<div class="actions">
<%= form.submit "Add", class: "submit-btn" %>
</div>
<% end %>
17 changes: 0 additions & 17 deletions app/views/transaction_details/_transaction_detail.html.erb

This file was deleted.

14 changes: 0 additions & 14 deletions app/views/transaction_details/index.html.erb

This file was deleted.

10 changes: 2 additions & 8 deletions app/views/transaction_details/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
<h1>New transaction detail</h1>
<%= render "layouts/navbar", path: categories_path, title: "New transaction" %>

<%= render "form", transaction_detail: @transaction_detail %>

<br>

<div>
<%= link_to "Back to transaction details", transaction_details_path %>
</div>
<%= render "form", transaction_detail: @transaction_detail, categories: @categories %>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class CreateCategoryTransactionDetails < ActiveRecord::Migration[7.1]
def change
create_table :category_transaction_details do |t|
t.references :category, null: false, foreign_key: true
t.references :transaction_detail, null: false, foreign_key: true
t.references :category, null: false, foreign_key: { on_delete: :cascade}
t.references :transaction_detail, null: false, foreign_key: { on_delete: :cascade}

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCategoryIdToTransactionDetails < ActiveRecord::Migration[7.1]
def change
add_reference :transaction_details, :category, foreign_key: true
end
end
9 changes: 6 additions & 3 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions spec/controllers/transaction_details_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'rails_helper'

RSpec.describe TransactionDetailsController, type: :controller do
include Devise::Test::ControllerHelpers

let(:user) { create(:user) }
let(:category) { create(:category, user:) }
let(:transaction_detail) { create(:transaction_detail, user:) }

before do
sign_in user
end

describe 'GET #new' do
it 'returns a success response' do
get :new
expect(response).to be_successful
end
end

describe 'POST #create' do
context 'with valid params' do
it 'creates a new TransactionDetail' do
expect do
post :create, params: { transaction_detail: attributes_for(:transaction_detail) }
end.to change(TransactionDetail, :count).by(1)
end
end
end

describe 'DELETE #destroy' do
it 'destroys the requested transaction_detail' do
expect do
delete :destroy, params: { id: transaction_detail.id }
end.to change(TransactionDetail, :count).by(0)
end
end
end
99 changes: 52 additions & 47 deletions spec/examples.txt
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
example_id | status | run_time |
------------------------------------------------------------ | ------ | --------------- |
./spec/controllers/categories_controller_spec.rb[1:1:1] | passed | 0.5563 seconds |
./spec/controllers/categories_controller_spec.rb[1:2:1] | passed | 0.03806 seconds |
./spec/controllers/categories_controller_spec.rb[1:3:1:1] | passed | 0.04594 seconds |
./spec/controllers/categories_controller_spec.rb[1:3:1:2] | passed | 0.03319 seconds |
./spec/controllers/categories_controller_spec.rb[1:3:2:1] | passed | 0.02793 seconds |
./spec/controllers/categories_controller_spec.rb[1:4:1:1] | passed | 0.02859 seconds |
./spec/controllers/categories_controller_spec.rb[1:4:1:2] | passed | 0.03089 seconds |
./spec/controllers/categories_controller_spec.rb[1:4:2:1] | passed | 0.02773 seconds |
./spec/controllers/categories_controller_spec.rb[1:5:1] | passed | 0.0308 seconds |
./spec/controllers/categories_controller_spec.rb[1:5:2] | passed | 0.02746 seconds |
./spec/models/category_spec.rb[1:1] | passed | 0.47169 seconds |
./spec/models/category_spec.rb[1:2] | passed | 0.01377 seconds |
./spec/models/category_spec.rb[1:3] | passed | 0.01547 seconds |
./spec/models/category_spec.rb[1:4] | passed | 0.01538 seconds |
./spec/models/category_spec.rb[1:5:1] | passed | 0.07821 seconds |
./spec/models/transaction_detail_spec.rb[1:1] | passed | 0.0148 seconds |
./spec/models/transaction_detail_spec.rb[1:2] | passed | 0.02004 seconds |
./spec/models/transaction_detail_spec.rb[1:3] | passed | 0.0218 seconds |
./spec/models/transaction_detail_spec.rb[1:4] | passed | 0.01279 seconds |
./spec/models/user_spec.rb[1:1:1] | passed | 0.01018 seconds |
./spec/models/user_spec.rb[1:1:2:1] | passed | 0.01241 seconds |
./spec/models/user_spec.rb[1:1:3:1] | passed | 0.01055 seconds |
./spec/routing/categories_routing_spec.rb[1:1:1] | passed | 0.01427 seconds |
./spec/routing/categories_routing_spec.rb[1:1:2] | passed | 0.00891 seconds |
./spec/routing/categories_routing_spec.rb[1:1:3] | passed | 0.01014 seconds |
./spec/routing/categories_routing_spec.rb[1:1:4] | passed | 0.0038 seconds |
./spec/routing/categories_routing_spec.rb[1:1:5] | passed | 0.00337 seconds |
./spec/routing/categories_routing_spec.rb[1:1:6] | passed | 0.00252 seconds |
./spec/routing/categories_routing_spec.rb[1:1:7] | passed | 0.00359 seconds |
./spec/routing/categories_routing_spec.rb[1:1:8] | passed | 0.01188 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:1] | passed | 0.00274 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:2] | passed | 0.00575 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:3] | passed | 0.00335 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:4] | passed | 0.0054 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:5] | passed | 0.00566 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:6] | passed | 0.00454 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:7] | passed | 0.00508 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:8] | passed | 0.00486 seconds |
./spec/views/categories/new.html.erb_spec.rb[1:1] | passed | 4.16 seconds |
./spec/views/categories/new.html.erb_spec.rb[1:2] | passed | 1.87 seconds |
./spec/views/devise/registration/new.html.erb_spec.rb[1:1:1] | passed | 4.8 seconds |
./spec/views/devise/sessions/new.html.erb_spec.rb[1:1:1] | passed | 1.16 seconds |
./spec/views/home/index.html.erb_spec.rb[1:1] | passed | 2.8 seconds |
./spec/views/home/index.html.erb_spec.rb[1:2] | passed | 0.606 seconds |
./spec/views/home/index.html.erb_spec.rb[1:3] | passed | 0.49981 seconds |
example_id | status | run_time |
------------------------------------------------------------------ | ------ | --------------- |
./spec/controllers/categories_controller_spec.rb[1:1:1] | passed | 0.5563 seconds |
./spec/controllers/categories_controller_spec.rb[1:2:1] | passed | 0.03806 seconds |
./spec/controllers/categories_controller_spec.rb[1:3:1:1] | passed | 0.04594 seconds |
./spec/controllers/categories_controller_spec.rb[1:3:1:2] | passed | 0.03319 seconds |
./spec/controllers/categories_controller_spec.rb[1:3:2:1] | passed | 0.02793 seconds |
./spec/controllers/categories_controller_spec.rb[1:4:1:1] | passed | 0.02859 seconds |
./spec/controllers/categories_controller_spec.rb[1:4:1:2] | passed | 0.03089 seconds |
./spec/controllers/categories_controller_spec.rb[1:4:2:1] | passed | 0.02773 seconds |
./spec/controllers/categories_controller_spec.rb[1:5:1] | passed | 0.0308 seconds |
./spec/controllers/categories_controller_spec.rb[1:5:2] | passed | 0.02746 seconds |
./spec/controllers/transaction_details_controller_spec.rb[1:1:1] | passed | 0.49889 seconds |
./spec/controllers/transaction_details_controller_spec.rb[1:2:1:1] | passed | 0.0552 seconds |
./spec/controllers/transaction_details_controller_spec.rb[1:3:1] | passed | 0.01962 seconds |
./spec/models/category_spec.rb[1:1] | passed | 0.47169 seconds |
./spec/models/category_spec.rb[1:2] | passed | 0.01377 seconds |
./spec/models/category_spec.rb[1:3] | passed | 0.01547 seconds |
./spec/models/category_spec.rb[1:4] | passed | 0.01538 seconds |
./spec/models/category_spec.rb[1:5:1] | passed | 0.07821 seconds |
./spec/models/transaction_detail_spec.rb[1:1] | passed | 0.0148 seconds |
./spec/models/transaction_detail_spec.rb[1:2] | passed | 0.02004 seconds |
./spec/models/transaction_detail_spec.rb[1:3] | passed | 0.0218 seconds |
./spec/models/transaction_detail_spec.rb[1:4] | passed | 0.01279 seconds |
./spec/models/user_spec.rb[1:1:1] | passed | 0.01018 seconds |
./spec/models/user_spec.rb[1:1:2:1] | passed | 0.01241 seconds |
./spec/models/user_spec.rb[1:1:3:1] | passed | 0.01055 seconds |
./spec/routing/categories_routing_spec.rb[1:1:1] | passed | 0.01427 seconds |
./spec/routing/categories_routing_spec.rb[1:1:2] | passed | 0.00891 seconds |
./spec/routing/categories_routing_spec.rb[1:1:3] | passed | 0.01014 seconds |
./spec/routing/categories_routing_spec.rb[1:1:4] | passed | 0.0038 seconds |
./spec/routing/categories_routing_spec.rb[1:1:5] | passed | 0.00337 seconds |
./spec/routing/categories_routing_spec.rb[1:1:6] | passed | 0.00252 seconds |
./spec/routing/categories_routing_spec.rb[1:1:7] | passed | 0.00359 seconds |
./spec/routing/categories_routing_spec.rb[1:1:8] | passed | 0.01188 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:1] | passed | 0.00274 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:2] | passed | 0.00575 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:3] | passed | 0.00335 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:4] | passed | 0.0054 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:5] | passed | 0.00566 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:6] | passed | 0.00454 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:7] | passed | 0.00508 seconds |
./spec/routing/transaction_details_routing_spec.rb[1:1:8] | passed | 0.00486 seconds |
./spec/views/categories/new.html.erb_spec.rb[1:1] | passed | 4.16 seconds |
./spec/views/categories/new.html.erb_spec.rb[1:2] | passed | 1.87 seconds |
./spec/views/devise/registration/new.html.erb_spec.rb[1:1:1] | passed | 4.8 seconds |
./spec/views/devise/sessions/new.html.erb_spec.rb[1:1:1] | passed | 1.16 seconds |
./spec/views/home/index.html.erb_spec.rb[1:1] | passed | 2.8 seconds |
./spec/views/home/index.html.erb_spec.rb[1:2] | passed | 0.606 seconds |
./spec/views/home/index.html.erb_spec.rb[1:3] | passed | 0.49981 seconds |
./spec/views/transaction_details/new.html.erb_spec.rb[1:1] | passed | 4.47 seconds |
./spec/views/transaction_details/new.html.erb_spec.rb[1:2] | passed | 2.24 seconds |
Loading

0 comments on commit 960beec

Please sign in to comment.