From 2e062693ea32bce66d62f60fef9ce36fe729ed27 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 16 May 2017 23:12:25 +0200 Subject: [PATCH] Fixed issue #1086 - Tag Bug - deleted without changes. --- app/models/tag.rb | 28 +++++++++++++------------ test/unit/tag_test.rb | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index 255e8a38a..529114905 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -215,6 +215,9 @@ rename tag items old_tag_item = Tag::Item.find(data[:id]) already_existing_tag = Tag::Item.lookup(name: new_tag_name) + # check if no remame is needed + return true if new_tag_name.downcase == old_tag_item.name.downcase + # merge old with new tag if already existing if already_existing_tag @@ -245,22 +248,21 @@ rename tag items # delete not longer used tag old_tag_item.destroy + return true + end # update new tag name - else + old_tag_item.name = new_tag_name + old_tag_item.save - old_tag_item.name = new_tag_name - old_tag_item.save - - # touch reference objects - Tag.where(tag_item_id: old_tag_item.id).each { |tag| - tag_object = Tag::Object.lookup(id: tag.tag_object_id) - Tag.touch_reference_by_params( - object: tag_object.name, - o_id: tag.o_id, - ) - } - end + # touch reference objects + Tag.where(tag_item_id: old_tag_item.id).each { |tag| + tag_object = Tag::Object.lookup(id: tag.tag_object_id) + Tag.touch_reference_by_params( + object: tag_object.name, + o_id: tag.o_id, + ) + } true end diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb index d31fbd32b..fff602e9e 100644 --- a/test/unit/tag_test.rb +++ b/test/unit/tag_test.rb @@ -293,4 +293,53 @@ class TagTest < ActiveSupport::TestCase assert_not_equal(ticket2_lookup4.updated_at.to_s, ticket2_lookup3.updated_at.to_s) travel_back end + + test 'tags - rename tag with same name' do + + ticket1 = Ticket.create( + title: 'rename tag1', + group: Group.lookup(name: 'Users'), + customer_id: 2, + updated_by_id: 1, + created_by_id: 1, + ) + ticket2 = Ticket.create( + title: 'rename tag2', + group: Group.lookup(name: 'Users'), + customer_id: 2, + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1.tag_add('some rename tag1', 1) + ticket1.tag_add('some rename tag2 ', 1) + + ticket2.tag_add('some rename tag2', 1) + + tags_ticket1 = ticket1.tag_list + assert_equal(2, tags_ticket1.count) + assert(tags_ticket1.include?('some rename tag1')) + assert(tags_ticket1.include?('some rename tag2')) + + tags_ticket2 = ticket2.tag_list + assert_equal(1, tags_ticket2.count) + assert(tags_ticket2.include?('some rename tag2')) + + tag_item1 = Tag::Item.find_by(name: 'some rename tag1') + Tag::Item.rename( + id: tag_item1.id, + name: ' some rename tag1', + created_by_id: 1, + ) + + tags_ticket1 = ticket1.tag_list + assert_equal(2, tags_ticket1.count) + assert(tags_ticket1.include?('some rename tag1')) + assert(tags_ticket1.include?('some rename tag2')) + + tags_ticket2 = ticket2.tag_list + assert_equal(1, tags_ticket2.count) + assert(tags_ticket2.include?('some rename tag2')) + + end end