FIX: Allow non-persisted color-scheme colors to be edited (#20104)

When we introduce new color scheme colors, they are not immediately persisted to the database for all color schemes. Previously, this meant that they would be unavailable in the admin UI for editing. The only way to work with the new colors was to create a new color scheme.

This commit updates the serializer so that all colors are serialized, even if they are not yet persisted to the database for the current scheme. This means that they now show up in the admin UI and can be edited.
This commit is contained in:
David Taylor 2023-01-31 17:10:32 +00:00 committed by GitHub
parent 64986244d7
commit c760efc924
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -11,4 +11,11 @@ class ColorSchemeSerializer < ApplicationSerializer
def theme_id
object.theme&.id
end
def colors
db_colors = object.colors.index_by(&:name)
object.resolved_colors.map do |name, default|
db_colors[name] || ColorSchemeColor.new(name: name, hex: default, color_scheme: object)
end
end
end

View File

@ -31,6 +31,20 @@ RSpec.describe Admin::ColorSchemesController do
expect(scheme_colors[0]["name"]).to eq(base_scheme_colors[0].name)
expect(scheme_colors[0]["hex"]).to eq(base_scheme_colors[0].hex)
end
it "serializes default colors even when not present in database" do
scheme = ColorScheme.create_from_base({ name: "my color scheme" })
scheme.colors.find_by(name: "primary").destroy!
scheme_name = scheme.name
get "/admin/color_schemes.json"
expect(response.status).to eq(200)
serialized_scheme = response.parsed_body.find { |s| s["name"] == "my color scheme" }
scheme_colors = serialized_scheme["colors"]
expect(scheme_colors[0]["name"]).to eq("primary")
expect(scheme_colors[0]["hex"]).to eq(scheme.resolved_colors["primary"])
end
end
shared_examples "color schemes inaccessible" do