remove-acts-as-taggable #38
| @ -6,14 +6,10 @@ class GuestsController < ApplicationController | |||||||
|   # GET /guests or /guests.json |   # GET /guests or /guests.json | ||||||
|   def index |   def index | ||||||
|     @guests = Guest.all |     @guests = Guest.all | ||||||
|                    .left_outer_joins(:affinity_groups) |                    .joins(:group) | ||||||
|                    .order('tags.name' => :asc) |                    .order('groups.name' => :asc) | ||||||
|                    .includes(:affinity_groups, :unbreakable_bonds) |  | ||||||
| 
 | 
 | ||||||
|                    respond_to do |format| |       render jsonapi: @guests | ||||||
|                       format.html |  | ||||||
|                       format.json { render jsonapi: @guests } |  | ||||||
|                    end |  | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   # GET /guests/1 or /guests/1.json |   # GET /guests/1 or /guests/1.json | ||||||
|  | |||||||
| @ -1,7 +1,9 @@ | |||||||
| 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' |   has_many :children, class_name: 'Group', foreign_key: 'parent_id' | ||||||
|     belongs_to :parent, class_name: 'Group', optional: true |   belongs_to :parent, class_name: 'Group', optional: true | ||||||
|  | 
 | ||||||
|  |   has_many :guests | ||||||
| end | end | ||||||
|  | |||||||
| @ -1,6 +1,6 @@ | |||||||
| class Guest < ApplicationRecord | class Guest < ApplicationRecord | ||||||
|   acts_as_taggable_on :affinity_groups, :unbreakable_bonds |   acts_as_taggable_on :affinity_groups, :unbreakable_bonds | ||||||
|    |   belongs_to :group | ||||||
|   def full_name |   def full_name | ||||||
|     "#{first_name} #{last_name}" |     "#{first_name} #{last_name}" | ||||||
|   end |   end | ||||||
|  | |||||||
| @ -1,9 +1,13 @@ | |||||||
| class SerializableGuest < JSONAPI::Serializable::Resource | class SerializableGuest < JSONAPI::Serializable::Resource | ||||||
|   type 'guest' |   type 'guest' | ||||||
| 
 | 
 | ||||||
|   attributes :id, :email |   attributes :id, :email, :group_id | ||||||
| 
 | 
 | ||||||
|   attribute :name do |   attribute :name do | ||||||
|     "#{@object.first_name} #{@object.last_name}" |     "#{@object.first_name} #{@object.last_name}" | ||||||
|   end |   end | ||||||
|  | 
 | ||||||
|  |   attribute :group_name do | ||||||
|  |     @object.group.name | ||||||
|  |   end | ||||||
| end | end | ||||||
|  | |||||||
							
								
								
									
										5
									
								
								db/migrate/20240811154115_add_group_to_guest.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								db/migrate/20240811154115_add_group_to_guest.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | |||||||
|  | class AddGroupToGuest < ActiveRecord::Migration[7.1] | ||||||
|  |   def change | ||||||
|  |     add_reference :guests, :group, null: false, foreign_key: true, type: :uuid | ||||||
|  |   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_143801) do | ActiveRecord::Schema[7.1].define(version: 2024_08_11_154115) 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" | ||||||
| 
 | 
 | ||||||
| @ -44,6 +44,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_08_11_143801) do | |||||||
|     t.string "phone" |     t.string "phone" | ||||||
|     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 "group_id", null: false | ||||||
|  |     t.index ["group_id"], name: "index_guests_on_group_id" | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   create_table "seats", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| |   create_table "seats", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| | ||||||
| @ -94,6 +96,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_08_11_143801) do | |||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   add_foreign_key "groups", "groups", column: "parent_id" |   add_foreign_key "groups", "groups", column: "parent_id" | ||||||
|  |   add_foreign_key "guests", "groups" | ||||||
|   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" | ||||||
|  | |||||||
							
								
								
									
										94
									
								
								db/seeds.rb
									
									
									
									
									
								
							
							
						
						
									
										94
									
								
								db/seeds.rb
									
									
									
									
									
								
							| @ -22,82 +22,44 @@ 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| | 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| |   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 close family", icon: 'pi pi-home') | ||||||
|     family.children.create!(name: "Jim's cousins", 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") |     family.children.create!(name: "Jim's relatives", icon: 'pi pi-home') | ||||||
|   end |   end | ||||||
|   parent.children.create!(name: "Jim's friends", icon: "pi pi-bullseye") |   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| |   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") |     work.children.create!(name: "Jim's besties at work", icon: 'pi pi-briefcase') | ||||||
|   end |   end | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
| Group.create!(name: "Pam's guests", icon: "pi pi-heart-fill").tap do |parent| | 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| |   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 close family", icon: 'pi pi-home') | ||||||
|     family.children.create!(name: "Pam's cousins", 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") |     family.children.create!(name: "Pam's relatives", icon: 'pi pi-home') | ||||||
|   end |   end | ||||||
|   parent.children.create!(name: "Pam's friends", icon: "pi pi-bullseye") |   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| |   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") |     work.children.create!(name: "Pam's besties at work", icon: 'pi pi-briefcase') | ||||||
|   end |   end | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
| Group.create!(name: "Common guests", icon: "pi pi-users").tap do |parent| | 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: 'College friends', icon: 'pi pi-calculator') | ||||||
|   parent.children.create!(name: "High school friends", icon: "pi pi-crown") |   parent.children.create!(name: 'High school friends', icon: 'pi pi-crown') | ||||||
|   parent.children.create!(name: "Childhood friends", icon: "pi pi-envelope") |   parent.children.create!(name: 'Childhood friends', icon: 'pi pi-envelope') | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
| samples = { | groups = Group.all | ||||||
|   close_family_a: 10, |  | ||||||
|   close_family_b: 10, |  | ||||||
|   cousins_a: 20, |  | ||||||
|   cousins_b: 15, |  | ||||||
|   relatives_a: 15, |  | ||||||
|   relatives_b: 10, |  | ||||||
|   work_a: 10, |  | ||||||
|   work_b: 10, |  | ||||||
|   besties_work_a: 5, |  | ||||||
|   besties_work_b: 5, |  | ||||||
|   college_friends_a: 10, |  | ||||||
|   college_friends_b: 10, |  | ||||||
|   high_school_friends_a: 10, |  | ||||||
|   high_school_friends_b: 10, |  | ||||||
|   childhood_friends_a: 10, |  | ||||||
|   childhood_friends_b: 10, |  | ||||||
|   basket_team_a: 10, |  | ||||||
|   football_team_a: 15, |  | ||||||
|   dance_club: 10 |  | ||||||
| }.each_with_object([]) do |(affinity_group, count), acc| |  | ||||||
|   count.times { acc << affinity_group } |  | ||||||
| end |  | ||||||
| 
 | 
 | ||||||
| NUMBER_OF_GUESTS.times do | NUMBER_OF_GUESTS.times do | ||||||
|   guest = Guest.create!(first_name: Faker::Name.first_name, |   Guest.create!( | ||||||
|                         last_name: Faker::Name.last_name, |     first_name: Faker::Name.first_name, | ||||||
|                         email: Faker::Internet.email, |     last_name: Faker::Name.last_name, | ||||||
|                         phone: Faker::PhoneNumber.cell_phone) |     email: Faker::Internet.email, | ||||||
| 
 |     phone: Faker::PhoneNumber.cell_phone, | ||||||
|   guest.affinity_group_list.add(samples.sample) |     group: groups.sample | ||||||
|   guest.save! |   ) | ||||||
| end |  | ||||||
| 
 |  | ||||||
| # Add unbreakable bonds |  | ||||||
| Guest.affinity_group_counts.each do |group| |  | ||||||
|   couples = (group.taggings_count / 4).floor |  | ||||||
| 
 |  | ||||||
|   guests_involved = Guest.tagged_with(group.name).limit(couples * 2) |  | ||||||
|   guests_involved.each_slice(2) do |a, b| |  | ||||||
|     bond_name = "#{a.full_name} & #{b.full_name}" |  | ||||||
| 
 |  | ||||||
|     a.unbreakable_bond_list.add(bond_name) |  | ||||||
|     b.unbreakable_bond_list.add(bond_name) |  | ||||||
| 
 |  | ||||||
|     a.save! |  | ||||||
|     b.save! |  | ||||||
|   end |  | ||||||
| end | end | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user