Skip to content

Commit bb21aa7

Browse files
committed
Implement toolbar extension mechanism for providers.
* Toolbar::Basic is moved to a base class Toolbar::Base * Toolbar::Override (practically Toolbar::Base) is where the provider extentions should inherit from. * Toolbar::Basic newly includes the extention mechanism.
1 parent b9dab27 commit bb21aa7

File tree

4 files changed

+97
-59
lines changed

4 files changed

+97
-59
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class ApplicationHelper::Toolbar::Base
2+
include Singleton
3+
extend SingleForwardable
4+
delegate %i(api_button button select twostate separator definition button_group custom_content) => :instance
5+
6+
def custom_content(name, args)
7+
@definition[name] = ApplicationHelper::Toolbar::Custom.new(name, args)
8+
end
9+
10+
def button_group(name, buttons)
11+
@definition[name] = ApplicationHelper::Toolbar::Group.new(name, buttons)
12+
end
13+
14+
def api_button(id, icon, title, text, keys = {})
15+
keys[:data] = {
16+
'function' => 'sendDataWithRx',
17+
'function-data' => {
18+
'type' => "generic",
19+
'controller' => "toolbarActions",
20+
'payload' => {
21+
'entity' => keys[:api][:entity].pluralize,
22+
'action' => keys[:api][:action]
23+
}
24+
}.to_json
25+
}
26+
generic_button(:button, id, icon, title, text, keys)
27+
end
28+
29+
def button(id, icon, title, text, keys = {})
30+
generic_button(:button, id, icon, title, text, keys)
31+
end
32+
33+
def select(id, icon, title, text, keys = {})
34+
generic_button(:buttonSelect, id, icon, title, text, keys)
35+
end
36+
37+
def twostate(id, icon, title, text, keys = {})
38+
generic_button(:buttonTwoState, id, icon, title, text, keys)
39+
end
40+
41+
def separator
42+
{:separator => true}
43+
end
44+
45+
attr_reader :definition
46+
47+
private
48+
49+
def initialize
50+
@loaded = false
51+
@definition = {}
52+
end
53+
54+
def generic_button(type, id, icon, title, text, keys)
55+
if text.kind_of?(Hash)
56+
keys = text
57+
text = title
58+
end
59+
{
60+
:type => type,
61+
:id => id.to_s,
62+
:icon => icon,
63+
:title => title,
64+
:text => text
65+
}.merge(keys)
66+
end
67+
end
Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,34 @@
1-
class ApplicationHelper::Toolbar::Basic
2-
include Singleton
3-
extend SingleForwardable
4-
delegate %i(api_button button select twostate separator definition button_group custom_content) => :instance
5-
6-
attr_reader :definition
7-
8-
def custom_content(name, args)
9-
@definition[name] = ApplicationHelper::Toolbar::Custom.new(name, args)
10-
end
11-
12-
def button_group(name, buttons)
13-
@definition[name] = ApplicationHelper::Toolbar::Group.new(name, buttons)
14-
end
15-
16-
def api_button(id, icon, title, text, keys = {})
17-
keys[:data] = {
18-
'function' => 'sendDataWithRx',
19-
'function-data' => {
20-
'type' => "generic",
21-
'controller' => "toolbarActions",
22-
'payload' => {
23-
'entity' => keys[:api][:entity].pluralize,
24-
'action' => keys[:api][:action]
25-
}
26-
}.to_json
27-
}
28-
generic_button(:button, id, icon, title, text, keys)
1+
class ApplicationHelper::Toolbar::Basic < ApplicationHelper::Toolbar::Base
2+
def definition(record = nil)
3+
extension_classes_filtered(record).reduce(@definition) do |acc, ext|
4+
acc.merge(ext.definition)
5+
end
296
end
307

31-
def button(id, icon, title, text, keys = {})
32-
generic_button(:button, id, icon, title, text, keys)
33-
end
8+
private
349

35-
def select(id, icon, title, text, keys = {})
36-
generic_button(:buttonSelect, id, icon, title, text, keys)
10+
def extension_classes
11+
@extension_classes ||= load_extension_classes
3712
end
3813

39-
def twostate(id, icon, title, text, keys = {})
40-
generic_button(:buttonTwoState, id, icon, title, text, keys)
41-
end
14+
def extension_classes_filtered(record)
15+
return extension_classes if record.nil?
4216

43-
def separator
44-
{:separator => true}
17+
# Example:
18+
# "ManageIQ::Providers::Amazon::ToolbarOverrides::EmsCloudCenter" is a match for
19+
# "ManageIQ::Providers::Amazon::CloudManager
20+
extension_classes.find_all do |ext|
21+
ext.name.split('::')[0..-3] == record.class.name.split('::')[0..-2]
22+
end
4523
end
4624

47-
private
25+
def load_extension_classes
26+
return [] if self.class.name.nil?
4827

49-
def initialize
50-
@definition = {}
51-
end
52-
53-
def generic_button(type, id, icon, title, text, keys)
54-
if text.kind_of?(Hash)
55-
keys = text
56-
text = title
57-
end
58-
{
59-
:type => type,
60-
:id => id.to_s,
61-
:icon => icon,
62-
:title => title,
63-
:text => text
64-
}.merge(keys)
28+
toolbar_base_name = self.class.name.to_s.split('::').last
29+
Vmdb::Plugins.collect do |plugin|
30+
instance = [plugin.name.chomp('::Engine'), 'ToolbarOverrides', toolbar_base_name].join('::')
31+
instance.safe_constantize
32+
end.compact
6533
end
6634
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ApplicationHelper::Toolbar::Override < ApplicationHelper::Toolbar::Base
2+
# For now this class is empty. However we want the provider toolbar extentions to inherit from here.
3+
end

app/helpers/application_helper/toolbar_builder.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ def build_toolbar(toolbar_name)
1616

1717
build_toolbar_setup
1818
toolbar_class = toolbar_class(toolbar_name)
19-
build_toolbar_from_class(toolbar_class)
19+
build_toolbar_from_class(toolbar_class, @record)
2020
end
2121

2222
def build_toolbar_by_class(toolbar_class)
2323
build_toolbar_setup
24-
build_toolbar_from_class(toolbar_class)
24+
build_toolbar_from_class(toolbar_class, @record)
2525
end
2626

2727
private
@@ -450,8 +450,8 @@ def build_toolbar_setup
450450
@sep_added = false
451451
end
452452

453-
def build_toolbar_from_class(toolbar_class)
454-
toolbar_class.definition.each_with_index do |(name, group), group_index|
453+
def build_toolbar_from_class(toolbar_class, record)
454+
toolbar_class.definition(record).each_with_index do |(_name, group), group_index|
455455
@sep_added = false
456456
@groups_added.push(group_index)
457457
case group

0 commit comments

Comments
 (0)