Merge pull request #3592 from evazion/fix-3583
Fix #3583: Copying notes should also change meta tags in the destination post
This commit is contained in:
@@ -8,6 +8,9 @@ class Post < ApplicationRecord
|
|||||||
class SearchError < Exception ; end
|
class SearchError < Exception ; end
|
||||||
class DeletionError < Exception ; end
|
class DeletionError < Exception ; end
|
||||||
|
|
||||||
|
# Tags to copy when copying notes.
|
||||||
|
NOTE_COPY_TAGS = %w[translated partially_translated check_translation translation_request reverse_translation]
|
||||||
|
|
||||||
before_validation :initialize_uploader, :on => :create
|
before_validation :initialize_uploader, :on => :create
|
||||||
before_validation :merge_old_changes
|
before_validation :merge_old_changes
|
||||||
before_validation :normalize_tags
|
before_validation :normalize_tags
|
||||||
@@ -1414,30 +1417,40 @@ class Post < ApplicationRecord
|
|||||||
last_noted_at.present?
|
last_noted_at.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_notes_to(other_post)
|
def copy_notes_to(other_post, copy_tags: NOTE_COPY_TAGS)
|
||||||
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
|
||||||
|
|
||||||
|
copy_tags.each do |tag|
|
||||||
|
other_post.remove_tag(tag)
|
||||||
|
other_post.add_tag(tag) if has_tag?(tag)
|
||||||
|
end
|
||||||
|
|
||||||
|
other_post.has_embedded_notes = has_embedded_notes
|
||||||
|
other_post.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
|
||||||
|
|
||||||
|
|||||||
@@ -2663,6 +2663,32 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "Notes:" do
|
||||||
|
context "#copy_notes_to" do
|
||||||
|
setup do
|
||||||
|
@src = FactoryGirl.create(:post, image_width: 100, image_height: 100, tag_string: "translated partially_translated", has_embedded_notes: true)
|
||||||
|
@dst = FactoryGirl.create(:post, image_width: 200, image_height: 200, tag_string: "translation_request")
|
||||||
|
|
||||||
|
@src.notes.create(x: 10, y: 10, width: 10, height: 10, body: "test")
|
||||||
|
@src.notes.create(x: 10, y: 10, width: 10, height: 10, body: "deleted", is_active: false)
|
||||||
|
@src.reload
|
||||||
|
|
||||||
|
@src.copy_notes_to(@dst)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "copy notes and tags" do
|
||||||
|
assert_equal(1, @dst.notes.active.length)
|
||||||
|
assert_equal(true, @dst.has_embedded_notes)
|
||||||
|
assert_equal("lowres partially_translated translated", @dst.tag_string)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "rescale notes" do
|
||||||
|
note = @dst.notes.active.first
|
||||||
|
assert_equal([20, 20, 20, 20], [note.x, note.y, note.width, note.height])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "Mass assignment: " do
|
context "Mass assignment: " do
|
||||||
should_not allow_mass_assignment_of(:last_noted_at).as(:member)
|
should_not allow_mass_assignment_of(:last_noted_at).as(:member)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user