From c27bab7dc9fee3db93f8da057843d21fe07427b8 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Sat, 22 Mar 2025 11:29:54 +0800 Subject: [PATCH] refactor: simplify weather art creation process - Moved weather art creation inside the transaction block - Updated variable names for clarity - Ensured the transaction block manages the creation and attachment of images more clearly This refactor improves the readability and maintainability of the `create_weather_art` method while preserving its original functionality. The changes also ensure that the transaction affects the creation and attachment operations coherently. --- app/workers/generate_weather_art_worker.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/workers/generate_weather_art_worker.rb b/app/workers/generate_weather_art_worker.rb index 3fdb17a..e0f7499 100644 --- a/app/workers/generate_weather_art_worker.rb +++ b/app/workers/generate_weather_art_worker.rb @@ -40,8 +40,8 @@ class GenerateWeatherArtWorker def create_weather_art(weather_data, prompt, image_url) tempfile = nil - ActiveRecord::Base.transaction do - weather_art = city.weather_arts.create!( + weather_art = ActiveRecord::Base.transaction do + w = city.weather_arts.create!( weather_date: Date.today, prompt: prompt, **weather_data @@ -49,12 +49,12 @@ class GenerateWeatherArtWorker tempfile = Down.download(image_url) - weather_art.image.attach( + w.image.attach( io: File.open(tempfile.path), filename: generate_filename, content_type: "image/png" ) - weather_art + w end weather_art.webp_image.processed weather_art.preview_image.processed