groups-structure #36
| @ -1,5 +1,5 @@ | |||||||
| class GroupsController < ApplicationController | class GroupsController < ApplicationController | ||||||
|   def index |   def index | ||||||
|     render jsonapi: Group.all |     render jsonapi: Group.where(parent_id: nil), include: [children: [children: [:children]]] | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  | |||||||
| @ -1,4 +1,7 @@ | |||||||
| class Group < ApplicationRecord | class Group < ApplicationRecord | ||||||
|     validates :name, uniqueness: true |     validates :name, uniqueness: true | ||||||
|     validates :name, :order, presence: 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 | end | ||||||
|  | |||||||
| @ -2,4 +2,6 @@ class SerializableGroup < JSONAPI::Serializable::Resource | |||||||
|   type 'group' |   type 'group' | ||||||
| 
 | 
 | ||||||
|   attributes :name, :icon |   attributes :name, :icon | ||||||
|  | 
 | ||||||
|  |   has_many :children | ||||||
| end | end | ||||||
|  | |||||||
							
								
								
									
										5
									
								
								db/migrate/20240811143801_add_parent_to_group.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								db/migrate/20240811143801_add_parent_to_group.rb
									
									
									
									
									
										Normal file
									
								
							| @ -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 | ||||||
							
								
								
									
										5
									
								
								db/schema.rb
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										5
									
								
								db/schema.rb
									
									
									
										generated
									
									
									
								
							| @ -10,7 +10,7 @@ | |||||||
| # | # | ||||||
| # It's strongly recommended that you check this file into your version control system. | # It's strongly recommended that you check this file into your version control system. | ||||||
| 
 | 
 | ||||||
| ActiveRecord::Schema[7.1].define(version: 2024_08_11_142121) 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 |   # These are extensions that must be enabled in order to support this database | ||||||
|   enable_extension "plpgsql" |   enable_extension "plpgsql" | ||||||
| 
 | 
 | ||||||
| @ -32,7 +32,9 @@ ActiveRecord::Schema[7.1].define(version: 2024_08_11_142121) do | |||||||
|     t.integer "order", default: 1, null: false |     t.integer "order", default: 1, null: false | ||||||
|     t.datetime "created_at", null: false |     t.datetime "created_at", null: false | ||||||
|     t.datetime "updated_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 ["name"], name: "index_groups_on_name", unique: true | ||||||
|  |     t.index ["parent_id"], name: "index_groups_on_parent_id" | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   create_table "guests", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| |   create_table "guests", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| | ||||||
| @ -91,6 +93,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_08_11_142121) do | |||||||
|     t.index ["name"], name: "index_tags_on_name", unique: true |     t.index ["name"], name: "index_tags_on_name", unique: true | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  |   add_foreign_key "groups", "groups", column: "parent_id" | ||||||
|   add_foreign_key "seats", "guests" |   add_foreign_key "seats", "guests" | ||||||
|   add_foreign_key "seats", "tables_arrangements", on_delete: :cascade |   add_foreign_key "seats", "tables_arrangements", on_delete: :cascade | ||||||
|   add_foreign_key "taggings", "tags" |   add_foreign_key "taggings", "tags" | ||||||
|  | |||||||
							
								
								
									
										30
									
								
								db/seeds.rb
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								db/seeds.rb
									
									
									
									
									
								
							| @ -22,6 +22,35 @@ Expense.create!(name: 'Transportation', amount: 3000, pricing_type: 'fixed') | |||||||
| Expense.create!(name: 'Invitations', amount: 200, pricing_type: 'fixed') | Expense.create!(name: 'Invitations', amount: 200, pricing_type: 'fixed') | ||||||
| Expense.create!(name: 'Cake', amount: 500, 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 = { | samples = { | ||||||
|   close_family_a: 10, |   close_family_a: 10, | ||||||
| @ -44,7 +73,6 @@ samples = { | |||||||
|   football_team_a: 15, |   football_team_a: 15, | ||||||
|   dance_club: 10 |   dance_club: 10 | ||||||
| }.each_with_object([]) do |(affinity_group, count), acc| | }.each_with_object([]) do |(affinity_group, count), acc| | ||||||
|   Group.find_or_create_by!(name: affinity_group.to_s.humanize) |  | ||||||
|   count.times { acc << affinity_group } |   count.times { acc << affinity_group } | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user