From 44b654cecd506e53510453cfe1c0667f5ad229a6 Mon Sep 17 00:00:00 2001 From: ulferts Date: Mon, 16 Feb 2026 17:41:44 +0100 Subject: [PATCH] simplify CustomStyle#remove_xyz implementation --- app/controllers/custom_styles_controller.rb | 4 ++-- app/models/custom_style.rb | 10 +++------- app/seeders/env_data/custom_design_seeder.rb | 2 +- spec/features/custom_styles/tabs_navigation_spec.rb | 2 +- spec/models/custom_style_spec.rb | 13 ++++++++++--- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/controllers/custom_styles_controller.rb b/app/controllers/custom_styles_controller.rb index e35c1880fd0..301fbca287f 100644 --- a/app/controllers/custom_styles_controller.rb +++ b/app/controllers/custom_styles_controller.rb @@ -249,7 +249,7 @@ class CustomStylesController < ApplicationController def file_download(path_method) @custom_style = CustomStyle.current - if @custom_style && @custom_style.send(path_method) + if @custom_style&.send(path_method) expires_in 1.year, public: true, must_revalidate: false send_file(@custom_style.send(path_method)) else @@ -263,7 +263,7 @@ class CustomStylesController < ApplicationController return render_404 end - @custom_style.send(remove_method) + @custom_style.send("#{remove_method}!") redirect_to custom_style_path, status: :see_other end end diff --git a/app/models/custom_style.rb b/app/models/custom_style.rb index b135658c8e7..ac23d2c8bff 100644 --- a/app/models/custom_style.rb +++ b/app/models/custom_style.rb @@ -70,13 +70,9 @@ class CustomStyle < ApplicationRecord end end - define_method :"remove_#{name}" do - _mounter(name).remove! - write_uploader(name, nil) - - unless new_record? - update_columns(name => nil, updated_at: Time.zone.now) - end + define_method :"remove_#{name}!" do + super() + save! end end end diff --git a/app/seeders/env_data/custom_design_seeder.rb b/app/seeders/env_data/custom_design_seeder.rb index 82bd3fa0720..d0d41302b6c 100644 --- a/app/seeders/env_data/custom_design_seeder.rb +++ b/app/seeders/env_data/custom_design_seeder.rb @@ -54,7 +54,7 @@ module EnvData data = Setting.seed_design[key.to_s] if data.blank? - custom_style.public_send(:"remove_#{key}") + custom_style.public_send(:"remove_#{key}!") elsif data.match?(/^https?:\/\//) seed_remote_url(custom_style, key, data) else diff --git a/spec/features/custom_styles/tabs_navigation_spec.rb b/spec/features/custom_styles/tabs_navigation_spec.rb index 2ab45898a2e..d2304dd356c 100644 --- a/spec/features/custom_styles/tabs_navigation_spec.rb +++ b/spec/features/custom_styles/tabs_navigation_spec.rb @@ -84,7 +84,7 @@ RSpec.describe "Tabs navigation and content switching on the admin/design page" expect(page).to have_current_path custom_style_path(tab: "branding") # remove the logo and redirect to the branding tab - custom_style.send :remove_logo + custom_style.send :remove_logo! expect(File.exist?(file_path)).to be false expect(page).to have_current_path custom_style_path(tab: "branding") end diff --git a/spec/models/custom_style_spec.rb b/spec/models/custom_style_spec.rb index 2101b05d698..b484d0be4f6 100644 --- a/spec/models/custom_style_spec.rb +++ b/spec/models/custom_style_spec.rb @@ -35,17 +35,24 @@ RSpec.describe CustomStyle do let!(:file_path) { custom_style.send(image).file.path } - before do - custom_style.send :"remove_#{image}" - end + subject { custom_style.send :"remove_#{image}!" } it "deletes the file" do + subject + expect(File.exist?(file_path)).to be false end it "clears the file mount column" do + subject + expect(custom_style.reload.send(image).file).to be_nil end + + it "updates the model" do + expect { subject } + .to change(custom_style, :updated_at) + end end describe "#remove_favicon" do