Skip to content

Commit

Permalink
Merge pull request #19491 from yrudman/dashboard-name-uniqueness-for-…
Browse files Browse the repository at this point in the history
…group

Allow dashboards for different groups to have the same name
  • Loading branch information
Fryguy authored Nov 14, 2019
2 parents db684c9 + 6ca85cb commit dd27112
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 14 additions & 3 deletions app/models/miq_widget_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class MiqWidgetSet < ApplicationRecord
acts_as_miq_set

before_destroy :destroy_user_versions
before_save :keep_group_when_saving

WIDGET_DIR = File.expand_path(File.join(Rails.root, "product/dashboard/dashboards"))

Expand All @@ -15,7 +16,7 @@ def self.with_users

def destroy_user_versions
# userid, group_id and name are set for user version
# owner_type and owner_id are set for group version
# group_id, owner_type and owner_id are set for group version
return if userid

# When we destroy a WidgetSet for a group, we also want to destroy all user-modified versions
Expand All @@ -27,10 +28,14 @@ def self.destroy_user_versions
end

def self.where_unique_on(name, user = nil)
# user is nil for dashboards set for group
userid = user.try(:userid)
group_id = user.try(:current_group_id)
# a unique record is defined by name, group_id and userid
where(:name => name, :group_id => group_id, :userid => userid)
if userid.present?
where(:name => name, :group_id => user.current_group_id, :userid => userid)
else
where(:name => name, :userid => nil)
end
end

def self.subscribed_for_user(user)
Expand Down Expand Up @@ -98,4 +103,10 @@ def self.find_with_same_order(ids)
def self.display_name(number = 1)
n_('Dashboard', 'Dashboards', number)
end

private

def keep_group_when_saving
self.group_id = owner_id if owner_type == "MiqGroup" && owner_id.present?
end
end
4 changes: 2 additions & 2 deletions spec/models/miq_widget_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@
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")
it "does not raises error if the same dashboard name used for different groups" do
expect { MiqWidgetSet.copy_dashboard(@ws_group, @ws_group.name, tab) }.not_to raise_error
end

it "raises error if passed tab name is empty" do
Expand Down

0 comments on commit dd27112

Please sign in to comment.