A platform for community discussion. Free, open, simple.
FIX: Only unlink components from the updated theme when syncing
Régis Hanol036953d95d23
diff --git a/app/models/remote_theme.rb b/app/models/remote_theme.rbindex 1061b7a695c..573cac0f117 100644--- a/app/models/remote_theme.rb+++ b/app/models/remote_theme.rb@@ -126,15 +126,14 @@ class RemoteTheme < ActiveRecord::Baseendtheme.component = theme_info["component"].to_s == "true"- theme.child_components = child_components = theme_info["components"].presence || []+ theme.child_components = theme_info["components"].presence || []theme.skip_child_components_update = true if update_components == "none"+ update_children = existing && update_components.present? && update_components != "none"remote_theme = newremote_theme.theme = themeremote_theme.remote_url = ""- do_update_child_components = false-theme.transaction doremote_theme.update_from_remote(importer,@@ -145,28 +144,17 @@ class RemoteTheme < ActiveRecord::Basebefore_save: before_save,)- if existing && update_components.present? && update_components != "none"- child_components = child_components.map { |url| ThemeStore::GitImporter.new(url.strip).url }-- if update_components == "sync"- ChildTheme- .joins(child_theme: :remote_theme)- .where.not(remote_themes: { remote_url: child_components })- .delete_all- end-- child_components -=- theme- .child_themes- .joins(:remote_theme)- .where("remote_themes.remote_url IN (?)", child_components)- .pluck("remote_themes.remote_url")- theme.child_components = child_components- do_update_child_components = true+ if existing && update_components == "sync"+ child_urls = theme.child_components.map { |url| ThemeStore::GitImporter.new(url.strip).url }+ ChildTheme+ .where(parent_theme_id: theme.id)+ .joins(child_theme: :remote_theme)+ .where.not(remote_themes: { remote_url: child_urls })+ .delete_allendend- theme.update_child_components if do_update_child_components+ theme.update_child_components if update_childrenthemeensurebegindiff --git a/app/models/theme.rb b/app/models/theme.rbindex 75e8aa7aada..2cf00c6e66a 100644--- a/app/models/theme.rb+++ b/app/models/theme.rb@@ -200,13 +200,15 @@ class Theme < ActiveRecord::Baseenddef update_child_components- if !component? && child_components.present? && !skip_child_components_update- child_components.each do |url|- url = ThemeStore::GitImporter.new(url.strip).url- theme = RemoteTheme.find_by(remote_url: url)&.theme- theme ||= RemoteTheme.import_theme(url, user)- child_themes << theme- end+ return if component? || child_components.blank? || skip_child_components_update++ linked_urls = child_themes.joins(:remote_theme).pluck("remote_themes.remote_url").to_set+ child_components.each do |url|+ url = ThemeStore::GitImporter.new(url.strip).url+ next if linked_urls.include?(url)+ theme = RemoteTheme.find_by(remote_url: url)&.theme+ theme ||= RemoteTheme.import_theme(url, user)+ child_themes << themeendenddiff --git a/spec/models/remote_theme_spec.rb b/spec/models/remote_theme_spec.rbindex 08c58321d2e..82b638b06b8 100644--- a/spec/models/remote_theme_spec.rb+++ b/spec/models/remote_theme_spec.rb@@ -810,6 +810,72 @@ RSpec.describe RemoteTheme doendend+ describe ".update_zipped_theme" do+ fab!(:theme)++ let(:listed_url) { "https://github.com/discourse/listed.git" }+ let(:unlisted_url) { "https://github.com/discourse/unlisted.git" }+ let!(:listed_component) do+ Fabricate(+ :theme,+ component: true,+ remote_theme: Fabricate(:remote_theme, remote_url: listed_url),+ )+ end+ let!(:unlisted_component) do+ Fabricate(+ :theme,+ component: true,+ remote_theme: Fabricate(:remote_theme, remote_url: unlisted_url),+ )+ end+ let(:new_repo) { setup_git_repo("about.json" => { name: "New", component: true }.to_json) }+ let(:new_url) { MockGitImporter.register("https://github.com/discourse/new.git", new_repo) }+ let(:bundle_dir) { Dir.mktmpdir }++ around(:each) { |example| MockGitImporter.with_mock { example.run } }++ before { theme.update!(child_theme_ids: [listed_component.id, unlisted_component.id]) }++ after { FileUtils.rm_rf([new_repo, bundle_dir]) }++ def upload(update_components:)+ FileUtils.mkdir_p("#{bundle_dir}/bundle")+ File.write(+ "#{bundle_dir}/bundle/about.json",+ { name: theme.name, components: [listed_url, new_url] }.to_json,+ )+ zip = Compression::Zip.new.compress(bundle_dir, "bundle")++ RemoteTheme.update_zipped_theme(zip, "bundle.zip", theme_id: theme.id, update_components:)+ end++ it "links the new components when adding without reimporting the linked ones" do+ expect { upload(update_components: "add") }.to change { Theme.count }.by(1)++ expect(theme.reload.child_themes.map(&:name)).to contain_exactly(+ listed_component.name,+ unlisted_component.name,+ "New",+ )+ end++ it "unlinks the components that are no longer listed from this theme only when syncing" do+ other_theme = Fabricate(:theme, child_theme_ids: [unlisted_component.id])++ upload(update_components: "sync")++ expect(theme.reload.child_themes.map(&:name)).to contain_exactly(listed_component.name, "New")+ expect(other_theme.reload.child_themes).to contain_exactly(unlisted_component)+ end++ it "leaves the linked components untouched when components are set to none" do+ expect { upload(update_components: "none") }.not_to change { Theme.count }++ expect(theme.reload.child_themes).to contain_exactly(listed_component, unlisted_component)+ end+ end+describe ".import_theme_from_directory" dolet(:theme_dir) { "#{Rails.root.join("spec/fixtures/themes/discourse-test-theme")}" }