diff --git a/app/models/miq_widget_set.rb b/app/models/miq_widget_set.rb index e2c78aeade4..e0dc8cba054 100644 --- a/app/models/miq_widget_set.rb +++ b/app/models/miq_widget_set.rb @@ -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")) @@ -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 @@ -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) @@ -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 diff --git a/spec/models/miq_widget_set_spec.rb b/spec/models/miq_widget_set_spec.rb index 3d9a6546c55..7f26ade16c3 100644 --- a/spec/models/miq_widget_set_spec.rb +++ b/spec/models/miq_widget_set_spec.rb @@ -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