simplify CustomStyle#remove_xyz implementation

This commit is contained in:
ulferts
2026-02-16 17:41:44 +01:00
parent e737e3bf65
commit 44b654cecd
5 changed files with 17 additions and 14 deletions
+2 -2
View File
@@ -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
+3 -7
View File
@@ -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
+1 -1
View File
@@ -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
@@ -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
+10 -3
View File
@@ -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