-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategories_controller.rb
54 lines (46 loc) · 1.47 KB
/
categories_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class CategoriesController < ApplicationController
before_action :set_category, only: :show
def index
categories = Category.all
render inertia: "Categories/Index", props: { categories: categories }
end
def show
render inertia: "Categories/Show", props: { category: serialized_category }
end
def create
category = Current.user.categories.new(category_params)
if category.save
redirect_to categories_url, notice: 'Category created successfully'
else
redirect_to categories_url, inertia: { errors: category.errors }
end
end
private
def set_category
@category = Category.friendly.find(params[:id])
end
def category_params
params.require(:category).permit(:name, :description, :color_code, :icon, :user_id)
end
def serialized_category
{
id: @category.id,
name: @category.name,
description: @category.description,
color_code: @category.color_code,
icon: @category.icon,
user_id: @category.user_id,
transactions: @category.transactions.map do |transaction|
{
id: transaction.id,
transaction_type: transaction.transaction_type.humanize,
amount: helpers.humanized_money_with_symbol(transaction.amount),
date: transaction.date.strftime("%B %d, %Y"),
notes: transaction.notes,
category_name: transaction.category.name,
category_color_code: transaction.category.color_code
}
end
}
end
end