summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-07-21 14:24:48 +0200
committererdgeist <erdgeist@erdgeist.org>2026-07-21 14:24:48 +0200
commit1912b8fbf0e1e1ea84a89c033e20e34235d19cce (patch)
tree1a675ea6cd9c742af9f53506b820761a49c3452e
parent93800f8899fba97c00016301015fe812dec1b6e5 (diff)
Stop related_assets from duplicating on every clone
self.assets = page.assets implicitly cleared the collection before recreating it. Replacing it with an explicit loop, to preserve the headline flag across clones, dropped that clearing -- every clone_attributes_from call since has been appending related_assets rather than replacing them. Restored with an explicit delete_all before the copy. update_assets (the sort_images reorder action) had the same gap for headline specifically; it now carries the previous headline forward when that asset is still present in the new list.
-rw-r--r--app/models/page.rb14
1 files changed, 10 insertions, 4 deletions
diff --git a/app/models/page.rb b/app/models/page.rb
index f33d88d..ab42b1c 100644
--- a/app/models/page.rb
+++ b/app/models/page.rb
@@ -220,7 +220,12 @@ class Page < ApplicationRecord
220 end 220 end
221 221
222 # Clone asset references 222 # Clone asset references
223 self.assets = page.assets 223 self.related_assets.delete_all
224 page.related_assets.each do |related|
225 self.related_assets.create!(:asset_id => related.asset_id,
226 :position => related.position,
227 :headline => related.headline)
228 end
224 229
225 self.save 230 self.save
226 end 231 end
@@ -315,18 +320,19 @@ class Page < ApplicationRecord
315 end 320 end
316 321
317 def update_assets image_ids 322 def update_assets image_ids
318
319 transaction do 323 transaction do
324 previous_headline_asset_id = self.related_assets.find_by(headline: true)&.asset_id
325
320 self.related_assets.delete_all 326 self.related_assets.delete_all
321 327
322 if image_ids 328 if image_ids
323 image_ids.each_with_index do |id, index| 329 image_ids.each_with_index do |id, index|
324 asset = Asset.find(id) 330 asset = Asset.find(id)
325 self.related_assets.create!(:asset_id => asset.id, :position => index+1) 331 self.related_assets.create!(:asset_id => asset.id, :position => index+1,
332 :headline => asset.id == previous_headline_asset_id)
326 end 333 end
327 end 334 end
328 end 335 end
329
330 end 336 end
331 337
332 # Installs (or re-installs) the trigger that keeps page_translations' 338 # Installs (or re-installs) the trigger that keeps page_translations'