Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the button order on copy #19227

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/models/custom_button_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ 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] ||= []
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 => []})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@d-m-u How does the button_order get updated to cb-#{id}? Is there a rails callback that's updating it?

Copy link
Contributor Author

@d-m-u d-m-u Aug 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's what the rest of the changes, 30 and 35, in this file are doing

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
15 changes: 10 additions & 5 deletions 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,17 +42,22 @@ 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)
expect(service_template.custom_button_sets.first.set_data).to eq(set_data)
expect(service_template.custom_button_sets.first.children).to eq([custom_button])
new_service_template = copy_template(service_template, "new_template")
new_button_group = new_service_template.custom_button_sets.first
new_button = new_service_template.custom_button_sets.first.custom_buttons.first
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.custom_buttons.count).to eq(1)
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)
expect(new_button_group.set_data).not_to eq(set_data)
expect(new_button_group.set_data[:applies_to_id]).not_to eq(service_template.custom_button_sets.first.set_data[:applies_to_id])
expect(new_button_group.custom_buttons.count).to eq(1)
expect(new_service_template[:options][:button_order]).to contain_exactly("cbg-#{new_button_group.id}", "cb-#{new_button.id}")
expect(new_button_group.children).not_to eq([custom_button])
expect(new_button_group.children).to eq(new_button_group.custom_buttons)
end

it "with non-copyable resource (configuration script base)" do
Expand Down