impress/app/models/color.rb
Emi Matchu bd6b6450d9 Handle newly-released species in Item Getting Guide
This is less likely than the newly-released color case for PB items,
but I figure let's be resilient anyway, especially since it's so easy
to—and also I figure this is less likely to be triggered by an *actual*
new species, and more likely to be triggered by a surprise in an item's
naming conventions.

But yeah, if `Item#pb_species` returns `nil` upstream, it'll be passed
to `Color#example_pet_type`, which will crash trying to read its ID. So
in this change, we update `Color#example_pet_type` to accept a `nil`
value, and fall back to the first Species (Acara) in that case.

This means that, if you e.g. take the Mutant Aisha Collar and delete
the word "Aisha" from the name, then load it in the Item Getting Guide,
you'll see a thumbnail of a Mutant Acara. Good enough!
2024-06-05 19:27:38 -07:00

42 lines
1.1 KiB
Ruby

class Color < ApplicationRecord
has_many :pet_types
scope :alphabetical, -> { order(:name) }
scope :basic, -> { where(basic: true) }
scope :standard, -> { where(standard: true) }
scope :nonstandard, -> { where(standard: false) }
scope :funny, -> { order(:prank) unless pranks_funny? }
validates :name, presence: true
def as_json(options={})
{id: id, name: name, human_name: human_name}
end
def human_name
if prank? && !Color.pranks_funny?
unfunny_human_name + ' ' + I18n.translate('colors.prank_suffix')
else
unfunny_human_name
end
end
def example_pet_type(preferred_species: nil)
preferred_species ||= Species.first
pet_types.order([Arel.sql("species_id = ? DESC"), preferred_species.id],
"species_id ASC").first
end
def unfunny_human_name
if name
name.split(' ').map { |word| word.capitalize }.join(' ')
else
I18n.translate('colors.default_human_name')
end
end
def self.pranks_funny?
now = Time.now.in_time_zone('Pacific Time (US & Canada)')
now.month == 4 && now.day == 1
end
end