Skip to content

Commit

Permalink
Don't attempt to destroy nonexisting attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
elektronaut committed Jun 7, 2024
1 parent 04b3ce4 commit 5aff153
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/models/concerns/pages_core/page_model/attachments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def page_files
super.in_locale(locale)
end

def page_files_attributes=(attrs)
ids = page_files.map(&:id)
super(attrs.reject { |a| a["_destroy"] && ids.exclude?(a["id"]) })
end

def files
page_files
end
Expand Down
15 changes: 10 additions & 5 deletions app/models/concerns/pages_core/page_model/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ module Images

after_save :update_primary_image

accepts_nested_attributes_for :page_images,
reject_if: proc { |a|
a["image_id"].blank?
},
allow_destroy: true
accepts_nested_attributes_for(
:page_images,
reject_if: proc { |a| a["image_id"].blank? },
allow_destroy: true
)
end

def image?
Expand All @@ -42,6 +42,11 @@ def page_images
super.in_locale(locale)
end

def page_images_attributes=(attrs)
ids = page_images.map(&:id)
super(attrs.reject { |a| a["_destroy"] && ids.exclude?(a["id"]) })
end

private

def update_primary_image
Expand Down

0 comments on commit 5aff153

Please sign in to comment.