Post#copy_notes_to: wrap in transaction.
This commit is contained in:
@@ -1415,29 +1415,31 @@ class Post < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def copy_notes_to(other_post)
|
def copy_notes_to(other_post)
|
||||||
if id == other_post.id
|
transaction do
|
||||||
errors.add :base, "Source and destination posts are the same"
|
if id == other_post.id
|
||||||
return false
|
errors.add :base, "Source and destination posts are the same"
|
||||||
end
|
return false
|
||||||
unless has_notes?
|
end
|
||||||
errors.add :post, "has no notes"
|
unless has_notes?
|
||||||
return false
|
errors.add :post, "has no notes"
|
||||||
end
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
notes.active.each do |note|
|
notes.active.each do |note|
|
||||||
note.copy_to(other_post)
|
note.copy_to(other_post)
|
||||||
end
|
end
|
||||||
|
|
||||||
dummy = Note.new
|
dummy = Note.new
|
||||||
if notes.active.length == 1
|
if notes.active.length == 1
|
||||||
dummy.body = "Copied 1 note from post ##{id}."
|
dummy.body = "Copied 1 note from post ##{id}."
|
||||||
else
|
else
|
||||||
dummy.body = "Copied #{notes.active.length} notes from post ##{id}."
|
dummy.body = "Copied #{notes.active.length} notes from post ##{id}."
|
||||||
|
end
|
||||||
|
dummy.is_active = false
|
||||||
|
dummy.post_id = other_post.id
|
||||||
|
dummy.x = dummy.y = dummy.width = dummy.height = 0
|
||||||
|
dummy.save
|
||||||
end
|
end
|
||||||
dummy.is_active = false
|
|
||||||
dummy.post_id = other_post.id
|
|
||||||
dummy.x = dummy.y = dummy.width = dummy.height = 0
|
|
||||||
dummy.save
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user