From 2c8a973f6c96a8c3bef33755465492e05b86ce13 Mon Sep 17 00:00:00 2001
From: nasark <nasarak_786@hotmail.com>
Date: Wed, 31 Aug 2022 10:07:24 -0400
Subject: [PATCH] add pluggability for miq widgets

---
 app/models/miq_widget.rb                      | 10 +---------
 lib/vmdb/plugins.rb                           |  4 ++++
 spec/lib/task_helpers/exports/widgets_spec.rb |  4 +++-
 spec/models/miq_widget/chart_content_spec.rb  |  4 +++-
 spec/models/miq_widget_spec.rb                |  3 ++-
 5 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/app/models/miq_widget.rb b/app/models/miq_widget.rb
index 3a0cb045058..0b33ac66573 100644
--- a/app/models/miq_widget.rb
+++ b/app/models/miq_widget.rb
@@ -32,7 +32,6 @@ class MiqWidget < ApplicationRecord
   include YAMLImportExportMixin
   acts_as_miq_set_member
 
-  WIDGET_DIR =  File.expand_path(File.join(Rails.root, "product/dashboard/widgets"))
   WIDGET_REPORT_SOURCE = "Generated for widget".freeze
 
   before_destroy :destroy_schedule
@@ -458,7 +457,7 @@ def self.get_group(group)
   end
 
   def self.sync_from_dir
-    Dir.glob(File.join(WIDGET_DIR, "*.yaml")).sort.each { |f| sync_from_file(f) }
+    Vmdb::Plugins.miq_widgets_content.sort.each { |f| sync_from_file(f) }
   end
 
   def self.sync_from_file(filename)
@@ -556,13 +555,6 @@ def self.seed
     sync_from_dir
   end
 
-  def self.seed_widget(pattern)
-    files = Dir.glob(File.join(WIDGET_DIR, "*#{pattern}*"))
-    files.collect do |f|
-      sync_from_file(f)
-    end
-  end
-
   def save_with_shortcuts(shortcuts)  # [[<shortcut.id>, <widget_shortcut.description>], ...]
     transaction do
       ws = []  # Create an array of widget shortcuts
diff --git a/lib/vmdb/plugins.rb b/lib/vmdb/plugins.rb
index f5d4e40229b..b8004af9294 100644
--- a/lib/vmdb/plugins.rb
+++ b/lib/vmdb/plugins.rb
@@ -80,6 +80,10 @@ def automate_domains
       end
     end
 
+    def miq_widgets_content
+      @miq_widgets_content ||= Dir.glob(Rails.root.join("product/dashboard/widgets", "*")) + flat_map { |engine| content_directories(engine, "dashboard/widgets") }
+    end
+
     def provider_plugins
       @provider_plugins ||= select { |engine| engine.name.start_with?("ManageIQ::Providers::") }
     end
diff --git a/spec/lib/task_helpers/exports/widgets_spec.rb b/spec/lib/task_helpers/exports/widgets_spec.rb
index 868b24671f0..b23e52a0c18 100644
--- a/spec/lib/task_helpers/exports/widgets_spec.rb
+++ b/spec/lib/task_helpers/exports/widgets_spec.rb
@@ -3,11 +3,13 @@
     Dir.mktmpdir('miq_exp_dir')
   end
 
+  let(:widget_path) { Rails.root.join("product/dashboard/widgets/chart_vendor_and_guest_os.yaml") }
+
   before do
     EvmSpecHelper.local_miq_server
 
     MiqReport.seed_report("Vendor and Guest OS")
-    MiqWidget.seed_widget("chart_vendor_and_guest_os")
+    MiqWidget.sync_from_file(widget_path)
     MiqWidget.sync_from_hash(YAML.safe_load("
     description: Test Widget
     title: Test Widget
diff --git a/spec/models/miq_widget/chart_content_spec.rb b/spec/models/miq_widget/chart_content_spec.rb
index e75a5339ffd..5effa8ac334 100644
--- a/spec/models/miq_widget/chart_content_spec.rb
+++ b/spec/models/miq_widget/chart_content_spec.rb
@@ -1,10 +1,12 @@
 RSpec.describe "Widget Chart Content" do
   let(:widget) { MiqWidget.find_by(:description => "chart_vendor_and_guest_os") }
+  let(:widget_path) { Rails.root.join("product/dashboard/widgets/chart_vendor_and_guest_os.yaml") }
+
   before do
     EvmSpecHelper.local_miq_server
 
     MiqReport.seed_report("Vendor and Guest OS")
-    MiqWidget.seed_widget("chart_vendor_and_guest_os")
+    MiqWidget.sync_from_file(widget_path)
 
     @role  = FactoryBot.create(:miq_user_role)
     @group = FactoryBot.create(:miq_group, :miq_user_role => @role)
diff --git a/spec/models/miq_widget_spec.rb b/spec/models/miq_widget_spec.rb
index f01b69afee1..ecb46f8ad79 100644
--- a/spec/models/miq_widget_spec.rb
+++ b/spec/models/miq_widget_spec.rb
@@ -684,10 +684,11 @@ def add_dashboard_for_user(db_name, userid, group)
 
   context "multiple groups" do
     let(:widget) { MiqWidget.find_by(:description => "chart_vendor_and_guest_os") }
+    let(:widget_path) { Rails.root.join("product/dashboard/widgets/chart_vendor_and_guest_os.yaml") }
 
     before do
       MiqReport.seed_report("Vendor and Guest OS")
-      MiqWidget.seed_widget("chart_vendor_and_guest_os")
+      MiqWidget.sync_from_file(widget_path)
 
       # tests are written for timezone_matters = true
       widget.options[:timezone_matters] = true if widget.options