Skip to content

Commit

Permalink
Merge pull request ManageIQ#18448 from yrudman/remove-timezone-filter…
Browse files Browse the repository at this point in the history
…-if-widget-content-not-found

When user changing display timezone than some widgets show no data
  • Loading branch information
jrafanie authored Feb 20, 2019
2 parents f893003 + 80373b6 commit d6776df
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
26 changes: 14 additions & 12 deletions app/models/miq_widget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def find_or_build_contents_for_user(group, user, timezone = nil)
settings_for_build = {:miq_group_id => group.id}
settings_for_build[:user_id] = user.id if user
settings_for_build[:timezone] = timezone if timezone
contents = contents_for_owner(group, user, timezone) || miq_widget_contents.build(settings_for_build)
contents = miq_widget_contents.find_by(settings_for_build) || miq_widget_contents.build(settings_for_build)
contents.updated_at = Time.now.utc # Force updated timestamp to change when saved even if the new contents are the same

contents
Expand Down Expand Up @@ -355,20 +355,22 @@ def create_initial_content_for_user(user, group = nil)
end
end

def contents_for_owner(group, user, timezone = nil)
return unless group
timezone = "UTC" if timezone && !timezone_matters?
conditions = {:miq_group_id => group.id}
conditions[:user_id] = user.id if user
conditions[:timezone] = timezone if timezone
miq_widget_contents.find_by(conditions)
end

def contents_for_user(user)
user = self.class.get_user(user)
timezone = timezone_matters? ? user.get_timezone : "UTC"
contents = contents_for_owner(user.current_group, user, timezone)
contents ||= contents_for_owner(user.current_group, nil, timezone)
conditions = {:miq_group_id => user.current_group.id}
conditions[:user_id] = user.id
conditions[:timezone] = timezone
contents = miq_widget_contents.find_by(conditions)

conditions.delete(:user_id)
contents ||= miq_widget_contents.find_by(conditions)

if contents.nil?
_log.warn("No contents found for Widget: '#{title}' Group: #{user.current_group.description} in Timezone '#{timezone}'. Attempting to get widget's contents from any Timezone ...")
conditions.delete(:timezone)
contents = miq_widget_contents.find_by(conditions)
end
contents
end

Expand Down
32 changes: 17 additions & 15 deletions spec/models/miq_widget_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def add_dashboard_for_user(db_name, userid, group)
end

context "#contents_for_user" do
it "user owned" do
it "returns user owned widget contents in UTC timezone if user's timezone not specified" do
content = FactoryBot.create(:miq_widget_content,
:miq_widget => @widget_report_vendor_and_guest_os,
:user_id => @user1.id,
Expand All @@ -175,24 +175,26 @@ def add_dashboard_for_user(db_name, userid, group)
expect(@widget_report_vendor_and_guest_os.contents_for_user(@user1)).to eq(content)
end

it "owned by miq_group and in user's timezone" do
it "returns widget contents in user's timezone when content from different timezone also available" do
@user1.settings.store_path(:display, :timezone, "Eastern Time (US & Canada)")
content = FactoryBot.create(:miq_widget_content,
:miq_widget => @widget_report_vendor_and_guest_os,
:miq_group_id => @group1.id,
:timezone => "Eastern Time (US & Canada)"
)
expect(@widget_report_vendor_and_guest_os.contents_for_user(@user1)).to eq(content)
FactoryBot.create(:miq_widget_content,
:miq_widget => @widget_report_vendor_and_guest_os,
:miq_group_id => @group1.id,
:timezone => "UTC")
content_user_timezone = FactoryBot.create(:miq_widget_content,
:miq_widget => @widget_report_vendor_and_guest_os,
:miq_group_id => @group1.id,
:timezone => "Eastern Time (US & Canada)")
expect(@widget_report_vendor_and_guest_os.contents_for_user(@user1)).to eq(content_user_timezone)
end

it "owned by miq_group and not in user's timezone" do
it "returns widget contents if only content available is not in user's timezone" do
@user1.settings.store_path(:display, :timezone, "Eastern Time (US & Canada)")
FactoryBot.create(:miq_widget_content,
:miq_widget => @widget_report_vendor_and_guest_os,
:miq_group_id => @group1.id,
:timezone => "UTC"
)
expect(@widget_report_vendor_and_guest_os.contents_for_user(@user1)).to be_nil
content_utc = FactoryBot.create(:miq_widget_content,
:miq_widget => @widget_report_vendor_and_guest_os,
:miq_group_id => @group1.id,
:timezone => "UTC")
expect(@widget_report_vendor_and_guest_os.contents_for_user(@user1)).to eq(content_utc)
end

it "both user and miq_group owned" do
Expand Down

0 comments on commit d6776df

Please sign in to comment.