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

Added ability to copy dashboard for the same or another group #18550

Merged
merged 2 commits into from
Mar 15, 2019
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
9 changes: 9 additions & 0 deletions app/models/miq_widget_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions spec/models/miq_widget_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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