Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Allow editing theme-owned palettes (#34722)

Osama Sayegha7fb9e189725
diff --git a/app/assets/javascripts/admin/addon/components/admin-config-areas/color-palette.gjs b/app/assets/javascripts/admin/addon/components/admin-config-areas/color-palette.gjs
index 51bc6100302..b6834b10c19 100644
--- a/app/assets/javascripts/admin/addon/components/admin-config-areas/color-palette.gjs
+++ b/app/assets/javascripts/admin/addon/components/admin-config-areas/color-palette.gjs
@@ -414,7 +414,6 @@ export default class AdminConfigAreasColorPalette extends Component {
>
<field.Custom>
<ColorPaletteEditor
- @disabled={{this.installedWithTheme}}
@colors={{transientData.colors}}
@onColorChange={{this.onColorChange}}
/>
diff --git a/app/assets/javascripts/admin/addon/components/color-palette-list-item.gjs b/app/assets/javascripts/admin/addon/components/color-palette-list-item.gjs
index dce6f343768..04683c84d3d 100644
--- a/app/assets/javascripts/admin/addon/components/color-palette-list-item.gjs
+++ b/app/assets/javascripts/admin/addon/components/color-palette-list-item.gjs
@@ -32,12 +32,6 @@ export default class ColorPaletteListItem extends Component {
return this.args.scheme?.theme_id;
}
- get editButtonLabel() {
- return this.isThemePalette && !this.isBuiltInDefault
- ? "admin.customize.colors.view"
- : "admin.customize.colors.edit";
- }
-
get canDelete() {
return !this.isBuiltInDefault && !this.args.scheme?.theme_id;
}
@@ -177,7 +171,7 @@ export default class ColorPaletteListItem extends Component {
<DButton
@route="adminConfig.colorPalettes.show"
@routeModels={{array @scheme.id}}
- @label={{this.editButtonLabel}}
+ @label="admin.customize.colors.edit"
class="btn-secondary"
@disabled={{not this.canEdit}}
/>
diff --git a/app/assets/javascripts/admin/addon/models/color-scheme.js b/app/assets/javascripts/admin/addon/models/color-scheme.js
index 6c615a31520..7ed9a059466 100644
--- a/app/assets/javascripts/admin/addon/models/color-scheme.js
+++ b/app/assets/javascripts/admin/addon/models/color-scheme.js
@@ -179,6 +179,7 @@ export default class ColorScheme extends EmberObject {
if (result.id) {
this.set("id", result.id);
}
+ this.set("base_scheme_id", result.base_scheme_id);
if (!opts?.saveNameOnly) {
if (!opts || !opts.enabledOnly) {
diff --git a/app/controllers/admin/color_schemes_controller.rb b/app/controllers/admin/color_schemes_controller.rb
index f04d78936df..e0ebe99d3fc 100644
--- a/app/controllers/admin/color_schemes_controller.rb
+++ b/app/controllers/admin/color_schemes_controller.rb
@@ -23,7 +23,6 @@ class Admin::ColorSchemesController < Admin::AdminController
if @color_scheme.theme_id.present?
if (update_params.key?(:name) && update_params[:name] != @color_scheme.name) ||
- update_params[:colors].present? ||
(
update_params.key?(:base_scheme_id) &&
update_params[:base_scheme_id] != @color_scheme.base_scheme_id
diff --git a/app/models/color_scheme.rb b/app/models/color_scheme.rb
index 9040f7755ab..4a5c14aa55f 100644
--- a/app/models/color_scheme.rb
+++ b/app/models/color_scheme.rb
@@ -331,6 +331,8 @@ class ColorScheme < ActiveRecord::Base
@hex_cache ||= DistributedCache.new("scheme_hex_for_name")
end
+ default_scope { where(remote_copy: false) }
+
attr_accessor :is_base
attr_accessor :skip_publish
attr_accessor :is_builtin_default
@@ -343,9 +345,11 @@ class ColorScheme < ActiveRecord::Base
after_save_commit :publish_discourse_stylesheet, unless: :skip_publish
after_save_commit :dump_caches
after_destroy :dump_caches
+ after_destroy :destroy_remote_original
belongs_to :theme
- belongs_to :base_scheme, class_name: "ColorScheme"
+ belongs_to :base_scheme, -> { unscope(where: :remote_copy) }, class_name: "ColorScheme"
+ validate :no_edits_for_remote_copies, on: :update
validates_associated :color_scheme_colors
BASE_COLORS_FILE = "#{Rails.root}/app/assets/stylesheets/common/foundation/colors.scss"
@@ -572,6 +576,54 @@ class ColorScheme < ActiveRecord::Base
def is_wcag?
base_scheme_id == NAMES_TO_ID_MAP["WCAG"] || base_scheme_id == NAMES_TO_ID_MAP["WCAG Dark"]
end
+
+ def diverge_from_remote
+ new_scheme = dup
+ new_scheme.colors = self.colors_hashes
+ new_scheme.via_wizard = false
+ new_scheme.user_selectable = false
+ new_scheme.base_scheme_id = nil
+ new_scheme.skip_publish = true
+ new_scheme.remote_copy = true
+
+ DistributedMutex.synchronize("color_scheme_diverge_from_remote_#{self.id}") do
+ self.reload
+ if self.base_scheme.blank?
+ self.transaction do
+ new_scheme.save!
+ self.base_scheme_id = new_scheme.id
+ self.save!
+ end
+ end
+ end
+
+ self
+ end
+
+ private
+
+ def destroy_remote_original
+ return if theme_id.blank?
+ return if base_scheme_id.blank? || base_scheme_id < 0
+
+ ColorScheme
+ .unscoped
+ .where(theme_id: theme_id, id: base_scheme_id, remote_copy: true)
+ .destroy_all
+ end
+
+ def no_edits_for_remote_copies
+ if (will_save_change_to_remote_copy? && remote_copy_was) ||
+ (
+ remote_copy &&
+ (
+ will_save_change_to_base_scheme_id? || will_save_change_to_user_selectable? ||
+ will_save_change_to_theme_id?
+ )
+ )
+ errors.add(:base, I18n.t("color_schemes.errors.cannot_edit_remote_copies"))
+ end
+ end
end
# == Schema Information
@@ -580,6 +632,7 @@ end
#
# id :integer not null, primary key
# name :string not null
+# remote_copy :boolean default(FALSE), not null
# user_selectable :boolean default(FALSE), not null
# version :integer default(1), not null
# via_wizard :boolean default(FALSE), not null
diff --git a/app/models/color_scheme_color.rb b/app/models/color_scheme_color.rb
index 91194d815a0..09af19f367c 100644
--- a/app/models/color_scheme_color.rb
+++ b/app/models/color_scheme_color.rb
@@ -5,7 +5,7 @@ class ColorSchemeColor < ActiveRecord::Base
"dark_hex", # TODO: Remove when 20250821155127_drop_dark_hex_from_color_scheme_color has been promoted to pre-deploy
]
- belongs_to :color_scheme
+ belongs_to :color_scheme, -> { unscope(where: :remote_copy) }
validates :hex, format: { with: /\A([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\z/ }
diff --git a/app/models/remote_theme.rb b/app/models/remote_theme.rb
index 435ddd37416..bdd9e1b53d7 100644
--- a/app/models/remote_theme.rb
+++ b/app/models/remote_theme.rb
@@ -417,12 +417,21 @@ class RemoteTheme < ActiveRecord::Base
end
def update_theme_color_schemes(theme, schemes)
- missing_scheme_names = Hash[*theme.color_schemes.pluck(:name, :id).flatten]
+ existing_schemes = ColorScheme.unscoped.where(theme_id: theme.id)
+
+ missing_scheme_names =
+ existing_schemes.reduce({}) do |hash, cs|
+ hash[cs.name] = cs if !cs.remote_copy
+ hash
+ end
+
ordered_schemes = []
schemes&.each do |name, colors|
missing_scheme_names.delete(name)
- scheme = theme.color_schemes.find_by(name: name) || theme.color_schemes.build(name: name)
+ scheme = existing_schemes.find { |cs| cs.name == name && cs.remote_copy }
+ scheme ||= existing_schemes.find { |cs| cs.name == name }
+ scheme ||= theme.color_schemes.build(name: name)
# Update main colors
ColorScheme.base.colors_hashes.each do |color|
@@ -452,8 +461,16 @@ class RemoteTheme < ActiveRecord::Base
end
if missing_scheme_names.length > 0
- ColorScheme.where(id: missing_scheme_names.values).delete_all
- # we may have stuff pointed at the incorrect scheme?
+ to_be_deleted_ids = []
+ missing_scheme_names.values.each do |cs|
+ if (base = existing_schemes.find { |s| s.id == cs.base_scheme_id && s.remote_copy })
+ to_be_deleted_ids << cs.base_scheme_id
+ else
+ to_be_deleted_ids << cs.id
+ end
+ end
+
+ ColorScheme.unscoped.where(id: to_be_deleted_ids).destroy_all
end
theme.color_scheme = ordered_schemes.first if theme.new_record?
diff --git a/app/serializers/color_scheme_color_serializer.rb b/app/serializers/color_scheme_color_serializer.rb
index 9586481504a..63b468892b4 100644
--- a/app/serializers/color_scheme_color_serializer.rb
+++ b/app/serializers/color_scheme_color_serializer.rb
@@ -9,7 +9,7 @@ class ColorSchemeColorSerializer < ApplicationSerializer
def default_hex
# return the hex value of the color when it is already a base color or no base_scheme is set
- if !object.color_scheme || object.color_scheme.base_scheme_id == 0
+ if !object.color_scheme || !object.color_scheme.base_scheme_id
object.hex
else
object.color_scheme.base_colors[object.name]
diff --git a/app/services/color_scheme_revisor.rb b/app/services/color_scheme_revisor.rb
index b1953f39337..91696ae74af 100644
--- a/app/services/color_scheme_revisor.rb
+++ b/app/services/color_scheme_revisor.rb
@@ -10,11 +10,7 @@ class ColorSchemeRevisor
self.new(color_scheme, params).revise
end
- def self.revise_existing_colors_only(color_scheme, params)
- self.new(color_scheme, params).revise(update_existing_colors_only: true)
- end
-
- def revise(update_existing_colors_only: false)
+ def revise
ColorScheme.transaction do
@color_scheme.name = @params[:name] if @params.has_key?(:name)
@color_scheme.user_selectable = @params[:user_selectable] if @params.has_key?(
@@ -24,11 +20,16 @@ class ColorSchemeRevisor
@color_scheme.base_scheme_id = @params[:base_scheme_id] if @params.has_key?(:base_scheme_id)
has_colors = @params[:colors]
+ if @params[:colors].present? && @color_scheme.theme_id.present? &&
+ @color_scheme.base_scheme_id.blank?
+ @color_scheme.diverge_from_remote
+ end
+
if has_colors
@params[:colors].each do |c|
if existing = @color_scheme.colors_by_name[c[:name]]
existing.update(c)
- elsif !update_existing_colors_only
+ else
@color_scheme.color_scheme_colors << ColorSchemeColor.new(name: c[:name], hex: c[:hex])
end
end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index be3c31d0bc8..b53c739da30 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -6898,7 +6898,7 @@ en:
copy_to_clipboard: "Copy to Clipboard"
copied_to_clipboard: "Copied to Clipboard"
copy_to_clipboard_error: "Error copying data to Clipboard"
- theme_owner: "Not editable, owned by:"
+ theme_owner: "Owned by:"
email_templates:
title: "Email"
subject: "Subject"
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index ddf25b4c1b7..45713d3c7f2 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -4806,6 +4806,8 @@ en:
latte_theme_name: "Latte"
summer_theme_name: "Summer"
dark_rose_theme_name: "Dark Rose"
+ errors:
+ cannot_edit_remote_copies: "Edits to remote copies of color palettes are not allowed."
edit_this_page: "Edit this page"
diff --git a/db/migrate/20250902221035_add_remote_copy_to_color_scheme.rb b/db/migrate/20250902221035_add_remote_copy_to_color_scheme.rb
new file mode 100644
index 00000000000..edbb8516b19
--- /dev/null
+++ b/db/migrate/20250902221035_add_remote_copy_to_color_scheme.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class AddRemoteCopyToColorScheme < ActiveRecord::Migration[8.0]
+ def change
+ add_column :color_schemes, :remote_copy, :boolean, default: false, null: false
+ end
+end
diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb
index c8ac636dff3..a352cf9adae 100644
--- a/spec/helpers/application_helper_spec.rb
+++ b/spec/helpers/application_helper_spec.rb
@@ -991,7 +991,7 @@ RSpec.describe ApplicationHelper do
it "renders a 'light' color-scheme if no dark scheme is set and the current scheme is light" do
ColorSchemeRevisor.revise(
color_scheme,
- colors: [{ name: "primary", hex: "333333" }, { name: "secondary", hex: "DDDDDD" }],
+ { colors: [{ name: "primary", hex: "333333" }, { name: "secondary", hex: "DDDDDD" }] },
)
helper.request.cookies["color_scheme_id"] = color_scheme.id
@@ -1004,7 +1004,7 @@ RSpec.describe ApplicationHelper do
it "renders a 'dark' color-scheme if no dark scheme is set and the default scheme is dark" do
ColorSchemeRevisor.revise(
color_scheme,
- colors: [{ name: "primary", hex: "F8F8F8" }, { name: "secondary", hex: "232323" }],
+ { colors: [{ name: "primary", hex: "F8F8F8" }, { name: "secondary", hex: "232323" }] },
)
@scheme_id = color_scheme.id
diff --git a/spec/integration/remote_theme_color_schemes_spec.rb b/spec/integration/remote_theme_color_schemes_spec.rb
new file mode 100644
index 00000000000..4f3c13680fe
--- /dev/null
+++ b/spec/integration/remote_theme_color_schemes_spec.rb
@@ -0,0 +1,152 @@
+# frozen_string_literal: true
+
+RSpec.describe "Remote theme update" do
+ let :about_json do
+ <<~JSON
+ {
+ "name": "awesome theme",
+ "about_url": "https://www.site.com/about",
+ "license_url": "https://www.site.com/license",
+ "theme_version": "1.0",
+ "minimum_discourse_version": "1.0.0",
+ "assets": {
+ "font": "assets/font.woff2"
+ },
+ "color_schemes": {
+ "Amazing": {
+ "love": "FAFAFA",
+ "tertiary-low": "FFFFFF"
+ }
+ }
+ }
+ JSON
+ end
+
+ let :initial_repo do
+ setup_git_repo("about.json" => about_json)
+ end
+
+ let :initial_repo_url do
+ MockGitImporter.register("https://example.com/initial_repo.git", initial_repo)
+ end
+
+ after { `rm -fr #{initial_repo}` }
+
+ around(:each) { |group| MockGitImporter.with_mock { group.run } }
+
+ it "updates the base schemes for schemes that have diverged colors" do
+ add_to_git_repo(
+ initial_repo,
+ "about.json" =>
+ JSON
+ .parse(about_json)
+ .merge(
+ color_schemes: {
+ scheme1: {
+ love: "FFFFFF",
+ tertiary_low: "000000",
+ },
+ scheme2: {
+ love: "AACCDD",
+ tertiary_low: "99AAFF",
+ },
+ },
+ )
+ .to_json,
+ )
+
+ theme = RemoteTheme.import_theme(initial_repo_url)
+ scheme1 = theme.color_schemes.find_by(name: "scheme1")
+ scheme2 = theme.color_schemes.find_by(name: "scheme2")
+ expect(scheme1.base_scheme_id).to eq(nil)
+ expect(scheme2.base_scheme_id).to eq(nil)
+
+ ColorSchemeRevisor.revise(scheme1, { colors: [{ name: "love", hex: "111111" }] })
+
+ scheme1_base_scheme_id = scheme1.reload.base_scheme_id
+ expect(scheme1_base_scheme_id).to be_present
+ expect(scheme1.colors.find_by(name: "love").hex).to eq("111111")
+ expect(
+ ColorScheme.unscoped.find(scheme1_base_scheme_id).colors.find_by(name: "love").hex,
+ ).to eq("ffffff")
+ expect(scheme2.base_scheme_id).to eq(nil)
+
+ add_to_git_repo(
+ initial_repo,
+ "about.json" =>
+ JSON
+ .parse(about_json)
+ .merge(
+ color_schemes: {
+ scheme1: {
+ love: "EEEEEE",
+ tertiary_low: "000000",
+ },
+ scheme2: {
+ love: "AACCDD",
+ tertiary_low: "99AAFF",
+ },
+ },
+ )
+ .to_json,
+ )
+ theme.remote_theme.update_from_remote
+
+ scheme1.reload
+ expect(scheme1.base_scheme_id).to eq(scheme1_base_scheme_id)
+ expect(scheme1.colors.find_by(name: "love").hex).to eq("111111")
+ expect(
+ ColorScheme.unscoped.find(scheme1_base_scheme_id).colors.find_by(name: "love").hex,
+ ).to eq("eeeeee")
+ expect(scheme2.base_scheme_id).to eq(nil)
+ end
+
+ it "deletes color schemes that haven't been modified and deletes the bases of schemes that have been modified" do
+ add_to_git_repo(
+ initial_repo,
+ "about.json" =>
+ JSON
+ .parse(about_json)
+ .merge(
+ color_schemes: {
+ scheme1: {
+ love: "FFFFFF",
+ tertiary_low: "000000",
+ },
+ scheme2: {
+ love: "AACCDD",
+ tertiary_low: "99AAFF",
+ },
+ },
+ )
+ .to_json,
+ )
+
+ theme = RemoteTheme.import_theme(initial_repo_url)
+
+ scheme1 = theme.color_schemes.find_by(name: "scheme1")
+ scheme2 = theme.color_schemes.find_by(name: "scheme2")
+
+ expect do
+ ColorSchemeRevisor.revise(scheme1, { colors: [{ name: "love", hex: "111111" }] })
+ end.to change { ColorScheme.unscoped.count }.by(1)
+
+ expect(scheme1.reload.base_scheme_id).to be_present
+ expect(scheme2.reload.base_scheme_id).to be_blank
+
+ scheme1_base_scheme_id = scheme1.base_scheme_id
+
+ add_to_git_repo(
+ initial_repo,
+ "about.json" => JSON.parse(about_json).merge(color_schemes: {}).to_json,
+ )
+
+ expect do theme.remote_theme.update_from_remote end.to change { ColorScheme.unscoped.count }.by(
+ -2,
+ )
+
+ expect(ColorScheme.unscoped.exists?(id: scheme1_base_scheme_id)).to eq(false)
+ expect(ColorScheme.unscoped.exists?(id: scheme1.id)).to eq(true)
+ expect(ColorScheme.unscoped.exists?(id: scheme2.id)).to eq(false)
+ end
+end
diff --git a/spec/lib/stylesheet/manager_spec.rb b/spec/lib/stylesheet/manager_spec.rb
index 442a0881d16..e90caf34a04 100644
--- a/spec/lib/stylesheet/manager_spec.rb
+++ b/spec/lib/stylesheet/manager_spec.rb
@@ -508,7 +508,7 @@ RSpec.describe Stylesheet::Manager do
digest1 = builder.color_scheme_digest
- ColorSchemeRevisor.revise(scheme, colors: [{ name: "primary", hex: "CC0000" }])
+ ColorSchemeRevisor.revise(scheme, { colors: [{ name: "primary", hex: "CC0000" }] })
digest2 = builder.color_scheme_digest
@@ -652,7 +652,7 @@ RSpec.describe Stylesheet::Manager do
it "uses the correct color scheme when a non-default theme is selected and it uses the base 'Light' scheme" do
cs = Fabricate(:color_scheme, name: "Not This")
- ColorSchemeRevisor.revise(cs, colors: [{ name: "primary", hex: "CC0000" }])
+ ColorSchemeRevisor.revise(cs, { colors: [{ name: "primary", hex: "CC0000" }] })
default_theme = Fabricate(:theme, color_scheme_id: cs.id)
SiteSetting.default_theme_id = default_theme.id
@@ -706,7 +706,7 @@ RSpec.describe Stylesheet::Manager do
)
stylesheet = builder.compile
- ColorSchemeRevisor.revise(scheme, colors: [{ name: "primary", hex: "CC0000" }])
+ ColorSchemeRevisor.revise(scheme, { colors: [{ name: "primary", hex: "CC0000" }] })
builder2 =
Stylesheet::Manager::Builder.new(
@@ -833,7 +833,7 @@ RSpec.describe Stylesheet::Manager do
name: "Neutral",
base_scheme_id: ColorScheme::NAMES_TO_ID_MAP["Neutral"],
)
- ColorSchemeRevisor.revise(custom_scheme, colors: [{ name: "primary", hex: "CC0000" }])
+ ColorSchemeRevisor.revise(custom_scheme, { colors: [{ name: "primary", hex: "CC0000" }] })
theme.color_scheme_id = custom_scheme.id
theme.save!
diff --git a/spec/models/color_scheme_spec.rb b/spec/models/color_scheme_spec.rb
index 82335ca4822..96e66812249 100644
--- a/spec/models/color_scheme_spec.rb
+++ b/spec/models/color_scheme_spec.rb
@@ -22,7 +22,7 @@ RSpec.describe ColorScheme do
href = manager.stylesheet_data(:desktop_theme)[0][:new_href]
colors_href = manager.color_scheme_stylesheet_details(scheme.id, fallback_to_base: true)
- ColorSchemeRevisor.revise(scheme, colors: [{ name: "primary", hex: "bbb" }])
+ ColorSchemeRevisor.revise(scheme, { colors: [{ name: "primary", hex: "bbb" }] })
href2 = manager.stylesheet_data(:desktop_theme)[0][:new_href]
colors_href2 = manager.color_scheme_stylesheet_details(scheme.id, fallback_to_base: true)
@@ -87,7 +87,7 @@ RSpec.describe ColorScheme do
scheme = ColorScheme.create_from_base(name: "test scheme")
ColorSchemeRevisor.revise(
scheme,
- colors: [{ name: "header_background", hex: "9dc927", default_hex: "949493" }],
+ { colors: [{ name: "header_background", hex: "9dc927", default_hex: "949493" }] },
)
scheme.reload
expect(ColorScheme.hex_for_name("header_background", scheme.id)).to eq("9dc927")
@@ -104,13 +104,13 @@ RSpec.describe ColorScheme do
scheme = ColorScheme.create_from_base(name: "Tester")
ColorSchemeRevisor.revise(
scheme,
- colors: [{ name: "primary", hex: "333333" }, { name: "secondary", hex: "DDDDDD" }],
+ { colors: [{ name: "primary", hex: "333333" }, { name: "secondary", hex: "DDDDDD" }] },
)
expect(scheme.is_dark?).to eq(false)
ColorSchemeRevisor.revise(
scheme,
- colors: [{ name: "primary", hex: "F8F8F8" }, { name: "secondary", hex: "232323" }],
+ { colors: [{ name: "primary", hex: "F8F8F8" }, { name: "secondary", hex: "232323" }] },
)
expect(scheme.is_dark?).to eq(true)
end
@@ -190,4 +190,106 @@ RSpec.describe ColorScheme do
expect(resolved["selected"]).to eq("e9e9e9")
end
end
+
+ describe "#diverge_from_remote" do
+ fab!(:theme)
+ fab!(:original_scheme) do
+ Fabricate(
+ :color_scheme,
+ name: "somescheme",
+ theme_id: theme.id,
+ user_selectable: true,
+ color_scheme_colors: [
+ Fabricate(:color_scheme_color, name: "primary", hex: "998877"),
+ Fabricate(:color_scheme_color, name: "secondary", hex: "553322"),
+ ],
+ )
+ end
+
+ it "creates a new scheme with the same colors and sets it as the base scheme" do
+ expect(original_scheme.base_scheme_id).to eq(nil)
+
+ original_scheme.diverge_from_remote
+
+ expect(original_scheme.base_scheme.colors.map { |c| [c.name, c.hex] }.sort_by(&:first)).to eq(
+ original_scheme.colors.map { |c| [c.name, c.hex] }.sort_by(&:first),
+ )
+ expect(original_scheme.base_scheme.theme_id).to eq(theme.id)
+ expect(original_scheme.base_scheme.user_selectable).to eq(false)
+ expect(original_scheme.base_scheme.remote_copy).to eq(true)
+ expect(original_scheme.base_scheme.via_wizard).to eq(false)
+ end
+
+ it "doesn't create multiple base schemes if called multiple times" do
+ expect do
+ original_scheme.diverge_from_remote
+ first_base_id = original_scheme.reload.base_scheme.id
+
+ original_scheme.diverge_from_remote
+ expect(original_scheme.reload.base_scheme.id).to eq(first_base_id)
+ end.to change { ColorScheme.unscoped.count }.by(1)
+ end
+ end
+
+ describe "#destroy_remote_original" do
+ fab!(:theme)
+
+ fab!(:original_scheme) { Fabricate(:color_scheme, name: "somescheme", theme_id: theme.id) }
+
+ fab!(:unrelated_scheme) do
+ Fabricate(:color_scheme, theme_id: theme.id, base_scheme_id: original_scheme.id)
+ end
+
+ before { original_scheme.diverge_from_remote }
+
+ it "deletes the base scheme that stores the original colors and is triggered on destroy" do
+ expect(ColorScheme.unscoped.exists?(id: original_scheme.base_scheme_id)).to eq(true)
+
+ expect do original_scheme.destroy! end.to change { ColorScheme.unscoped.count }.by(-2)
+
+ expect(ColorScheme.unscoped.exists?(id: original_scheme.base_scheme_id)).to eq(false)
+ end
+ end
+
+ describe "#no_edits_for_remote_copies" do
+ it "prevents editing remote copies of color schemes" do
+ remote_copy =
+ Fabricate(
+ :color_scheme,
+ remote_copy: true,
+ user_selectable: false,
+ theme_id: Fabricate(:theme).id,
+ )
+
+ remote_copy.user_selectable = true
+ expect(remote_copy.valid?).to eq(false)
+ expect(remote_copy.errors.full_messages).to include(
+ I18n.t("color_schemes.errors.cannot_edit_remote_copies"),
+ )
+
+ remote_copy.reload
+
+ remote_copy.base_scheme_id = 1
+ expect(remote_copy.valid?).to eq(false)
+ expect(remote_copy.errors.full_messages).to include(
+ I18n.t("color_schemes.errors.cannot_edit_remote_copies"),
+ )
+
+ remote_copy.reload
+
+ remote_copy.remote_copy = false
+ expect(remote_copy.valid?).to eq(false)
+ expect(remote_copy.errors.full_messages).to include(
+ I18n.t("color_schemes.errors.cannot_edit_remote_copies"),
+ )
+
+ remote_copy.reload
+
+ remote_copy.theme_id = 1
+ expect(remote_copy.valid?).to eq(false)
+ expect(remote_copy.errors.full_messages).to include(
+ I18n.t("color_schemes.errors.cannot_edit_remote_copies"),
+ )
+ end
+ end
end
diff --git a/spec/requests/admin/color_schemes_controller_spec.rb b/spec/requests/admin/color_schemes_controller_spec.rb
index d389b8ee7c9..cf35e4fe3b6 100644
--- a/spec/requests/admin/color_schemes_controller_spec.rb
+++ b/spec/requests/admin/color_schemes_controller_spec.rb
@@ -188,37 +188,109 @@ RSpec.describe Admin::ColorSchemesController do
expect(default_theme.dark_color_scheme_id).to eq(existing.id)
end
- it "doesn't allow editing the name or colors of a theme-owned palette" do
- existing.update!(theme_id: theme.id)
+ it "creates a copy of the scheme with the original colors when editing colors of a theme-owned scheme" do
+ existing.update!(theme_id: theme.id, base_scheme_id: nil)
+ existing.colors.destroy_all
+ existing.colors = [{ name: "primary", hex: "CCBB11" }, { name: "secondary", hex: "9900AA" }]
+ existing.save!
+
+ expect do
+ put "/admin/color_schemes/#{existing.id}.json",
+ params: {
+ color_scheme: {
+ colors: [{ name: "primary", hex: "7711EE" }],
+ },
+ }
+ end.to change { ColorScheme.unscoped.where(remote_copy: true).count }.by(1)
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["colors"].find { |c| c["name"] == "primary" }["hex"]).to eq(
+ "7711EE",
+ )
+ expect(
+ response.parsed_body["colors"].find { |c| c["name"] == "primary" }["default_hex"],
+ ).to eq("CCBB11")
+
+ expect(existing.reload.base_scheme_id).to be_present
+
+ base_scheme = ColorScheme.unscoped.find_by(id: existing.base_scheme_id)
+ expect(base_scheme.colors.find_by(name: "primary").hex).to eq("CCBB11")
+ expect(base_scheme.colors.find_by(name: "secondary").hex).to eq("9900AA")
+
+ expect(existing.colors.find_by(name: "primary").hex).to eq("7711EE")
+ expect(existing.colors.find_by(name: "secondary").hex).to eq("9900AA")
+
+ expect do
+ put "/admin/color_schemes/#{existing.id}.json",
+ params: {
+ color_scheme: {
+ colors: [{ name: "primary", hex: "2200FF" }],
+ },
+ }
+ end.not_to change { ColorScheme.unscoped.count }
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["colors"].find { |c| c["name"] == "primary" }["hex"]).to eq(
+ "2200FF",
+ )
+ expect(
+ response.parsed_body["colors"].find { |c| c["name"] == "primary" }["default_hex"],
+ ).to eq("CCBB11")
+
+ expect(base_scheme.colors.find_by(name: "primary").hex).to eq("CCBB11")
+ expect(base_scheme.colors.find_by(name: "secondary").hex).to eq("9900AA")
+
+ expect(existing.colors.find_by(name: "primary").hex).to eq("2200FF")
+ expect(existing.colors.find_by(name: "secondary").hex).to eq("9900AA")
+ end
- put "/admin/color_schemes/#{existing.id}.json", params: valid_params
+ it "doesn't create a copy of the scheme when editing the user-selectable status of a theme-owned scheme" do
+ existing.update!(theme_id: theme.id, user_selectable: false, base_scheme_id: nil)
- expect(response.status).to eq(403)
+ expect do
+ put "/admin/color_schemes/#{existing.id}.json",
+ params: {
+ color_scheme: {
+ user_selectable: true,
+ },
+ }
+ end.not_to change { ColorScheme.unscoped.count }
+
+ expect(response.status).to eq(200)
+ expect(existing.reload.user_selectable).to eq(true)
end
- it "allows making a theme-owned palette user selectable" do
- existing.update!(theme_id: theme.id, user_selectable: false)
+ it "doesn't allow editing the name or base scheme of a theme-owned scheme" do
+ existing.update!(theme_id: theme.id, name: "original name", base_scheme_id: nil)
+ put "/admin/color_schemes/#{existing.id}.json",
+ params: {
+ color_scheme: {
+ name: "A new name",
+ },
+ }
+ expect(response.status).to eq(403)
put "/admin/color_schemes/#{existing.id}.json",
params: {
color_scheme: {
- user_selectable: true,
+ base_scheme_id: Fabricate(:color_scheme).id,
},
}
+ expect(response.status).to eq(403)
- expect(response.status).to eq(200)
- expect(existing.reload.user_selectable).to eq(true)
+ expect(existing.reload.name).to eq("original name")
+ expect(existing.base_scheme_id).to be_nil
end
it "allows making a theme-owned palette the default theme's palette" do
existing.update!(theme_id: theme.id)
- put "/admin/color_schemes/#{existing.id}.json",
- params: {
- color_scheme: {
- default_light_on_theme: true,
- },
- }
+ expect do
+ put "/admin/color_schemes/#{existing.id}.json",
+ params: {
+ color_scheme: {
+ default_light_on_theme: true,
+ },
+ }
+ end.not_to change { ColorScheme.unscoped.count }
expect(response.status).to eq(200)
expect(Theme.find_default.reload.color_scheme_id).to eq(existing.id)