From 8e7ad9eb97edf8353d24512746d156706bc75dfe Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 31 Mar 2018 11:44:49 -0500 Subject: [PATCH] Post#copy_notes_to: wrap in transaction. --- app/models/post.rb | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 9e6a211e0..c0cd10fbf 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1415,29 +1415,31 @@ class Post < ApplicationRecord end def copy_notes_to(other_post) - if id == other_post.id - errors.add :base, "Source and destination posts are the same" - return false - end - unless has_notes? - errors.add :post, "has no notes" - return false - end + transaction do + if id == other_post.id + errors.add :base, "Source and destination posts are the same" + return false + end + unless has_notes? + errors.add :post, "has no notes" + return false + end - notes.active.each do |note| - note.copy_to(other_post) - end + notes.active.each do |note| + note.copy_to(other_post) + end - dummy = Note.new - if notes.active.length == 1 - dummy.body = "Copied 1 note from post ##{id}." - else - dummy.body = "Copied #{notes.active.length} notes from post ##{id}." + dummy = Note.new + if notes.active.length == 1 + dummy.body = "Copied 1 note from post ##{id}." + else + 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 - dummy.is_active = false - dummy.post_id = other_post.id - dummy.x = dummy.y = dummy.width = dummy.height = 0 - dummy.save end end