generated from mailsg/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
324 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,74 @@ | ||
class CategoriesController < ApplicationController | ||
before_action :set_category, only: %i[show edit update destroy] | ||
|
||
# GET /categories or /categories.json | ||
def index | ||
@user = current_user | ||
@categories = @user.category | ||
end | ||
|
||
# GET /categories/1 or /categories/1.json | ||
def show | ||
@category = Category.find(params[:id]) | ||
@expenses = @category.expense.order(created_at: :desc) | ||
@categories = current_user.category | ||
end | ||
|
||
# GET /categories/new | ||
def new | ||
@user = current_user | ||
@category = Category.new | ||
end | ||
|
||
# POST /categories or /categories.json | ||
def create | ||
@user = current_user | ||
@category = @user.category.new(category_params) | ||
|
||
respond_to do |format| | ||
if @category.save | ||
format.html { redirect_to categories_url, notice: 'Category created successfully.' } | ||
format.json { render :show, status: :created, location: @category } | ||
else | ||
format.html { render :new, status: :unprocessable_entity } | ||
format.json { render json: @category.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
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 updated successfully .' } | ||
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]) | ||
@category.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to categories_url, notice: 'Category deleted successfully.' } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
|
||
# Callbacks to share common setup between actions. | ||
def set_category | ||
@category = Category.find(params[:id]) | ||
end | ||
|
||
# Allow a list of trusted parameters. | ||
def category_params | ||
params.require(:category).permit(:name, :icon) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,90 @@ | ||
class ExpensesController < ApplicationController | ||
before_action :set_expense, only: %i[show edit update destroy] | ||
|
||
# GET /expenses or /expenses.json | ||
def index | ||
@category = Category.find(params[:category_id]) | ||
@expenses = @category.expense.all | ||
end | ||
|
||
# GET /expenses/new | ||
def new | ||
@author = current_user | ||
@category = Category.find(params[:category_id]) | ||
@expense = Expense.new | ||
end | ||
|
||
# POST /expenses or /expenses.json | ||
def create | ||
@author = current_user | ||
saved_expenses = [] | ||
category_ids = params[:expense][:category_ids].reject(&:empty?).map(&:to_i).uniq | ||
|
||
category_ids.each do |c_id| | ||
@category = Category.find(c_id) | ||
@expense = Expense.new(expense_params) | ||
@expense.author_id = current_user.id | ||
@category.expense << @expense | ||
|
||
if @expense.save | ||
saved_expenses << @expense | ||
else | ||
# Handle the case where an expense can't be saved | ||
respond_to do |format| | ||
format.html { render :new, status: :unprocessable_entity } | ||
format.json { render json: @expense.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
save?(saved_expenses, category_ids) | ||
# Redirect to the first category's show page after all expenses have been processed | ||
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 updated successfully.' } | ||
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 successfully deleted.' } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
|
||
# Use callbacks to share common setup between actions. | ||
def set_expense | ||
@expense = Expense.find(params[:id]) | ||
end | ||
|
||
# Allow a list of trusted parameters. | ||
def expense_params | ||
params.require(:expense).permit(:name, :amount, :author_id) | ||
end | ||
|
||
def save?(saved_expenses, category_ids) | ||
if saved_expenses.present? | ||
redirect_to category_path(category_ids.first), notice: 'Expenses created successfully.' | ||
else | ||
# The case where no expenses were successfully saved | ||
respond_to do |format| | ||
format.html { render :new, status: :unprocessable_entity } | ||
format.json { render json: { error: 'No expenses saved.' }, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<%= stylesheet_link_tag "categories" %> | ||
|
||
<div id="<%= dom_id category %>" class= "category"> | ||
<div class="icon-name"> | ||
<img src='<%= category.icon %>' class= "icon"/> | ||
<span class="cname"> | ||
<p><%= category.name %></p> | ||
<p><%= category.created_at.to_date%></p> | ||
</span> | ||
</div> | ||
<p class="tamount"> | ||
$<%=category.total_amount%> | ||
</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<%=form_with(model: category) do |form| %> | ||
<% if category.errors.any? %> | ||
<div style="color: red"> | ||
<h2><%= pluralize(category.errors.count, "error") %> did not allow this to be saved:</h2> | ||
|
||
<ul> | ||
<% category.errors.each do |error| %> | ||
<li><%= error.full_message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<div class="field"> | ||
<%= form.label :name, style: "display: block" %> | ||
<%= form.text_field :name ,placeholder: "full name", autofocus:true %> | ||
</div> | ||
|
||
<div class="field"> | ||
<%= form.label :icon, style: "display: block" %> | ||
<%= form.text_field :icon , placeholder: "url" , autofocus:true %> | ||
</div> | ||
|
||
<div class="field actions"> | ||
<%= form.submit "save", class: "link_to" %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<% content_for :page_title, "Categories" %> | ||
<%= link_to "New category", new_category_path, class: "link_to newcat" %> | ||
<h1>Categories</h1> | ||
|
||
<div id="categories"> | ||
<% @categories.each do |category| %> | ||
<%= link_to render(category), category_path(category) %> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<%= stylesheet_link_tag "form" %> | ||
<%= stylesheet_link_tag "categories" %> | ||
<% content_for :page_title, "New Category" %> | ||
<div> | ||
<%= link_to " < Back", categories_path, class:"link_to back"%> | ||
</div> | ||
<h1>New category</h1> | ||
<section class = "new_cat"> | ||
<%= render "form", category: @category %> | ||
</section> | ||
<br> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<%= stylesheet_link_tag "categories" %> | ||
<% content_for :page_title, "Expenses" %> | ||
<p class="tamountE"> Total Amount: <%= @category.total_amount%> </p> | ||
|
||
<div id="expenses"> | ||
<% @expenses.each do |expense| %> | ||
<%= render expense %> | ||
<% end %> | ||
</div> | ||
|
||
<div > | ||
<div class = "links" > | ||
<%= link_to "Back to categories", categories_path , class: "link_to"%> | ||
<%= link_to "New expense", new_category_expense_path(@category) , class: "link_to" %> | ||
</div> | ||
<%= button_to "Delete this category", @category, method: :delete , class: "link_to delete" %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<%= stylesheet_link_tag "categories" %> | ||
<div id="<%= dom_id expense %>", class ="expense"> | ||
<p> | ||
<strong>Name:</strong> | ||
<%= expense.name %> | ||
</p> | ||
|
||
<p> | ||
<strong>Amount:</strong> | ||
<%= expense.amount %> | ||
</p> | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<%= form_with(model: [@category, @expense]) do |form| %> | ||
<% if expense.errors.any? %> | ||
<div style="color: red"> | ||
<h2><%= pluralize(expense.errors.count, "error") %> prohibited this expense from being saved:</h2> | ||
|
||
<ul> | ||
<% expense.errors.each do |error| %> | ||
<li><%= error.full_message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<div class = "field"> | ||
<%= form.label :name, style: "display: block" %> | ||
<%= form.text_field :name , required: true %> | ||
</div> | ||
|
||
<div class = "field"> | ||
<%= form.label :amount, style: "display: block" %> | ||
<%= form.text_field :amount , required: true%> | ||
</div> | ||
|
||
<div class="checkbox-exp"> | ||
<%= form.label :categories %> | ||
<%= form.collection_check_boxes(:category_ids, @author.category.all, :id, :name) do |b| %> | ||
<div class="checkbox"> | ||
<%= b.check_box %> | ||
<%= b.label %> | ||
</div> | ||
<% end %> | ||
</div> | ||
|
||
<div class = "field actions"> | ||
<%= form.submit 'Save', class: "link_to" %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<% content_for :page_title, "Expenses" %> | ||
|
||
<h1>Expenses</h1> | ||
|
||
<div id="expenses"> | ||
<% @expenses.each do |expense| %> | ||
<%= render expense %> | ||
<% end %> | ||
</div> | ||
|
||
<%= link_to "New expense", new_category_expense_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<%= stylesheet_link_tag "form" %> | ||
<%= stylesheet_link_tag "categories" %> | ||
|
||
<div> | ||
<%= link_to "Back", category_path(@category) , class:"link_to back" %> | ||
</div> | ||
|
||
<% content_for :page_title, "New Expense" %> | ||
<section class = "new-exp"> | ||
<%= render "form", expense: @expense %> | ||
</section> | ||
<br> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<p style="color: green"><%= notice %></p> | ||
|
||
<%= render @expense %> | ||
|
||
<div> | ||
<%= link_to "Edit this expense", edit_expense_path(@expense) %> | | ||
<%= link_to "Back to expenses", expenses_path %> | ||
<%= button_to "Delete this expense", @expense, method: :delete %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters