Skip to content

Commit

Permalink
Add custom_button and custom_button_set copy to service_template copy
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Feb 27, 2019
1 parent 2cbc654 commit ce894ab
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
20 changes: 16 additions & 4 deletions app/models/service_template/copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ def template_copy(new_name = "Copy of " + name + Time.zone.now.to_s)
ActiveRecord::Base.transaction do
dup.tap do |template|
template.update_attributes(:name => new_name, :display => false)
service_resources.each do |sr|
resource = sr.resource.respond_to?(:service_template_resource_copy) ? sr.resource.service_template_resource_copy : sr.resource
template.add_resource(resource, sr)
end
service_resources.each { |sr| resource_copy(sr, template) }
custom_buttons.each { |cb| custom_button_copy(cb, template) }
custom_button_sets_with_generics.each { |cbs| custom_button_set_copy(cbs, template) }
end.save!
end
end
end

def resource_copy(sr, template)
resource = sr.resource.respond_to?(:service_template_resource_copy) ? sr.resource.service_template_resource_copy : sr.resource
template.add_resource(resource, sr)
end

def custom_button_copy(cb, template)
cb.copy(:name => "Copy of " + cb.name + Time.zone.now.to_s, :description => "Copy of " + cb.description + " for template #{template.id}")
end

def custom_button_set_copy(cbs, template)
cbs.deep_copy(:owner => template)
end
end
26 changes: 26 additions & 0 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@
context "#template_copy" do
let(:service_template_ansible_tower) { FactoryBot.create(:service_template_ansible_tower, :name => "thing") }
let(:service_template_orchestration) { FactoryBot.create(:service_template_orchestration, :name => "thing2") }
let(:custom_button) { FactoryBot.create(:custom_button, :applies_to_class => "Service") }
let(:custom_button_set) { FactoryBot.create(:custom_button_set) }
before do
@st1 = FactoryBot.create(:service_template)
end
Expand All @@ -285,6 +287,30 @@
expect(ServiceTemplate.find_by(:name => "drew").guid).not_to eq(@st1.guid)
end

it "with custom button" do
custom_button
expect(@st1.custom_buttons.count).to eq(1)
@st1.template_copy("drew")
expect(ServiceTemplate.count).to eq(2)
expect(ServiceTemplate.find_by(:name => "drew")).not_to be(nil)
expect(ServiceTemplate.find_by(:name => "drew").display).to be(false)
expect(ServiceTemplate.find_by(:name => "drew").guid).not_to eq(@st1.guid)
expect(ServiceTemplate.find_by(:name => "drew").custom_buttons.count).to eq(2)
end

it "with custom button set" do
custom_button_set.add_member(custom_button)
expect(@st1.custom_button_sets_with_generics.count).to eq(1)
expect(@st1.custom_button_sets.count).to eq(0)
@st1.template_copy("drew")
expect(ServiceTemplate.count).to eq(2)
expect(ServiceTemplate.find_by(:name => "drew")).not_to be(nil)
expect(ServiceTemplate.find_by(:name => "drew").display).to be(false)
expect(ServiceTemplate.find_by(:name => "drew").guid).not_to eq(@st1.guid)
expect(ServiceTemplate.find_by(:name => "drew").custom_button_sets_with_generics.count).to eq(2)
expect(ServiceTemplate.find_by(:name => "drew").custom_button_sets.count).to eq(1)
end

it "with non-copyable resource (configuration script base)" do
@st1.add_resource(FactoryBot.create(:configuration_script_base))
expect(ServiceTemplate.count).to eq(1)
Expand Down

0 comments on commit ce894ab

Please sign in to comment.