From 6ae9e0412d4ffad5ab53face347ff0d6d90dd234 Mon Sep 17 00:00:00 2001 From: Yuri Rudman Date: Wed, 13 Mar 2019 15:54:22 -0400 Subject: [PATCH 1/2] added ability to copy dashboard: MiqWidgetSet.copy_dashboard BZ https://bugzilla.redhat.com/show_bug.cgi?id=1314875 --- app/models/miq_widget_set.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/models/miq_widget_set.rb b/app/models/miq_widget_set.rb index a24224198a7..e64e1373402 100644 --- a/app/models/miq_widget_set.rb +++ b/app/models/miq_widget_set.rb @@ -69,6 +69,15 @@ def self.sync_from_file(filename) end end + def self.copy_dashboard(source_widget_set, destination_name, destination_description, assign_to_group_id = nil) + assign_to_group = MiqGroup.find(assign_to_group_id || source_widget_set.group_id || source_widget_set.owner_id) + MiqWidgetSet.create!(:name => destination_name, + :description => destination_description, + :owner_type => "MiqGroup", + :set_type => source_widget_set.set_type, + :owner_id => assign_to_group.id) + end + def self.seed sync_from_dir end From 83410793acf19e00a51443fcc3d7811e8ab3c015 Mon Sep 17 00:00:00 2001 From: Yuri Rudman Date: Wed, 13 Mar 2019 15:55:28 -0400 Subject: [PATCH 2/2] rspec for MiqWidgetSet.copy_dashboard qWidgetSet# reword 2090bda0a8 added dashboard clone BZ https://bugzilla.redhat.com/show_bug.cgi?id=1314875 --- spec/models/miq_widget_set_spec.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/spec/models/miq_widget_set_spec.rb b/spec/models/miq_widget_set_spec.rb index 6a4181a0d00..6ea937e4de8 100644 --- a/spec/models/miq_widget_set_spec.rb +++ b/spec/models/miq_widget_set_spec.rb @@ -115,4 +115,34 @@ end end end + + describe ".copy_dashboard" do + let(:name) { "New Dashboard Name" } + let(:tab) { "Dashboard Tab" } + + it "raises error if passed name already taken" do + expect { MiqWidgetSet.copy_dashboard(@ws_group, @ws_group.name, tab) }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: MiqWidgetSet: Name has already been taken") + end + + it "raises error if passed tab name is empty" do + expect { MiqWidgetSet.copy_dashboard(@ws_group, name, "") }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: MiqWidgetSet: Description can't be blank") + end + + it "raises error if group with passed id does not exist" do + expect { MiqWidgetSet.copy_dashboard(@ws_group, name, tab, "9999") }.to raise_error(ActiveRecord::RecordNotFound, "Couldn't find MiqGroup with 'id'=9999") + end + + it "copy dashboard and set its owner to the group with passed group_id" do + another_group = FactoryBot.create(:miq_group, :description => 'some_group') + MiqWidgetSet.copy_dashboard(@ws_group, name, tab, another_group.id) + dashboard = MiqWidgetSet.find_by(:owner_id => another_group.id) + expect(dashboard.name).to eq(name) + end + + it "copy dashboard and set its owner to the same group if no group_id parameter passed" do + expect(MiqWidgetSet.where(:owner_id => group.id).count).to eq(1) + dashboard = MiqWidgetSet.copy_dashboard(@ws_group, name, tab) + expect(MiqWidgetSet.find_by(:owner_id => group.id, :name => name)).to eq dashboard + end + end end