1
0
Fork 0
forked from OpenNeo/impress
impress/app/models/zone.rb
Matchu eaf875c6c7 zone, species, color alphabetical by the current locale
We were joining to the translations table to sort records
alphabetically, but then it sorted by *all* of the translations in
some strange way. Now use with_translations to restrict the join
to the current locale.
2013-01-27 00:25:52 -06:00

28 lines
877 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, lambda {
with_translations(I18n.locale).order(Zone::Translation.arel_table[:label])
}
scope :includes_translations, lambda { includes(:translations) }
scope :with_plain_label, lambda { |label|
t = Zone::Translation.arel_table
includes(:translations).where(t[:plain_label].eq(Zone.plainify_label(label)))
}
def uncertain_label
@sometimes ? "#{label} sometimes" : label
end
def self.all_plain_labels
Zone.select([:id]).includes(:translations).all.map(&:plain_label).uniq.sort
end
def self.plainify_label(label)
label.delete('\- /').parameterize
end
end