Skip to content

Commit

Permalink
Merge pull request #3 from ab-noori/home
Browse files Browse the repository at this point in the history
Home (Categories) page.
  • Loading branch information
ab-noori committed Sep 20, 2023
2 parents 2bcebe1 + 52ac056 commit 2c19cdb
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 9 deletions.
34 changes: 34 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class CategoriesController < ApplicationController
before_action :authenticate_user!

def index
@categories = Category.all
end

def show
@category = Category.find(params[:id])
end

def new
@category = Category.new
end

def create
@category = Category.new(category_params)
@category.user = current_user

if @category.save
redirect_to categories_path, notice: 'Category created successfully.'
else
flash.now[:alert] = 'Error creating category:'
flash.now[:alert_details] = @category.errors.full_messages.join(', ')
render :new
end
end

private

def category_params
params.require(:category).permit(:name, :icon)
end
end
4 changes: 3 additions & 1 deletion app/controllers/splash_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class SplashController < ApplicationController
skip_before_action :authenticate_user!, only: [:index]

def index; end
def index
redirect_to authenticated_root_path if user_signed_in?
end
end
2 changes: 2 additions & 0 deletions app/helpers/categories_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CategoriesHelper
end
1 change: 1 addition & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Category < ApplicationRecord
belongs_to :user
has_many :expense_category_associations
has_one_attached :icon, dependent: :destroy
has_many :expenses, through: :expense_category_associations
end
52 changes: 52 additions & 0 deletions app/views/categories/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<%= render partial: 'layouts/shared/navbar', locals: { back_destination: '', page_name: 'Categories', link: { destination: destroy_user_session_path, label: 'Log Out' } } %>
<%= render partial: "layouts/shared/messages" %>

<div class="container position-relative" style="min-height: 100vh;">
<div class="row">
<% @categories.each do |category| %>
<div class="col-12">
<%= link_to category_path(category), class: "nav-link" do %>
<div class="card">
<div class="row no-gutters align-items-center">
<div class="col-md-2 col-4">
<div class="card-body">
<% if category.icon.attached? %>
<%= image_tag category.icon, style: 'width: 80px; height: 80px;', class: 'img-fluid rounded' %>
<% else %>
<div class="text-center">
No Icon Available
</div>
<% end %>
</div>
</div>
<div class="col-md-8 col-5 p-0">
<div class="card-body text-secondary">
<h4 class="card-title"><%= category.name %></h4>
<p class="mb-0"><%= category.created_at.strftime("%d %b %Y %H:%M") %></p> <!-- Formatted date -->
</div>
</div>
<div class="col-md-2 col-3 text-md-center">
<div class="card-body text-dark font-weight-bold p-0 p-md-2">
<!-- Include the total for the category here -->
<h5 class="text-nowrap">$1500</h5>
</div>
</div>
</div>
</div>
<% end %>
</div>
<% end %>
</div>

<div class="sticky-bottom mb-2">
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<div class="d-grid gap-1">
<%= link_to "Add new category", new_category_path, class: "btn btn-success btn-lg mb-1" %>
</div>
</div>
</div>
</div>
</div>
</div>
30 changes: 30 additions & 0 deletions app/views/categories/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<%= render partial: 'layouts/shared/navbar', locals: { back_destination: root_path, page_name: 'New Category', link: { destination: destroy_user_session_path, label: 'Log Out' } } %>

<div class="container mt-4">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
New Category
</div>
<div class="card-body">
<%= form_with(model: @category, url: categories_path, method: :post) do |form| %>
<div class="mb-3">
<%= form.label :name, class: "form-label" %>
<%= form.text_field :name, class: "form-control" %>
</div>

<div class="mb-3">
<%= form.label :icon, class: "form-label" %>
<%= form.file_field :icon, class: "form-control" %>
</div>

<div class="text-center">
<%= form.submit 'Save', class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
33 changes: 33 additions & 0 deletions app/views/categories/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%= render partial: 'layouts/shared/navbar', locals: { back_destination: root_path, page_name: 'Transactions', link: { destination: destroy_user_session_path, label: 'Log Out' } } %>

<div class="container">
<div class="row">
<div class="card">
<div class="row no-gutters align-items-center">
<div class="col-md-2 col-4">
<div class="card-body">
<% if @category.icon.attached? %>
<%= image_tag @category.icon, style: 'width: 80px; height: 80px;', class: 'img-fluid rounded' %>
<% else %>
<div class="text-center">
No Icon Available
</div>
<% end %>
</div>
</div>
<div class="col-md-8 col-5 p-0">
<div class="card-body text-secondary">
<h4 class="card-title"><%= @category.name %></h4>
<p class="mb-0"><%= @category.created_at.strftime("%d %b %Y %H:%M") %></p> <!-- Formatted date -->
</div>
</div>
<div class="col-md-2 col-3 text-md-center">
<div class="card-body text-dark font-weight-bold p-0 p-md-2">
<!-- Include the total for the category here -->
<h5 class="text-nowrap">$1500</h5>
</div>
</div>
</div>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%= render partial: 'layouts/shared/navbar', locals: { back_destination: root_path, page_name: 'Sign Up', link: { destination: new_user_session_path, label: 'Log In' } } %>
<%= render partial: "layouts/shared/messages" %>

<div class="container mt-5">
<div class="row justify-content-center">
Expand Down
1 change: 1 addition & 0 deletions app/views/devise/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%= render partial: 'layouts/shared/navbar', locals: { back_destination: root_path, page_name: 'Log In', link: { destination: new_user_registration_path, label: 'Sign Up' } } %>
<%= render partial: "layouts/shared/messages" %>

<div class="container mt-5">
<div class="row justify-content-center">
Expand Down
3 changes: 1 addition & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
</head>

<body>
<%= render partial: "layouts/shared/messages" %>
<%= yield %>

<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/shared/_messages.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="container mt-4 col-md-4">
<div class="container mt-3 col-md-3">
<!-- Bootstrap alert for notice -->
<% if notice.present? %>
<div class="alert alert-success alert-dismissible fade show" role="alert">
Expand Down
15 changes: 13 additions & 2 deletions app/views/layouts/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
<a href="<%= back_destination %>" class="btn btn-link text-white">
<i class="fas fa-arrow-left fa-lg"></i>
</a>
<% else %>
<a href="#" class="btn btn-link text-white">
<i class="fas fa-bars fa-lg"></i>
</a>
<% end %>
<span class="navbar-brand mx-auto text-white"><%= page_name %></span>

<span class="navbar-brand mx-auto text-white text-center"> <!-- Added text-center class -->
<%= page_name %>
</span>

<% if link.present? %>
<a href="<%= link[:destination] %>" class="ml-2 text-white no-underline"><%= link[:label] %></a>
<% if link[:label] == "Log Out" %>
<%= link_to "Log Out", link[:destination], data: { turbo_method: :delete }, class: "text-white nav-link" %>
<% else %>
<a href="<%= link[:destination] %>" class="text-white nav-link"><%= link[:label] %></a>
<% end %>
<% end %>
</div>
</nav>
2 changes: 2 additions & 0 deletions app/views/splash/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<%= render partial: "layouts/shared/messages" %>

<div class="container text-center">
<div class="row mt-5">
<div class="col-md-6 offset-md-3">
Expand Down
11 changes: 9 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Rails.application.routes.draw do
devise_for :users

resources :users
root "splash#index"
resources :categories do
resources :expenses
end

authenticated :user do
root 'categories#index', as: :authenticated_root
end

root 'splash#index'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :key ], unique: true
end

create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false

t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end

private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
32 changes: 31 additions & 1 deletion db/schema.rb

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

7 changes: 7 additions & 0 deletions test/controllers/categories_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class CategoriesControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

0 comments on commit 2c19cdb

Please sign in to comment.