Use Rails serialize method to save/load cached fields in Item
Just packing some serialization complexity away into its own thing, so the model code doesn't need to sweat it!
This commit is contained in:
parent
7ba68c52d4
commit
e52838ba70
4 changed files with 28 additions and 5 deletions
|
@ -10,6 +10,9 @@ class Item < ApplicationRecord
|
||||||
|
|
||||||
SwfAssetType = 'object'
|
SwfAssetType = 'object'
|
||||||
|
|
||||||
|
serialize :cached_compatible_body_ids, coder: Serializers::IntegerSet
|
||||||
|
serialize :cached_occupied_zone_ids, coder: Serializers::IntegerSet
|
||||||
|
|
||||||
has_many :closet_hangers
|
has_many :closet_hangers
|
||||||
has_one :contribution, as: :contributed, inverse_of: :contributed
|
has_one :contribution, as: :contributed, inverse_of: :contributed
|
||||||
has_one :nc_mall_record
|
has_one :nc_mall_record
|
||||||
|
@ -258,8 +261,8 @@ class Item < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_cached_fields
|
def update_cached_fields
|
||||||
self.cached_occupied_zone_ids = occupied_zone_ids.sort.join(",")
|
self.cached_occupied_zone_ids = occupied_zone_ids
|
||||||
self.cached_compatible_body_ids = compatible_body_ids.sort.join(",")
|
self.cached_compatible_body_ids = compatible_body_ids
|
||||||
self.save!
|
self.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
class AllowNullInItemsCachedFields < ActiveRecord::Migration[7.2]
|
||||||
|
def change
|
||||||
|
# This is a bit more compatible with ActiveRecord's `serialize` utility,
|
||||||
|
# which seems pretty insistent that empty arrays should be saved as `NULL`,
|
||||||
|
# rather than the empty string our serializer would return if called :(
|
||||||
|
change_column_null :items, :cached_compatible_body_ids, true
|
||||||
|
change_column_null :items, :cached_occupied_zone_ids, true
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[7.2].define(version: 2024_10_01_052510) do
|
ActiveRecord::Schema[7.2].define(version: 2024_10_03_004726) do
|
||||||
create_table "alt_styles", charset: "utf8mb4", collation: "utf8mb4_unicode_520_ci", force: :cascade do |t|
|
create_table "alt_styles", charset: "utf8mb4", collation: "utf8mb4_unicode_520_ci", force: :cascade do |t|
|
||||||
t.integer "species_id", null: false
|
t.integer "species_id", null: false
|
||||||
t.integer "color_id", null: false
|
t.integer "color_id", null: false
|
||||||
|
@ -137,8 +137,8 @@ ActiveRecord::Schema[7.2].define(version: 2024_10_01_052510) do
|
||||||
t.text "description", size: :medium, null: false
|
t.text "description", size: :medium, null: false
|
||||||
t.string "rarity", default: "", null: false
|
t.string "rarity", default: "", null: false
|
||||||
t.integer "dyeworks_base_item_id"
|
t.integer "dyeworks_base_item_id"
|
||||||
t.string "cached_occupied_zone_ids", default: "", null: false
|
t.string "cached_occupied_zone_ids", default: ""
|
||||||
t.text "cached_compatible_body_ids", default: "", null: false
|
t.text "cached_compatible_body_ids", default: ""
|
||||||
t.index ["dyeworks_base_item_id"], name: "index_items_on_dyeworks_base_item_id"
|
t.index ["dyeworks_base_item_id"], name: "index_items_on_dyeworks_base_item_id"
|
||||||
t.index ["modeling_status_hint", "created_at", "id"], name: "items_modeling_status_hint_and_created_at_and_id"
|
t.index ["modeling_status_hint", "created_at", "id"], name: "items_modeling_status_hint_and_created_at_and_id"
|
||||||
t.index ["modeling_status_hint", "created_at"], name: "items_modeling_status_hint_and_created_at"
|
t.index ["modeling_status_hint", "created_at"], name: "items_modeling_status_hint_and_created_at"
|
||||||
|
|
11
lib/serializers/integer_set.rb
Normal file
11
lib/serializers/integer_set.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
module Serializers
|
||||||
|
module IntegerSet
|
||||||
|
def self.dump(array)
|
||||||
|
array.sort.join(",")
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.load(string)
|
||||||
|
(string || "").split(",").map(&:to_i)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue