Fix export/import losing custom column colors

record.to_json serializes through model accessors. Column::Colored
overrides the color accessor to return a Color struct, which serializes
as {"name":"Blue","value":"var(--color-card-default)"} instead of the
raw CSS string.

Two fixes:
- Export raw DB values via record.attributes.slice(*attributes).to_json
- Color.for_value parses legacy JSON format so columns imported from
  old exports self-heal when read
This commit is contained in:
Donal McBreen
2026-04-03 10:05:11 +01:00
parent 752237abab
commit b79c7897e6
4 changed files with 25 additions and 2 deletions
@@ -65,7 +65,7 @@ class Account::DataTransfer::RecordSet
end
def export_record(record)
zip.add_file "data/#{model_dir}/#{record.id}.json", record.to_json
zip.add_file "data/#{model_dir}/#{record.id}.json", record.attributes.slice(*attributes).to_json
end
def files
+15 -1
View File
@@ -2,9 +2,23 @@ Color = Struct.new(:name, :value)
class Color
class << self
# Finds a Color by its CSS value (e.g. "var(--color-card-4)").
# Falls back to extracting the value from legacy export formats where
# the Color struct was serialized instead of the raw CSS string.
def for_value(value)
COLORS.find { |it| it.value == value }
COLORS.find { |it| it.value == value } ||
extract_from_legacy_export(value)
end
private
# Broken exports serialized Color structs instead of raw CSS values,
# producing JSON like {"name":"Lime","value":"var(--color-card-4)"}.
# Parse it and extract the value.
def extract_from_legacy_export(value)
parsed = value.is_a?(String) && JSON.parse(value)
COLORS.find { |it| it.value == parsed["value"] } if parsed.is_a?(Hash)
rescue JSON::ParserError
end
end
def to_s