hensei-api/app/models/grid_summon.rb
Justin Edmund b86f3a90f6 Remove N+1 from grid object models
Reimplementing `character` `summon` and `weapon` was making N+1s which made queries really slow
2025-02-07 01:53:21 -08:00

43 lines
1.1 KiB
Ruby

# frozen_string_literal: true
class GridSummon < ApplicationRecord
belongs_to :summon, foreign_key: :summon_id, primary_key: :id
belongs_to :party,
counter_cache: :summons_count,
inverse_of: :summons
validates_presence_of :party
validate :compatible_with_position, on: :create
validate :no_conflicts, on: :create
def blueprint
GridSummonBlueprint
end
# Returns conflicting summons if they exist
def conflicts(party)
return unless summon.limit
party.summons.find do |grid_summon|
return unless grid_summon.id
grid_summon if summon.id == grid_summon.summon.id
end
end
private
# Validates whether there is a conflict with the party
def no_conflicts
# Check if the grid summon conflicts with any of the other grid summons in the party
errors.add(:series, 'must not conflict with existing summons') unless conflicts(party).nil?
end
# Validates whether the summon can be added to the desired position
def compatible_with_position
return unless [4, 5].include?(position.to_i) && !summon.subaura
errors.add(:position, 'must have subaura for position')
end
end