Grid model object updates

* Adds has_one association to canonical objects
* GridWeapon is_mainhand refactored
* GridCharacter add_awakening refactored
This commit is contained in:
Justin Edmund 2023-07-09 22:38:01 -07:00
parent a82e1512c2
commit 70e820b781
3 changed files with 10 additions and 8 deletions

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class GridCharacter < ApplicationRecord class GridCharacter < ApplicationRecord
has_one :object, class_name: 'Character', foreign_key: :id, primary_key: :character_id
belongs_to :awakening, optional: true belongs_to :awakening, optional: true
belongs_to :party, belongs_to :party,
counter_cache: :characters_count, counter_cache: :characters_count,
@ -87,9 +89,9 @@ class GridCharacter < ApplicationRecord
private private
def add_awakening def add_awakening
if self.awakening.nil? return unless awakening.nil?
self.awakening = Awakening.where(slug: "character-balanced").sole
end self.awakening = Awakening.where(slug: 'character-balanced').sole
end end
def check_value(property, type) def check_value(property, type)

View file

@ -5,6 +5,7 @@ class GridSummon < ApplicationRecord
counter_cache: :summons_count, counter_cache: :summons_count,
inverse_of: :summons inverse_of: :summons
validates_presence_of :party validates_presence_of :party
has_one :object, class_name: 'Summon', foreign_key: :id, primary_key: :summon_id
validate :compatible_with_position, on: :create validate :compatible_with_position, on: :create
validate :no_conflicts, on: :create validate :no_conflicts, on: :create
@ -23,6 +24,7 @@ class GridSummon < ApplicationRecord
party.summons.find do |grid_summon| party.summons.find do |grid_summon|
return unless grid_summon.id return unless grid_summon.id
grid_summon if summon.id == grid_summon.summon.id grid_summon if summon.id == grid_summon.summon.id
end end
end end

View file

@ -6,6 +6,8 @@ class GridWeapon < ApplicationRecord
inverse_of: :weapons inverse_of: :weapons
validates_presence_of :party validates_presence_of :party
has_one :object, class_name: 'Weapon', foreign_key: :id, primary_key: :weapon_id
belongs_to :weapon_key1, class_name: 'WeaponKey', foreign_key: :weapon_key1_id, optional: true 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 belongs_to :weapon_key2, class_name: 'WeaponKey', foreign_key: :weapon_key2_id, optional: true
belongs_to :weapon_key3, class_name: 'WeaponKey', foreign_key: :weapon_key3_id, optional: true belongs_to :weapon_key3, class_name: 'WeaponKey', foreign_key: :weapon_key3_id, optional: true
@ -78,10 +80,6 @@ class GridWeapon < ApplicationRecord
# Checks if the weapon should be a mainhand before saving the model # Checks if the weapon should be a mainhand before saving the model
def is_mainhand def is_mainhand
if self.position == -1 self.mainhand = position == -1
self.mainhand = true
else
self.mainhand = false
end
end end
end end