Start migrating off globalize gem for zones

Like in 0dca538, this is preliminary work for being able to drop the
`zone_translations` table! We're copying the field over first, to be
able to migrate DTI 2020 safely before dropping anything.
This commit is contained in:
Emi Matchu 2024-01-23 05:43:00 -08:00
parent 48f613d9bc
commit 73af9d4d77
5 changed files with 63 additions and 32 deletions

View file

@ -68,13 +68,13 @@ class Item < ApplicationRecord
where('description NOT LIKE ?',
'%' + sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%')
}
scope :occupies, ->(zone_label, locale = I18n.locale) {
zone_ids = Zone.matching_label(zone_label, locale).map(&:id)
scope :occupies, ->(zone_label) {
zone_ids = Zone.matching_label(zone_label).map(&:id)
sa = SwfAsset.arel_table
joins(:swf_assets).where(sa[:zone_id].in(zone_ids)).distinct
}
scope :not_occupies, ->(zone_label, locale = I18n.locale) {
zone_ids = Zone.matching_label(zone_label, locale).map(&:id)
scope :not_occupies, ->(zone_label) {
zone_ids = Zone.matching_label(zone_label).map(&:id)
i = Item.arel_table
sa = SwfAsset.arel_table
# Querying for "has NO swf_assets matching these zone IDs" is trickier than
@ -87,13 +87,13 @@ class Item < ApplicationRecord
condition = zone_ids.map { 'FIND_IN_SET(?, GROUP_CONCAT(zone_id)) = 0' }.join(' AND ')
joins(:swf_assets).group(i[:id]).having(condition, *zone_ids).distinct
}
scope :restricts, ->(zone_label, locale = I18n.locale) {
zone_ids = Zone.matching_label(zone_label, locale).map(&:id)
scope :restricts, ->(zone_label) {
zone_ids = Zone.matching_label(zone_label).map(&:id)
condition = zone_ids.map { '(SUBSTR(items.zones_restrict, ?, 1) = "1")' }.join(' OR ')
where(condition, *zone_ids)
}
scope :not_restricts, ->(zone_label, locale = I18n.locale) {
zone_ids = Zone.matching_label(zone_label, locale).map(&:id)
scope :not_restricts, ->(zone_label) {
zone_ids = Zone.matching_label(zone_label).map(&:id)
condition = zone_ids.map { '(SUBSTR(items.zones_restrict, ?, 1) = "1")' }.join(' OR ')
where("NOT (#{condition})", *zone_ids)
}

View file

@ -40,12 +40,12 @@ class Item
Filter.name_excludes(value, locale))
when 'occupies'
filters << (is_positive ?
Filter.occupies(value, locale) :
Filter.not_occupies(value, locale))
Filter.occupies(value) :
Filter.not_occupies(value))
when 'restricts'
filters << (is_positive ?
Filter.restricts(value, locale) :
Filter.not_restricts(value, locale))
Filter.restricts(value) :
Filter.not_restricts(value))
when 'fits'
color_name, species_name = value.split('-')
begin
@ -196,20 +196,20 @@ class Item
self.new Item.name_excludes(value, locale), "-#{q value}"
end
def self.occupies(value, locale)
self.new Item.occupies(value, locale), "occupies:#{q value}"
def self.occupies(value)
self.new Item.occupies(value), "occupies:#{q value}"
end
def self.not_occupies(value, locale)
self.new Item.not_occupies(value, locale), "-occupies:#{q value}"
def self.not_occupies(value)
self.new Item.not_occupies(value), "-occupies:#{q value}"
end
def self.restricts(value, locale)
self.new Item.restricts(value, locale), "restricts:#{q value}"
def self.restricts(value)
self.new Item.restricts(value), "restricts:#{q value}"
end
def self.not_restricts(value, locale)
self.new Item.not_restricts(value, locale), "-restricts:#{q value}"
def self.not_restricts(value)
self.new Item.not_restricts(value), "-restricts:#{q value}"
end
def self.fits(body_id, color_name, species_name)

View file

@ -1,23 +1,36 @@
class Zone < ActiveRecord::Base
translates :label, :plain_label
translates # TODO: Remove once we're all done with translations!
# 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 :alphabetical, -> { order(:label) }
scope :matching_label, ->(label) {
where(plain_label: Zone.plainify_label(label))
}
scope :for_items, -> { where(arel_table[:type_id].gt(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 label=(new_label)
globalize.write(:en, :label, new_label)
write_attribute(:label, new_label)
end
# 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 plain_label=(new_plain_label)
globalize.write(:en, :plain_label, new_plain_label)
write_attribute(:plain_label, new_plain_label)
end
def as_json(options={})
super({only: [:id, :depth, :label]}.merge(options))
end

View file

@ -0,0 +1,16 @@
class AddLabelAndPlainLabelToZones < ActiveRecord::Migration[7.1]
def change
add_column :zones, :label, :string, null: false
add_column :zones, :plain_label, :string, null: false
reversible do |direction|
direction.up do
Zone.includes(:translations).find_each do |zone|
zone.label = zone.translation_for(:en).label
zone.plain_label = zone.translation_for(:en).plain_label
zone.save!
end
end
end
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_01_23_125509) do
ActiveRecord::Schema[7.1].define(version: 2024_01_23_133215) do
create_table "auth_servers", id: :integer, charset: "latin1", collation: "latin1_swedish_ci", force: :cascade do |t|
t.string "short_name", limit: 10, null: false
t.string "name", limit: 40, null: false
@ -297,6 +297,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_23_125509) do
create_table "zones", id: :integer, charset: "latin1", collation: "latin1_swedish_ci", force: :cascade do |t|
t.integer "depth"
t.integer "type_id"
t.string "label", null: false
t.string "plain_label", null: false
end
end