Skip to content

Commit

Permalink
Update the button order on copy
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Aug 30, 2019
1 parent 4c54666 commit 9ce4609
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/models/custom_button_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ def deep_copy(options)
cbs.guid = SecureRandom.uuid
cbs.name = "#{name}-#{cbs.guid}"
cbs.set_data[:button_order] = []
cbs.set_data[:applies_to_id] = options[:owner].id
cbs.save!
custom_buttons.each do |cb|
cb_copy = cb.copy(:applies_to => options[:owner])
cbs.add_member(cb_copy)
options[:owner][:options][:button_order] << "cb-#{cb_copy.id}"
cbs.set_data[:button_order] << cb_copy.id
options[:owner].save!
cbs.save!
end
end
Expand Down
8 changes: 5 additions & 3 deletions app/models/service_template/copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def template_copy(new_name = "Copy of " + name + Time.zone.now.to_s)
if template_valid? && type != 'ServiceTemplateAnsiblePlaybook'
ActiveRecord::Base.transaction do
dup.tap do |template|
template.update_attributes(:name => new_name, :display => false)
template.update!(:name => new_name, :display => false, :options => {:button_order => []})
service_resources.each { |service_resource| resource_copy(service_resource, template) }
resource_action_copy(template)
additional_tenant_copy(template)
Expand All @@ -26,11 +26,13 @@ def additional_tenant_copy(template)
end

def custom_button_copy(custom_button, template)
custom_button.copy(:applies_to => template)
new_cb = custom_button.copy(:applies_to => template)
template[:options][:button_order] << "cb-#{new_cb.id}"
end

def custom_button_set_copy(custom_button_set, template)
custom_button_set.deep_copy(:owner => template)
new_cbs = custom_button_set.deep_copy(:owner => template)
template[:options][:button_order] << "cbg-#{new_cbs.id}"
end

def picture_copy(template)
Expand Down
5 changes: 4 additions & 1 deletion spec/models/service_template/copy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:service_template) { FactoryBot.create(:service_template) }
let(:service_template_ansible_tower) { FactoryBot.create(:service_template_ansible_tower) }
let(:service_template_orchestration) { FactoryBot.create(:service_template_orchestration) }
let(:set_data) { {:applies_to_class => "Service", :button_order => [custom_button.id]} }
let(:set_data) { {:applies_to_class => "Service", :button_order => [custom_button.id], :applies_to_id => service_template.id} }

def copy_template(template, name = nil)
copy = nil
Expand Down Expand Up @@ -42,6 +42,7 @@ def copy_template(template, name = nil)
end

it "with custom button set" do
service_template.update!(:options => {:button_order => ["cbg-#{custom_button_set.id}"]})
custom_button_set.add_member(custom_button)
expect(service_template.custom_button_sets.count).to eq(1)
expect(service_template.custom_button_sets.first.custom_buttons.count).to eq(1)
Expand All @@ -50,7 +51,9 @@ def copy_template(template, name = nil)
new_service_template = copy_template(service_template, "new_template")
expect(new_service_template.custom_button_sets.count).to eq(1)
expect(new_service_template.custom_button_sets.first.set_data).not_to eq(set_data)
expect(new_service_template.custom_button_sets.first.set_data[:applies_to_id]).not_to eq(service_template.custom_button_sets.first.set_data[:applies_to_id])
expect(new_service_template.custom_button_sets.first.custom_buttons.count).to eq(1)
expect(new_service_template[:options][:button_order]).to eq(["cbg-#{new_service_template.custom_button_sets.first.id}"])
expect(new_service_template.custom_button_sets.first.children).not_to eq([custom_button])
expect(new_service_template.custom_button_sets.first.children).to eq(new_service_template.custom_button_sets.first.custom_buttons)
end
Expand Down

0 comments on commit 9ce4609

Please sign in to comment.