Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Controllers adn Views #4

Merged
merged 8 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 1 addition & 24 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
class ApplicationController < ActionController::Base
before_action :authenticate_user!
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?

# This is a helper method that can be used in any controller
# to add a server timing header to the response.
def add_server_timing_header(name, duration)
# If the header already exists, append the new timing to the existing header.
if response.headers['Server-Timing'].present?
response.headers['Server-Timing'] += ", #{name};dur=#{duration}"
else
response.headers['Server-Timing'] = "#{name};dur=#{duration}"
end
end

protected

def after_sign_in_path_for(_resource)
home_index_path
end

def after_sign_out_path_for(_resource_or_scope)
new_user_session_path
end

def after_inactive_sign_up_path_for(_resource)
new_user_session_path
end

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: %i[name email password password_confirmation])
devise_parameter_sanitizer.permit(:account_update,
Expand Down
67 changes: 67 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class CategoriesController < ApplicationController
before_action :set_category, only: %i[show edit update destroy]
before_action :authenticate_user!

def index
@categories = Category.where(author_id: current_user.id).order(created_at: :desc) || []
end

def show
@categories = Category.includes(:entities).find(params[:id])
@entities = @category.entities.order(created_at: :desc)
end

def new
@category = Category.new
end

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

respond_to do |format|
if @category.save
format.html { redirect_to categories_path(@category.id), notice: 'Category was successfully created.' }
else
format.html { render :new, status: :unprocessable_entity }
end
end
end

def edit; end

# PATCH/PUT /categories/1 or /categories/1.json
def update
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to category_url(@category), notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: @category }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end

# DELETE /categories/1 or /categories/1.json
def destroy
@category = Category.find(params[:id])
respond_to do |format|
if @category.destroy
format.html { redirect_to categories_path, notice: 'Category was successfully destroyed.' }
else
format.html { redirect_to categories_path, alert: 'Category was not destroyed.' }
end
end
end

private

def set_category
@category = current_user.categories.find(params[:id])
end

def category_params
params.require(:category).permit(:name, :icon)
end
end
77 changes: 77 additions & 0 deletions app/controllers/entities_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class EntitiesController < ApplicationController
before_action :set_entity, only: %i[show edit update destroy]

# GET /entities or /entities.json
def index
@category = Category.find(params[:category_id])
@entities = Entity.where(category_id: @category.id).order('created_at DESC')
end

# GET /entities/1 or /entities/1.json
def show; end

# GET /entities/new
def new
@entity = Entity.new
@categories = Category.where(author_id: current_user.id) || []
@category_id = params[:category_id].to_i
end

# POST /entities or /entities.json
def create
@entity = Entity.new(entity_params)
@entity.author_id = current_user.id
@entity.category_id = params[:category_id].to_i

respond_to do |format|
if @entity.save
format.html do
redirect_to category_entities_path,
notice: 'Transaction was successfully created.'
end
format.json { render :show, status: :created, location: @entity }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @entity.errors, status: :unprocessable_entity }
end
end
end

# GET /entities/1/edit
def edit; end

# PATCH/PUT /entities/1 or /entities/1.json
def update
respond_to do |format|
if @entity.update(entity_params)
format.html { redirect_to entity_url(@entity), notice: 'Entity was successfully updated.' }
format.json { render :show, status: :ok, location: @entity }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @entity.errors, status: :unprocessable_entity }
end
end
end

# DELETE /entities/1 or /entities/1.json
def destroy
@entity.destroy

respond_to do |format|
format.html { redirect_to entities_url, notice: 'Entity was successfully destroyed.' }
format.json { head :no_content }
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_entity
@entity = Entity.find(params[:id])
end

# Only allow a list of trusted parameters through.
def entity_params
params.require(:entity).permit(:name, :amount, :category_id, category_ids: []).merge(author_id: current_user.id)
end
end
4 changes: 3 additions & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class HomeController < ApplicationController
def index; end
def index
redirect_to categories_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
2 changes: 2 additions & 0 deletions app/helpers/entities_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module EntitiesHelper
end
7 changes: 3 additions & 4 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Category < ApplicationRecord
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
has_many :categories_entities, dependent: :destroy
has_many :entities, through: :categories_entities
has_many :entities, dependent: :destroy

validates :name, presence: true, length: { minimum: 3, maximum: 60 }
validates :icon, presence: true
Expand All @@ -10,7 +9,7 @@ def self.default_icon
'fas fa-question-circle'
end

def total_amount
entities.sum(:amount)
def total_transactions
entities.where(category_id: id).sum(:amount).to_f
end
end
7 changes: 0 additions & 7 deletions app/models/category_entity.rb

This file was deleted.

3 changes: 1 addition & 2 deletions app/models/entity.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Entity < ApplicationRecord
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
has_many :categories_entities, dependent: :destroy
has_many :categories, through: :categories_entities
belongs_to :category, class_name: 'Category', foreign_key: 'category_id'

validates :name, presence: true, length: { minimum: 3, maximum: 60 }
validates :amount, presence: true, numericality: { greater_than: 0 }
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable

has_many :categories, dependent: :destroy
Expand Down
10 changes: 9 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
Rails.application.routes.draw do

resources :categories do
resources :entities
end

devise_scope :user do
get 'logout', to: 'devise/sessions#destroy'
end

devise_for :users
root "home#index"

end
10 changes: 0 additions & 10 deletions db/migrate/20230727200438_create_categories_entities.rb

This file was deleted.

8 changes: 4 additions & 4 deletions db/migrate/20230727220739_add_devise_to_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def self.up
# t.string :last_sign_in_ip

## Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable

## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddCategoriesReferencestoEntities < ActiveRecord::Migration[7.0]
def change
add_reference :entities, :category, foreign_key: { to_table: :categories } do
end
end
end
20 changes: 4 additions & 16 deletions 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
7 changes: 7 additions & 0 deletions test/controllers/entities_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

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