From 1cb14f8cf7045908d178ade7b7cd1fa2c2f9590c Mon Sep 17 00:00:00 2001
From: lgalis <lgalis@redhat.com>
Date: Mon, 13 Nov 2017 08:43:02 -0500
Subject: [PATCH] Toolbar fixes to account for applies_to_id in button group

---
 .../application_helper/toolbar_builder.rb     | 22 +++++++++++++------
 .../application_helper/toolbar_chooser.rb     |  2 ++
 app/views/service/show.html.haml              |  4 ++--
 config/routes.rb                              |  1 +
 4 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/app/helpers/application_helper/toolbar_builder.rb b/app/helpers/application_helper/toolbar_builder.rb
index 98e38a47958b..42f3b486be03 100644
--- a/app/helpers/application_helper/toolbar_builder.rb
+++ b/app/helpers/application_helper/toolbar_builder.rb
@@ -319,12 +319,14 @@ def custom_button_selects(model, record, toolbar_result)
   end
 
   def custom_toolbar_class(toolbar_result)
-    model = if @display == 'generic_objects'
-              GenericObjectDefinition
-            else
-              @record ? @record.class : model_for_custom_toolbar
-            end
-    build_custom_toolbar_class(model, @record, toolbar_result)
+    record = @record
+    if @display == 'generic_objects'
+      model = GenericObjectDefinition
+      record = GenericObject.find_by(:id => @sb[:rec_id])
+    else
+      model = @record ? @record.class : model_for_custom_toolbar
+    end
+    build_custom_toolbar_class(model, record, toolbar_result)
   end
 
   def build_custom_toolbar_class(model, record, toolbar_result)
@@ -359,7 +361,8 @@ def button_class_name(model)
   def service_template_id(record)
     case record
     when Service then         record.service_template_id
-    when ServiceTemplate then record.id
+    when ServiceTemplate, GenericObjectDefinition then record.id
+    when GenericObject then record.generic_object_definition.id
     end
   end
 
@@ -369,6 +372,11 @@ def record_to_service_buttons(record)
     record.service_template.custom_buttons.collect { |cb| create_raw_custom_button_hash(cb, record) }
   end
 
+  def record_to_generic_object_buttons(record)
+    return [] unless record.kind_of?(GenericObject)
+    record.generic_object_definition.custom_buttons.collect { |cb| create_raw_custom_button_hash(cb, record) }
+  end
+
   def get_custom_buttons(model, record, toolbar_result)
     cbses = CustomButtonSet.find_all_by_class_name(button_class_name(model), service_template_id(record))
     cbses = CustomButtonSet.filter_with_visibility_expression(cbses, record)
diff --git a/app/helpers/application_helper/toolbar_chooser.rb b/app/helpers/application_helper/toolbar_chooser.rb
index 472dfd36f1f7..1b1910345bff 100644
--- a/app/helpers/application_helper/toolbar_chooser.rb
+++ b/app/helpers/application_helper/toolbar_chooser.rb
@@ -466,6 +466,8 @@ def center_toolbar_filename_classic
       elsif to_display_center.include?(@display)
         return "#{@display}_center"
       end
+    elsif @display == 'generic_objects'
+      return "#{@display}_center"
     elsif @lastaction == "compare_miq" || @lastaction == "compare_compress"
       return "compare_center_tb"
     elsif @lastaction == "drift_history"
diff --git a/app/views/service/show.html.haml b/app/views/service/show.html.haml
index f38a9285ebca..2f2cd4a285b9 100644
--- a/app/views/service/show.html.haml
+++ b/app/views/service/show.html.haml
@@ -1,7 +1,7 @@
 #main_div
   - if %w(generic_objects).include?(@display)
-    - if @lastaction == 'generic_object'
-      = render :partial => "layouts/item", :locals => {:action_url => "show/#{@item.id}"}
+    - if @lastaction == 'generic_object' && @item
+      = render :partial => "layouts/item", :locals => {:action_url => "show/#{@item}"}
     - else
       = render :partial => "layouts/gtl", :locals => {:action_url => "show/#{@record.id}"}
   - else
diff --git a/config/routes.rb b/config/routes.rb
index 7122be93cdbe..44587ada03be 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -2968,6 +2968,7 @@
 
     :service                  => {
       :get  => %w(
+        dialog_load
         download_data
         edit
         explorer