diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb new file mode 100644 index 0000000..461a67f --- /dev/null +++ b/app/controllers/groups_controller.rb @@ -0,0 +1,5 @@ +class GroupsController < ApplicationController + def index + render jsonapi: Group.where(parent_id: nil), include: [children: [children: [:children]]] + end +end diff --git a/app/helpers/groups_helper.rb b/app/helpers/groups_helper.rb new file mode 100644 index 0000000..c091b2f --- /dev/null +++ b/app/helpers/groups_helper.rb @@ -0,0 +1,2 @@ +module GroupsHelper +end diff --git a/app/models/group.rb b/app/models/group.rb new file mode 100644 index 0000000..5a2e844 --- /dev/null +++ b/app/models/group.rb @@ -0,0 +1,7 @@ +class Group < ApplicationRecord + validates :name, uniqueness: true + validates :name, :order, presence: true + + has_many :children, class_name: 'Group', foreign_key: 'parent_id' + belongs_to :parent, class_name: 'Group', optional: true +end diff --git a/app/serializers/serializable_group.rb b/app/serializers/serializable_group.rb new file mode 100644 index 0000000..e90dc52 --- /dev/null +++ b/app/serializers/serializable_group.rb @@ -0,0 +1,7 @@ +class SerializableGroup < JSONAPI::Serializable::Resource + type 'group' + + attributes :name, :icon + + has_many :children +end diff --git a/config/routes.rb b/config/routes.rb index 67f091d..6e4d17b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :groups, only: :index resources :guests do post :import, on: :collection end diff --git a/db/migrate/20240811142121_create_groups.rb b/db/migrate/20240811142121_create_groups.rb new file mode 100644 index 0000000..0246978 --- /dev/null +++ b/db/migrate/20240811142121_create_groups.rb @@ -0,0 +1,13 @@ +class CreateGroups < ActiveRecord::Migration[7.1] + def change + create_table :groups, id: :uuid do |t| + t.string :name, null: false + t.string :icon + t.integer :order, null: false, default: 1 + + t.timestamps + end + + add_index :groups, :name, unique: true + end +end diff --git a/db/migrate/20240811143801_add_parent_to_group.rb b/db/migrate/20240811143801_add_parent_to_group.rb new file mode 100644 index 0000000..575af38 --- /dev/null +++ b/db/migrate/20240811143801_add_parent_to_group.rb @@ -0,0 +1,5 @@ +class AddParentToGroup < ActiveRecord::Migration[7.1] + def change + add_reference :groups, :parent, type: :uuid, index: true, foreign_key: { to_table: :groups } + end +end diff --git a/db/schema.rb b/db/schema.rb index 08c4026..00b4b60 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: 2024_07_24_181853) do +ActiveRecord::Schema[7.1].define(version: 2024_08_11_143801) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -26,6 +26,17 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_24_181853) do t.datetime "updated_at", null: false end + create_table "groups", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.string "name", null: false + t.string "icon" + t.integer "order", default: 1, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.uuid "parent_id" + t.index ["name"], name: "index_groups_on_name", unique: true + t.index ["parent_id"], name: "index_groups_on_parent_id" + end + create_table "guests", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.string "first_name" t.string "last_name" @@ -82,6 +93,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_24_181853) do t.index ["name"], name: "index_tags_on_name", unique: true end + add_foreign_key "groups", "groups", column: "parent_id" add_foreign_key "seats", "guests" add_foreign_key "seats", "tables_arrangements", on_delete: :cascade add_foreign_key "taggings", "tags" diff --git a/db/seeds.rb b/db/seeds.rb index 162a80f..5103a7d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,6 +5,7 @@ Expense.delete_all Guest.delete_all ActsAsTaggableOn::Tagging.delete_all ActsAsTaggableOn::Tag.delete_all +Group.delete_all Expense.create!(name: 'Photographer', amount: 3000, pricing_type: 'fixed') Expense.create!(name: 'Country house', amount: 6000, pricing_type: 'fixed') @@ -21,6 +22,35 @@ Expense.create!(name: 'Transportation', amount: 3000, pricing_type: 'fixed') Expense.create!(name: 'Invitations', amount: 200, pricing_type: 'fixed') Expense.create!(name: 'Cake', amount: 500, pricing_type: 'fixed') +Group.create!(name: "Jim's guests", icon: "pi pi-heart").tap do |parent| + parent.children.create!(name: "Jim's family", icon: "pi pi-users").tap do |family| + family.children.create!(name: "Jim's close family", icon: "pi pi-home") + family.children.create!(name: "Jim's cousins", icon: "pi pi-home") + family.children.create!(name: "Jim's relatives", icon: "pi pi-home") + end + parent.children.create!(name: "Jim's friends", icon: "pi pi-bullseye") + parent.children.create!(name: "Jim's work", icon: "pi pi-desktop").tap do |work| + work.children.create!(name: "Jim's besties at work", icon: "pi pi-briefcase") + end +end + +Group.create!(name: "Pam's guests", icon: "pi pi-heart-fill").tap do |parent| + parent.children.create!(name: "Pam's family", icon: "pi pi-users").tap do |family| + family.children.create!(name: "Pam's close family", icon: "pi pi-home") + family.children.create!(name: "Pam's cousins", icon: "pi pi-home") + family.children.create!(name: "Pam's relatives", icon: "pi pi-home") + end + parent.children.create!(name: "Pam's friends", icon: "pi pi-bullseye") + parent.children.create!(name: "Pam's work", icon: "pi pi-desktop").tap do |work| + work.children.create!(name: "Pam's besties at work", icon: "pi pi-briefcase") + end +end + +Group.create!(name: "Common guests", icon: "pi pi-users").tap do |parent| + parent.children.create!(name: "College friends", icon: "pi pi-calculator") + parent.children.create!(name: "High school friends", icon: "pi pi-crown") + parent.children.create!(name: "Childhood friends", icon: "pi pi-envelope") +end samples = { close_family_a: 10, diff --git a/spec/factories/groups.rb b/spec/factories/groups.rb new file mode 100644 index 0000000..a8fe677 --- /dev/null +++ b/spec/factories/groups.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :group do + name { "MyString" } + icon { "MyString" } + order { 1 } + end +end diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb new file mode 100644 index 0000000..be100cf --- /dev/null +++ b/spec/models/group_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Group, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end