1
0
Fork 1
impress/app/models/application_record.rb
Emi Matchu 97ffffb67a Add configurable full name field to alt styles
Sigh, the "Valentine Plushie" series is messing with me again! It
doesn't follow the previously established pattern of the names being
"<series> <color> <species>", because in this case the base color is
considered "Valentine".

Okay, well! In this change we add `full_name` as an explicit database
field, and set the previous full name value as a fallback. (We also
extract the generic fallback logic into `ApplicationRecord`, to help us
express it more concisely.)

We also tweak `adjective_name` to be able to shorten custom `full_name`
values, too. That way, in the outfit editor, the Styles options show
correct values like "Cherub Plushie" for the "Cherub Plushie Acara".

I also make some changes in the outfit editor to better accommodate the
longer series names, to try to better handle long words but also to
just only use the first word of the series main name anyway. (Currently,
all series main names are one word, except "Valentine Plushie" becomes
"Valentine".)
2025-02-15 21:52:47 -08:00

35 lines
No EOL
1.2 KiB
Ruby

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
# When the given attribute's value is nil, return the given block's value
# instead. This is useful for fields where there's a sensible derived value
# to use as the default for display purposes, but it's distinctly *not* the
# true value, and should be recognizable as such.
#
# This also creates methods `real_<attr>`, `real_<attr>=`, and `real_<attr>?`,
# to work with the actual attribute when necessary.
#
# It also creates `fallback_<attr>`, to find what the fallback value *would*
# be if the attribute's value were nil.
def self.fallback_for(attribute_name, &block)
define_method attribute_name do
read_attribute(attribute_name) || instance_eval(&block)
end
define_method "real_#{attribute_name}" do
read_attribute(attribute_name)
end
define_method "real_#{attribute_name}?" do
read_attribute(attribute_name).present?
end
define_method "real_#{attribute_name}=" do |new_value|
write_attribute(attribute_name, new_value)
end
define_method "fallback_#{attribute_name}" do
instance_eval(&block)
end
end
end