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:
@@ -65,7 +65,7 @@ class Account::DataTransfer::RecordSet
|
|||||||
end
|
end
|
||||||
|
|
||||||
def export_record(record)
|
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
|
end
|
||||||
|
|
||||||
def files
|
def files
|
||||||
|
|||||||
+15
-1
@@ -2,9 +2,23 @@ Color = Struct.new(:name, :value)
|
|||||||
|
|
||||||
class Color
|
class Color
|
||||||
class << self
|
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)
|
def for_value(value)
|
||||||
COLORS.find { |it| it.value == value }
|
COLORS.find { |it| it.value == value } ||
|
||||||
|
extract_from_legacy_export(value)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
|||||||
@@ -226,6 +226,7 @@ class Account::ImportTest < ActiveSupport::TestCase
|
|||||||
name: account.name,
|
name: account.name,
|
||||||
board_count: Board.where(account: account).count,
|
board_count: Board.where(account: account).count,
|
||||||
column_count: Column.where(account: account).count,
|
column_count: Column.where(account: account).count,
|
||||||
|
column_colors: Column.where(account: account).order(:id).pluck(:color),
|
||||||
card_count: Card.where(account: account).count,
|
card_count: Card.where(account: account).count,
|
||||||
comment_count: Comment.where(account: account).count,
|
comment_count: Comment.where(account: account).count,
|
||||||
tag_count: Tag.where(account: account).count
|
tag_count: Tag.where(account: account).count
|
||||||
|
|||||||
@@ -7,6 +7,14 @@ class Column::ColoredTest < ActiveSupport::TestCase
|
|||||||
assert_equal Column::Colored::DEFAULT_COLOR, column.color
|
assert_equal Column::Colored::DEFAULT_COLOR, column.color
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "reads color from legacy export format" do
|
||||||
|
column = columns(:writebook_triage)
|
||||||
|
# Broken exports serialized Color structs as JSON
|
||||||
|
column.update_column(:color, { "name" => "Lime", "value" => "var(--color-card-4)" }.to_json)
|
||||||
|
|
||||||
|
assert_equal Color.for_value("var(--color-card-4)"), column.reload.color
|
||||||
|
end
|
||||||
|
|
||||||
test "update the column color" do
|
test "update the column color" do
|
||||||
columns(:writebook_triage).update!(color: "var(--color-card-3)")
|
columns(:writebook_triage).update!(color: "var(--color-card-3)")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user