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.
This commit is contained in:
songtianlun 2025-03-22 11:29:54 +08:00
parent c489c19467
commit c27bab7dc9

View File

@ -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