1
0
Fork 0
forked from OpenNeo/impress
impress/app/models/species.rb
Emi Matchu 0dca538b0a Start migrating off globalize gem for species/color names
Non-English languages haven't been supported on Neopets for a while, so
I'd like to remove this extra cross-cutting complexity, especially
since it's now inconsistent with the real site anyway!

The main motivation is that I'd like to do this for items too, because
I have a hunch that all the complexity of `globalize` to read
`item.name` is part of what's making large user lists so slow to
render: lots of little objects getting created down the stack, and
needing to be garbage-collected later.

I'm not sure that's why! But I figure removing this complexity is a
simplicity win anyway, so let's do it!

Note that this doesn't *finish* the migration, it just starts it! The
`Species::Translation` and `Color::Translation` models still exist, and
still have their data, and not all references to them are scrubbed yet.
I especially don't want to delete the backing tables until both DTI and
DTI 2020 are ready for it!

So this change will someday be paired with another change to actually
drop the tables - after backing up the data for future records just in
case, of course!
2024-01-23 05:10:43 -08:00

36 lines
971 B
Ruby

class Species < ApplicationRecord
translates # TODO: Remove once we're all done with translations!
has_many :pet_types
scope :alphabetical, -> {
st = Species::Translation.arel_table
with_translations(I18n.locale).order(st[:name].asc)
}
scope :with_body_id, -> body_id {
pt = PetType.arel_table
joins(:pet_types).where(pt[:body_id].eq(body_id)).limit(1)
}
# Temporary writer to keep the English translation record updated, while
# primarily using the attribute on the model itself.
#
# Once this app and DTI 2020 are both comfortably off the translation system,
# we can remove this!
def name=(new_name)
globalize.write(:en, :name, new_name)
write_attribute(:name, new_name)
end
def as_json(options={})
super({only: [:id, :name], methods: [:human_name]}.merge(options))
end
def human_name
if name
name.capitalize
else
I18n.translate('species.default_human_name')
end
end
end