From 6b83dfb0f075bcc1ece6b36804d982c3459bf359 Mon Sep 17 00:00:00 2001 From: Yuri Rudman Date: Mon, 21 Jan 2019 16:22:39 -0500 Subject: [PATCH 1/2] Do not create new record in Tag table when editing existing tag Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1666887 --- app/models/classification.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/classification.rb b/app/models/classification.rb index ee701deed29..3afcc4a27bd 100644 --- a/app/models/classification.rb +++ b/app/models/classification.rb @@ -529,7 +529,11 @@ def find_tag def save_tag tag_name = Classification.name2tag(name, parent_id, ns) - self.tag = Tag.in_region(region_id).find_or_create_by(:name => tag_name) + if tag.present? + tag.update_attributes(:name => tag_name) + else + self.tag = Tag.in_region(region_id).find_or_create_by(:name => tag_name) + end end def delete_all_entries From fccb683d959c00a74de04857abb1cebe013a41dd Mon Sep 17 00:00:00 2001 From: Yuri Rudman Date: Tue, 22 Jan 2019 18:14:29 -0500 Subject: [PATCH 2/2] added rspec for Classification#save --- spec/models/classification_spec.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/spec/models/classification_spec.rb b/spec/models/classification_spec.rb index 366419185d4..1ff07f27c63 100644 --- a/spec/models/classification_spec.rb +++ b/spec/models/classification_spec.rb @@ -571,6 +571,31 @@ end end + describe '#save' do + let(:new_name) { "new_tag_name" } + + context "editing existing tag" do + it "updates record in Tag table which linked to this classification" do + classification = FactoryBot.create(:classification_tag, :name => "some_tag_name") + tag = classification.tag + classification.name = new_name + classification.save + expect(tag.id).to eq classification.tag.id + expect(classification.tag.name).to eq(Classification.name2tag(new_name)) + end + end + + context "saving new tag" do + it "creates new record in Tag table and links it to this classification" do + classification = Classification.new + classification.description = "some description" + classification.name = new_name + classification.save + expect(classification.tag).to eq(Tag.last) + end + end + end + def all_tagged_with(target, all, category = nil) tagged_with(target, :all => all, :cat => category) end