Skip to content

Commit

Permalink
Add copying tag as optional choice
Browse files Browse the repository at this point in the history
When service template is copied, tags might also need
to be copied as well. This change adds an optional filed
to allow tags to be copied as well.
  • Loading branch information
astrozzc committed Aug 26, 2019
1 parent 34f7b65 commit 7ddb0d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/models/service_template/copy.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ServiceTemplate::Copy
extend ActiveSupport::Concern

def template_copy(new_name = "Copy of " + name + Time.zone.now.to_s)
def template_copy(new_name = "Copy of " + name + Time.zone.now.to_s, copy_tags: false)
if template_valid? && type != 'ServiceTemplateAnsiblePlaybook'
ActiveRecord::Base.transaction do
dup.tap do |template|
Expand All @@ -10,7 +10,7 @@ def template_copy(new_name = "Copy of " + name + Time.zone.now.to_s)
resource_action_copy(template)
additional_tenant_copy(template)
picture_copy(template) if picture

tags_copy(template) if copy_tags
direct_custom_buttons.each { |custom_button| custom_button_copy(custom_button, template) }
custom_button_sets.each { |custom_button_set| custom_button_set_copy(custom_button_set, template) }
template.save!
Expand Down Expand Up @@ -45,4 +45,8 @@ def resource_copy(service_resource, template)
def resource_action_copy(template)
template.resource_actions << resource_actions.collect(&:dup)
end

def tags_copy(template)
template.tags << tags.dup
end
end
21 changes: 21 additions & 0 deletions spec/models/service_template/copy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,26 @@ def copy_template(template, name = nil)
expect(new_template.additional_tenants.count).to eq(2)
end
end

context "tags" do
it "does not duplicate tags by default" do
service_template.tags << [
FactoryBot.create(:tags)
]
expect(service_template.tags.count).to eq(1)
new_template = service_template.template_copy
expect(new_template.tags.count).to eq(0)
end

it "duplicates tags" do
service_template.tags << [
FactoryBot.create(:tags),
FactoryBot.create(:tags)
]
expect(service_template.tags.count).to eq(2)
new_template = service_template.template_copy(:copy_tags => true)
expect(new_template.tags.count).to eq(2)
end
end
end
end

0 comments on commit 7ddb0d7

Please sign in to comment.