diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index b553519..d8b808b 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -1,5 +1,10 @@ class GroupsController < ApplicationController before_action { redirect_to splash_index_path unless user_signed_in? } - def index; end + def index + @categories = current_user.groups + end + def show; end + + def new; end end diff --git a/app/models/entity.rb b/app/models/entity.rb index 72aa3bf..0b16e48 100644 --- a/app/models/entity.rb +++ b/app/models/entity.rb @@ -4,4 +4,9 @@ class Entity < ApplicationRecord validates :name, presence: true validates :amount, presence: true validates :author, presence: true + after_save :update_group_transactions_counter + + def update_group_transactions_counter + group.update(transactions_counter: group.entities.count) + end end diff --git a/app/models/group.rb b/app/models/group.rb index 7e65d2d..1222e32 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -2,4 +2,8 @@ class Group < ApplicationRecord belongs_to :user has_many :entities validates :name, presence: true + def initialize(attributes = {}) + super + self.transactions_counter = 0 + end end diff --git a/app/views/app/_category_card.html.erb b/app/views/app/_category_card.html.erb new file mode 100644 index 0000000..71eb9fb --- /dev/null +++ b/app/views/app/_category_card.html.erb @@ -0,0 +1,10 @@ +
diff --git a/app/views/groups/index.html.erb b/app/views/groups/index.html.erb new file mode 100644 index 0000000..8248f54 --- /dev/null +++ b/app/views/groups/index.html.erb @@ -0,0 +1,5 @@ +<% @categories.each do |category| %> + <% @total_amount = category.entities.sum(:amount) %> + <%= render partial: 'app/category_card', locals: { category: category, total_amount: @total_amount } %> +<% end %> +<%= link_to 'New Group', new_group_path %> diff --git a/app/views/groups/new.html.erb b/app/views/groups/new.html.erb new file mode 100644 index 0000000..fb8fd46 --- /dev/null +++ b/app/views/groups/new.html.erb @@ -0,0 +1 @@ +Placeholder for the new group form. \ No newline at end of file diff --git a/app/views/groups/show.html.erb b/app/views/groups/show.html.erb new file mode 100644 index 0000000..62fcd0b --- /dev/null +++ b/app/views/groups/show.html.erb @@ -0,0 +1,3 @@ +This will display Entiteis for each group +<%# go back button %> +<%= link_to 'Back', groups_path %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 679340f..7948be3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,5 +3,6 @@ root to: 'groups#index' resources :users resources :splash, only: [:index] + resources :entities resources :groups end diff --git a/db/migrate/20231025194948_add_transactions_counter_to_groups.rb b/db/migrate/20231025194948_add_transactions_counter_to_groups.rb new file mode 100644 index 0000000..aa195ed --- /dev/null +++ b/db/migrate/20231025194948_add_transactions_counter_to_groups.rb @@ -0,0 +1,5 @@ +class AddTransactionsCounterToGroups < ActiveRecord::Migration[7.1] + def change + add_column :groups, :transactions_counter, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 681b937..0208865 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2023_10_24_222732) do +ActiveRecord::Schema[7.1].define(version: 2023_10_25_194948) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -31,6 +31,7 @@ t.string "icon" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "transactions_counter" t.index ["user_id"], name: "index_groups_on_user_id" end