Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions app/helpers/application_helper/toolbar/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class ApplicationHelper::Toolbar::Base
include Singleton
extend SingleForwardable
delegate %i(api_button button select twostate separator definition button_group custom_content) => :instance

def custom_content(name, args)
@definition[name] = ApplicationHelper::Toolbar::Custom.new(name, args)
end

def button_group(name, buttons)
@definition[name] = ApplicationHelper::Toolbar::Group.new(name, buttons)
end

def api_button(id, icon, title, text, keys = {})
keys[:data] = {
'function' => 'sendDataWithRx',
'function-data' => {
'type' => "generic",
'controller' => "toolbarActions",
'payload' => {
'entity' => keys[:api][:entity].pluralize,
'action' => keys[:api][:action]
}
}.to_json
}
generic_button(:button, id, icon, title, text, keys)
end

def button(id, icon, title, text, keys = {})
generic_button(:button, id, icon, title, text, keys)
end

def select(id, icon, title, text, keys = {})
generic_button(:buttonSelect, id, icon, title, text, keys)
end

def twostate(id, icon, title, text, keys = {})
generic_button(:buttonTwoState, id, icon, title, text, keys)
end

def separator
{:separator => true}
end

attr_reader :definition

private

def initialize
@loaded = false
@definition = {}
end

def generic_button(type, id, icon, title, text, keys)
if text.kind_of?(Hash)
keys = text
text = title
end
{
:type => type,
:id => id.to_s,
:icon => icon,
:title => title,
:text => text
}.merge(keys)
end
end
78 changes: 23 additions & 55 deletions app/helpers/application_helper/toolbar/basic.rb
Original file line number Diff line number Diff line change
@@ -1,66 +1,34 @@
class ApplicationHelper::Toolbar::Basic
include Singleton
extend SingleForwardable
delegate %i(api_button button select twostate separator definition button_group custom_content) => :instance

attr_reader :definition

def custom_content(name, args)
@definition[name] = ApplicationHelper::Toolbar::Custom.new(name, args)
end

def button_group(name, buttons)
@definition[name] = ApplicationHelper::Toolbar::Group.new(name, buttons)
end

def api_button(id, icon, title, text, keys = {})
keys[:data] = {
'function' => 'sendDataWithRx',
'function-data' => {
'type' => "generic",
'controller' => "toolbarActions",
'payload' => {
'entity' => keys[:api][:entity].pluralize,
'action' => keys[:api][:action]
}
}.to_json
}
generic_button(:button, id, icon, title, text, keys)
class ApplicationHelper::Toolbar::Basic < ApplicationHelper::Toolbar::Base
def definition(record = nil)
extension_classes_filtered(record).reduce(@definition) do |acc, ext|
acc.merge(ext.definition)
end
end

def button(id, icon, title, text, keys = {})
generic_button(:button, id, icon, title, text, keys)
end
private

def select(id, icon, title, text, keys = {})
generic_button(:buttonSelect, id, icon, title, text, keys)
def extension_classes
@extension_classes ||= load_extension_classes
end

def twostate(id, icon, title, text, keys = {})
generic_button(:buttonTwoState, id, icon, title, text, keys)
end
def extension_classes_filtered(record)
return extension_classes if record.nil?

def separator
{:separator => true}
# Example:
# "ManageIQ::Providers::Amazon::ToolbarOverrides::EmsCloudCenter" is a match for
# "ManageIQ::Providers::Amazon::CloudManager
extension_classes.find_all do |ext|
ext.name.split('::')[0..2] == record.class.name.split('::')[0..2]
end
end

private
def load_extension_classes
return [] if self.class.name.nil?

def initialize
@definition = {}
end

def generic_button(type, id, icon, title, text, keys)
if text.kind_of?(Hash)
keys = text
text = title
end
{
:type => type,
:id => id.to_s,
:icon => icon,
:title => title,
:text => text
}.merge(keys)
toolbar_base_name = self.class.name.to_s.split('::').last
Vmdb::Plugins.collect do |plugin|
instance = [plugin.name.chomp('::Engine'), 'ToolbarOverrides', toolbar_base_name].join('::')
instance.safe_constantize
end.compact
end
end
3 changes: 3 additions & 0 deletions app/helpers/application_helper/toolbar/override.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ApplicationHelper::Toolbar::Override < ApplicationHelper::Toolbar::Base
# For now this class is empty. However we want the provider toolbar extentions to inherit from here.
end
8 changes: 4 additions & 4 deletions app/helpers/application_helper/toolbar_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def build_toolbar(toolbar_name)

build_toolbar_setup
toolbar_class = toolbar_class(toolbar_name)
build_toolbar_from_class(toolbar_class)
build_toolbar_from_class(toolbar_class, @record)
end

def build_toolbar_by_class(toolbar_class)
build_toolbar_setup
build_toolbar_from_class(toolbar_class)
build_toolbar_from_class(toolbar_class, @record)
end

private
Expand Down Expand Up @@ -450,8 +450,8 @@ def build_toolbar_setup
@sep_added = false
end

def build_toolbar_from_class(toolbar_class)
toolbar_class.definition.each_with_index do |(name, group), group_index|
def build_toolbar_from_class(toolbar_class, record)
toolbar_class.definition(record).each_with_index do |(_name, group), group_index|
@sep_added = false
@groups_added.push(group_index)
case group
Expand Down