From ff85de2f86a455ce7e0fe381d7f8ed35b5ca9e20 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 14 Mar 2022 18:20:12 -0700 Subject: [PATCH 1/4] Add counter cache for number of weapons in a party --- db/migrate/20220315005952_add_weapons_count_to_parties.rb | 5 +++++ db/migrate/20220315011802_populate_party_weapons_count.rb | 7 +++++++ db/schema.rb | 7 ++----- 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20220315005952_add_weapons_count_to_parties.rb create mode 100644 db/migrate/20220315011802_populate_party_weapons_count.rb diff --git a/db/migrate/20220315005952_add_weapons_count_to_parties.rb b/db/migrate/20220315005952_add_weapons_count_to_parties.rb new file mode 100644 index 0000000..cbd8e8e --- /dev/null +++ b/db/migrate/20220315005952_add_weapons_count_to_parties.rb @@ -0,0 +1,5 @@ +class AddWeaponsCountToParties < ActiveRecord::Migration[6.1] + def change + add_column :parties, :weapons_count, :integer + end +end diff --git a/db/migrate/20220315011802_populate_party_weapons_count.rb b/db/migrate/20220315011802_populate_party_weapons_count.rb new file mode 100644 index 0000000..ff52183 --- /dev/null +++ b/db/migrate/20220315011802_populate_party_weapons_count.rb @@ -0,0 +1,7 @@ +class PopulatePartyWeaponsCount < ActiveRecord::Migration[6.1] + def up + Party.find_each do |party| + Party.reset_counters(party.id, :weapons) + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 33e1e82..e3f8601 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,14 +10,13 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_03_09_013333) do +ActiveRecord::Schema.define(version: 2022_03_15_005952) do # These are extensions that must be enabled in order to support this database enable_extension "btree_gin" enable_extension "pg_trgm" enable_extension "pgcrypto" enable_extension "plpgsql" - enable_extension "timescaledb" create_table "characters", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.string "name_en" @@ -100,9 +99,6 @@ ActiveRecord::Schema.define(version: 2022_03_09_013333) do t.integer "element" t.index ["party_id"], name: "index_grid_weapons_on_party_id" t.index ["weapon_id"], name: "index_grid_weapons_on_weapon_id" - t.index ["weapon_key1_id"], name: "index_grid_weapons_on_weapon_key1_id" - t.index ["weapon_key2_id"], name: "index_grid_weapons_on_weapon_key2_id" - t.index ["weapon_key3_id"], name: "index_grid_weapons_on_weapon_key3_id" end create_table "oauth_access_grants", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| @@ -153,6 +149,7 @@ ActiveRecord::Schema.define(version: 2022_03_09_013333) do t.text "description" t.uuid "raid_id" t.integer "element" + t.integer "weapons_count" t.index ["user_id"], name: "index_parties_on_user_id" end From 72c144ef651ed264419648f7ec04c7c462cab152 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 14 Mar 2022 18:20:29 -0700 Subject: [PATCH 2/4] Add counter_cache flag to GridWeapon --- app/models/grid_weapon.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/grid_weapon.rb b/app/models/grid_weapon.rb index faf50cb..aed825e 100644 --- a/app/models/grid_weapon.rb +++ b/app/models/grid_weapon.rb @@ -1,5 +1,6 @@ class GridWeapon < ApplicationRecord - belongs_to :party + belongs_to :party, + counter_cache: :weapons_count belongs_to :weapon_key1, class_name: 'WeaponKey', foreign_key: :weapon_key1_id, optional: true belongs_to :weapon_key2, class_name: 'WeaponKey', foreign_key: :weapon_key2_id, optional: true From c7196f7c795352b399a1b11d68caf50cf9a08ffb Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 14 Mar 2022 18:21:40 -0700 Subject: [PATCH 3/4] Add a permanent weapon count condition to Teams This way, Discover Teams will only show high quality parties --- app/controllers/api/v1/parties_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/api/v1/parties_controller.rb b/app/controllers/api/v1/parties_controller.rb index 4038648..a612e7f 100644 --- a/app/controllers/api/v1/parties_controller.rb +++ b/app/controllers/api/v1/parties_controller.rb @@ -25,6 +25,7 @@ class Api::V1::PartiesController < Api::V1::ApiController conditions[:element] = request.params['element'] unless request.params['element'].blank? conditions[:raid] = request.params['raid'] unless request.params['raid'].blank? conditions[:created_at] = start_time..now unless request.params['recency'].blank? + conditions[:weapons_count] = 5..13 @parties = Party.where(conditions).each { |party| party.favorited = (current_user) ? party.is_favorited(current_user) : false From e6a09ab29b95aa20647ad22d7f2c1d52dcfa4ecc Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 14 Mar 2022 18:21:54 -0700 Subject: [PATCH 4/4] Make has_many readable --- app/models/party.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/app/models/party.rb b/app/models/party.rb index 0f0c080..20731ba 100644 --- a/app/models/party.rb +++ b/app/models/party.rb @@ -3,9 +3,20 @@ class Party < ApplicationRecord belongs_to :user, optional: true belongs_to :raid, optional: true - has_many :characters, foreign_key: "party_id", class_name: "GridCharacter", dependent: :destroy - has_many :weapons, foreign_key: "party_id", class_name: "GridWeapon", dependent: :destroy - has_many :summons, foreign_key: "party_id", class_name: "GridSummon", dependent: :destroy + has_many :characters, + foreign_key: "party_id", + class_name: "GridCharacter", + dependent: :destroy + + has_many :weapons, + foreign_key: "party_id", + class_name: "GridWeapon", + dependent: :destroy + + has_many :summons, + foreign_key: "party_id", + class_name: "GridSummon", + dependent: :destroy has_many :favorites attr_accessor :favorited