hensei-api/app/models/party.rb
Justin Edmund 78b5b063fc
Deploy July Quality Update (#114)
* Remove print statements from data migration

* (Hotfix) Fix data migration failing due to nil

* Fix stale migration errors

* Ensure new characters have Awakenings

Every character starts with Balanced Lv1 awakening

* Ensure weapons without awakenings do not expose key

* Updates database seeds (#109)

* Destroy favorites when a party is deleted

* Allow users to delete parties with remixes (#111)

There was a bug that prevented users from deleting parties with remixes, because the source party reference was not being nulled.

We fixed that with `dependent: :nullify` but also added a boolean key to the parties database to track if a party is a remix or not. This way, if a party is flagged as a remix but the source party is null, we know that the original party was deleted and can message this on the frontend.

* Fix deleting grid summons (#112)

A bad decision to try to reduce code in `set` made this fail with a 422 because `summon_params` was being tested against but... didn't exist? was nil?

I fixed it by not using `set` before calling `destroy`, and just finding the summon in the `destroy` method itself

* Usernames in URLs should not be case-sensitive (#113)

Amateur hour mistake

* Change default filters in users controller
2023-07-04 00:40:06 -07:00

163 lines
3.8 KiB
Ruby

# frozen_string_literal: true
class Party < ApplicationRecord
##### ActiveRecord Associations
belongs_to :source_party,
class_name: 'Party',
foreign_key: :source_party_id,
optional: true
has_many :derivative_parties,
class_name: 'Party',
foreign_key: :source_party_id,
inverse_of: :source_party,
dependent: :nullify
belongs_to :user, optional: true
belongs_to :raid, optional: true
belongs_to :job, optional: true
belongs_to :accessory,
foreign_key: 'accessory_id',
class_name: 'JobAccessory',
optional: true
belongs_to :skill0,
foreign_key: 'skill0_id',
class_name: 'JobSkill',
optional: true
belongs_to :skill1,
foreign_key: 'skill1_id',
class_name: 'JobSkill',
optional: true
belongs_to :skill2,
foreign_key: 'skill2_id',
class_name: 'JobSkill',
optional: true
belongs_to :skill3,
foreign_key: 'skill3_id',
class_name: 'JobSkill',
optional: true
belongs_to :guidebook1,
foreign_key: 'guidebook1_id',
class_name: 'Guidebook',
optional: true
belongs_to :guidebook2,
foreign_key: 'guidebook2_id',
class_name: 'Guidebook',
optional: true
belongs_to :guidebook3,
foreign_key: 'guidebook3_id',
class_name: 'Guidebook',
optional: true
has_many :characters,
foreign_key: 'party_id',
class_name: 'GridCharacter',
dependent: :destroy,
inverse_of: :party
has_many :weapons,
foreign_key: 'party_id',
class_name: 'GridWeapon',
dependent: :destroy,
inverse_of: :party
has_many :summons,
foreign_key: 'party_id',
class_name: 'GridSummon',
dependent: :destroy,
inverse_of: :party
has_many :favorites, dependent: :destroy
before_create :set_shortcode
before_create :set_edit_key
##### Amoeba configuration
amoeba do
set weapons_count: 0
set characters_count: 0
set summons_count: 0
nullify :description
nullify :shortcode
include_association :characters
include_association :weapons
include_association :summons
end
##### ActiveRecord Validations
validate :skills_are_unique
validate :guidebooks_are_unique
attr_accessor :favorited
def is_favorited(user)
user.favorite_parties.include? self if user
end
def is_remix
self.source_party != nil
end
def remixes
Party.where(source_party_id: self.id)
end
def blueprint
PartyBlueprint
end
private
def set_shortcode
self.shortcode = random_string
end
def set_edit_key
if !self.user
self.edit_key = Digest::SHA1.hexdigest([Time.now, rand].join)
end
end
def random_string
num_chars = 6
o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten
(0...num_chars).map { o[rand(o.length)] }.join
end
def skills_are_unique
skills = [skill0, skill1, skill2, skill3].compact
return if skills.uniq.length == skills.length
skills.each_with_index do |skill, index|
next if index.zero?
errors.add(:"skill#{index + 1}", 'must be unique') if skills[0...index].include?(skill)
end
errors.add(:job_skills, 'must be unique')
end
def guidebooks_are_unique
guidebooks = [guidebook1, guidebook2, guidebook3].compact
return if guidebooks.uniq.length == guidebooks.length
guidebooks.each_with_index do |book, index|
next if index.zero?
errors.add(:"guidebook#{index + 1}", 'must be unique') if guidebooks[0...index].include?(book)
end
errors.add(:guidebooks, 'must be unique')
end
end