impress/app/models/zone.rb
Matchu 111d4dcaee Fix bug with Arel ordering
Ok so weird little situation, usually Arel will accept an attribute as a param to `order()`, but not when it's in a very specific situation of all of the following:

`Item.joins(:translations).includes(:translations).limit(30).order(Item::Translation.arel_table[:name])`

For some reason, it's all like "hey I can't call `to_sql` on an attribute!", but only in the scenario where all 3 of those other things are present. Weird!

Anyway, explicitly saying `.asc` fixes this. Ok!
2023-08-02 16:29:31 -07:00

28 lines
882 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, -> {
zt = Zone::Translation.arel_table
with_translations(I18n.locale).order(zt[:label].asc)
}
scope :includes_translations, -> { includes(:translations) }
scope :matching_label, ->(label, locale = I18n.locale) {
t = Zone::Translation.arel_table
joins(:translations)
.where(t[:locale].eq(locale))
.where(t[:plain_label].eq(Zone.plainify_label(label)))
}
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