Skip to content

Commit

Permalink
Merge pull request #18436 from kbrock/tag_name_updates_hammer
Browse files Browse the repository at this point in the history
[HAMMER] Allow Tag names to be updated

(cherry picked from commit ffe2069)

https://bugzilla.redhat.com/show_bug.cgi?id=1668730
  • Loading branch information
simaishi committed Feb 11, 2019
1 parent 955087b commit 3b7bfa7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
7 changes: 6 additions & 1 deletion app/models/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,12 @@ def find_tag
end

def save_tag
self.tag = Tag.find_or_create_by_classification_name(name, region_id, ns, parent_id)
tag_name = Classification.name2tag(name, parent_id, ns)
if tag_id.present? || tag.present?
tag.update_attributes(:name => tag_name) unless tag.name == tag_name
else
self.tag = Tag.in_region(region_id).find_or_create_by(:name => tag_name)
end
end

def delete_all_entries
Expand Down
16 changes: 6 additions & 10 deletions spec/factories/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@
#

factory :classification_cost_center_with_tags, :parent => :classification_cost_center do
children do
[
FactoryGirl.create(:classification_tag, :name => "001", :description => "Cost Center 001"),
]
after(:create) do |c|
FactoryBot.create(:classification_tag, :name => "001", :description => "Cost Center 001", :parent => c)
end
end

factory :classification_department_with_tags, :parent => :classification_department do
children do
[
FactoryGirl.create(:classification_tag, :name => "accounting", :description => "Accounting"),
FactoryGirl.create(:classification_tag, :name => "finance", :description => "Financial Services"),
FactoryGirl.create(:classification_tag, :name => "hr", :description => "Human Resources"),
]
after(:create) do |c|
FactoryBot.create(:classification_tag, :parent => c, :name => "accounting", :description => "Accounting")
FactoryBot.create(:classification_tag, :parent => c, :name => "finance", :description => "Financial Services")
FactoryBot.create(:classification_tag, :parent => c, :name => "hr", :description => "Human Resources")
end
end
end
67 changes: 67 additions & 0 deletions spec/models/classification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,73 @@
end
end

describe "name2tag" do
let(:root_ns) { "/managed" }
let(:parent_ns) { "/managed/test_category" }
let(:entry_ns) { "/managed/test_category/test_entry" }
let(:parent) { FactoryBot.create(:classification, :name => "test_category") }

it "creates parent tag" do
expect(Classification.name2tag("test_category")).to eq(parent_ns)
end

it "creates tag with name and ns" do
expect(Classification.name2tag("test_entry", 0, parent_ns)).to eq(entry_ns)
expect(Classification.name2tag("test_category", 0, root_ns)).to eq(parent_ns)
end

it "creates tag with name, ns, and parent_id" do
expect(Classification.name2tag("test_entry", parent.id, root_ns)).to eq(entry_ns)
end

it "creates tag with name, ns and parent" do
expect(Classification.name2tag("test_entry", parent, root_ns)).to eq(entry_ns)
end
end

describe '.create_category!' do
it "is a category" do
c1 = Classification.create_category!(:name => 'a', :description => 'a')

expect(c1).to be_category
end
end

describe '#save' do
let(:new_name) { "new_tag_name" }
let(:category) { FactoryBot.create(:classification, :name => "category") }

context "editing existing classification" do
let(:classification) { FactoryBot.create(:classification_tag, :parent => category, :name => "some_tag_name") }
it "doesn't assign new tag " do
tag = classification.tag
classification.update_attributes!(:name => new_name)
classification.reload
expect(tag.id).to eq classification.tag.id
expect(classification.name).to eq(new_name)
expect(classification.tag.name).to eq(Classification.name2tag(new_name, category))
end
end

context "saving new classification" do
it "creates new tag" do
classification = Classification.create(:description => new_name, :parent => category, :name => new_name)
expect(classification.tag).to be_present
expect(classification.name).to eq(new_name)
expect(classification.tag.name).to eq(Classification.name2tag(new_name, category))
end
end
end

describe '.create' do
it "assigns proper tags" do
FactoryBot.create(:classification_department_with_tags)
Tag.all.each do |tag|
expect(tag.name).to eq(Classification.name2tag(tag.classification.name, tag.classification.parent_id))
end
end
end

def all_tagged_with(target, all, category = nil)
tagged_with(target, :all => all, :cat => category)
end
Expand Down

0 comments on commit 3b7bfa7

Please sign in to comment.