From 52d417a693101c608f4a729c0ddd43b3e8e40210 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 10 Feb 2025 18:15:53 -0800 Subject: [PATCH] Update party.rb We moved updating the party's element and extra flag to inside the party. We use an after_commit hook to minimize the amount of queries we're running to do this. --- app/models/party.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/models/party.rb b/app/models/party.rb index 19f7dc1..fc3dce4 100644 --- a/app/models/party.rb +++ b/app/models/party.rb @@ -87,6 +87,9 @@ class Party < ApplicationRecord before_create :set_shortcode before_create :set_edit_key + after_commit :update_element!, on: %i[create update] + after_commit :update_extra!, on: %i[create update] + ##### Amoeba configuration amoeba do set weapons_count: 0 @@ -194,6 +197,21 @@ class Party < ApplicationRecord private + def update_element! + main_weapon = weapons.detect { |gw| gw.position.to_i == -1 } + new_element = main_weapon&.weapon&.element + if new_element.present? && self.element != new_element + update_column(:element, new_element) + end + end + + def update_extra! + new_extra = weapons.any? { |gw| GridWeapon::EXTRA_POSITIONS.include?(gw.position.to_i) } + if self.extra != new_extra + update_column(:extra, new_extra) + end + end + def set_shortcode self.shortcode = random_string end