impress/app/models/zone.rb
Matchu a29e016555 Update to new scope syntax
Ohh ok, without this change all of our `scope`s were just immediately evaluating the argument and fetching _all_ such matching records immediately, instead of waiting to actually be called. This led to bugs like `pet_type.as_json` returning ALL pet states in the whole db, because the `PetState.emotion_order` scope was being treated as a single predefined query, rather than a query fragment to merge into the current context.

This also explains what happened in 724ed83: that's why things before the scope in the query were being ignored.
2023-10-23 19:05:03 -07:00

27 lines
854 B
Ruby

class Zone < ActiveRecord::Base
translates :label, :plain_label
# When selecting zones that an asset occupies, we allow the zone to set
# whether or not the zone is "sometimes" occupied. This is false by default.
attr_writer :sometimes
scope :alphabetical, -> {
with_translations(I18n.locale).order(Zone::Translation.arel_table[:label])
}
scope :includes_translations, -> { includes(:translations) }
scope :with_plain_label, ->(label) {
t = Zone::Translation.arel_table
includes(:translations)
.where(t[:plain_label].eq(Zone.plainify_label(label)))
.where(t[:locale].eq(I18n.locale))
}
scope :for_items, -> { where(arel_table[:type_id].gt(1)) }
def uncertain_label
@sometimes ? "#{label} sometimes" : label
end
def self.plainify_label(label)
label.delete('\- /').parameterize
end
end