From 6b49ba34701b325a7dd03e5dcce103b136eccd65 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 16 Jan 2017 19:40:23 -0500 Subject: [PATCH 1/4] MiqWidget#queued_at is a delegated field --- app/models/miq_widget.rb | 6 +----- spec/models/miq_widget_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/app/models/miq_widget.rb b/app/models/miq_widget.rb index 59d3d5205fe..16aa2b8deb3 100644 --- a/app/models/miq_widget.rb +++ b/app/models/miq_widget.rb @@ -40,7 +40,7 @@ def destroy_schedule virtual_column :status, :type => :string, :uses => :miq_task virtual_column :status_message, :type => :string, :uses => :miq_task - virtual_column :queued_at, :type => :datetime, :uses => :miq_task + virtual_delegate :queued_at, :to => "miq_task.created_on", :allow_nil => true virtual_column :last_run_on, :type => :datetime, :uses => :miq_schedule def row_count(row_count_param = nil) @@ -59,10 +59,6 @@ def next_run_on miq_schedule && miq_schedule.next_run_on end - def queued_at - miq_task && miq_task.created_on - end - def status if miq_task.nil? return "None" if last_run_on.nil? diff --git a/spec/models/miq_widget_spec.rb b/spec/models/miq_widget_spec.rb index b6460ff4116..1ffca120678 100644 --- a/spec/models/miq_widget_spec.rb +++ b/spec/models/miq_widget_spec.rb @@ -725,4 +725,30 @@ def add_dashboard_for_user(db_name, userid, group) end end end + + describe "#queued_at" do + it "is nil when no task" do + widget = FactoryGirl.build(:miq_widget) + expect(widget.queued_at).to be_nil + end + + it "uses task value" do + dt = Time.now.utc + widget = FactoryGirl.build(:miq_widget, :miq_task => FactoryGirl.build(:miq_task, :created_on => dt)) + expect(widget.queued_at).to eq(dt) + end + end + + describe "#status_message" do + it "is nil when no task" do + widget = FactoryGirl.build(:miq_widget) + expect(widget.status_message).to eq("Unknown") + end + + it "uses task value" do + dt = Time.now.utc + widget = FactoryGirl.build(:miq_widget, :miq_task => FactoryGirl.build(:miq_task, :message => "message")) + expect(widget.status_message).to eq("message") + end + end end From 49bc66b983fa142105914196b8c7235247fbc8fa Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 16 Jan 2017 20:03:53 -0500 Subject: [PATCH 2/4] remove unused MiqWidget#generation_status --- app/models/miq_widget.rb | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/app/models/miq_widget.rb b/app/models/miq_widget.rb index 16aa2b8deb3..db4cc870a1d 100644 --- a/app/models/miq_widget.rb +++ b/app/models/miq_widget.rb @@ -71,30 +71,6 @@ def status_message miq_task.nil? ? "Unknown" : miq_task.message end - # Returns status, last_run_on, message - # Status: None | Queued | Running | Complete - def generation_status - miq_task = self.miq_task - - if miq_task.nil? - return "None" if last_run_on.nil? - return "Complete", last_run_on - end - - status = case miq_task.state - when MiqTask::STATE_QUEUED - "Queued" - when MiqTask::STATE_FINISHED - "Complete" - when MiqTask::STATE_ACTIVE - "Running" - else - raise "Unknown state=#{miq_task.state.inspect}" - end - - return status, last_run_on, miq_task.message - end - def create_task(num_targets, userid = User.current_userid) userid ||= "system" context_data = {:targets => num_targets, :complete => 0} From 0250b2b380f7f018be147fe932a90c5f863dbf28 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 16 Jan 2017 21:05:52 -0500 Subject: [PATCH 3/4] delegate miq_widget#next_run_on to miq_schedule --- app/models/miq_widget.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/models/miq_widget.rb b/app/models/miq_widget.rb index db4cc870a1d..760f5b4a476 100644 --- a/app/models/miq_widget.rb +++ b/app/models/miq_widget.rb @@ -55,9 +55,7 @@ def last_run_on last_generated_content_on || (miq_schedule && miq_schedule.last_run_on) end - def next_run_on - miq_schedule && miq_schedule.next_run_on - end + delegate :next_run_on, :to => :miq_schedule, :allow_nil => true def status if miq_task.nil? From 7aaf73c9b392e237634b97320f89f81706aa276c Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 16 Jan 2017 21:11:44 -0500 Subject: [PATCH 4/4] delegate status_message to miq_task --- app/models/miq_widget.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/models/miq_widget.rb b/app/models/miq_widget.rb index 760f5b4a476..7c8919ff4e5 100644 --- a/app/models/miq_widget.rb +++ b/app/models/miq_widget.rb @@ -39,7 +39,7 @@ def destroy_schedule end virtual_column :status, :type => :string, :uses => :miq_task - virtual_column :status_message, :type => :string, :uses => :miq_task + virtual_delegate :status_message, :to => "miq_task.message", :allow_nil => true, :default => "Unknown" virtual_delegate :queued_at, :to => "miq_task.created_on", :allow_nil => true virtual_column :last_run_on, :type => :datetime, :uses => :miq_schedule @@ -65,10 +65,6 @@ def status miq_task.human_status end - def status_message - miq_task.nil? ? "Unknown" : miq_task.message - end - def create_task(num_targets, userid = User.current_userid) userid ||= "system" context_data = {:targets => num_targets, :complete => 0}