Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpovolny committed May 22, 2019
1 parent 36cbc5b commit b606834
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
5 changes: 4 additions & 1 deletion app/presenters/menu/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def initialize
load_default_items
load_custom_items(Menu::YamlLoader)
load_custom_items(Menu::CustomLoader)
load_custom_items(Menu::SettingsLoader)
end

def merge_sections(sections)
Expand All @@ -85,6 +86,7 @@ def merge_sections(sections)
position = parent.index { |existing_section| existing_section.id == section.before }
end

binding.pry
if position
parent.insert(position, section)
else
Expand All @@ -95,6 +97,7 @@ def merge_sections(sections)

def merge_items(items)
items.each do |item|
binding.pry
parent = @id_to_section[item.parent_id]
raise InvalidMenuDefinition, 'Invalid parent' if parent.nil?

Expand Down Expand Up @@ -124,7 +127,7 @@ def preprocess_sections
end

def valid_sections
# format is {"vi" => :vi, "svc" => :svc . . }
# format is {"vi" => :vi, "svc" => :svc . . }
@valid_sections ||= @id_to_section.keys.index_by(&:to_s)
end
end
Expand Down
1 change: 1 addition & 0 deletions app/presenters/menu/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def features_recursive
end

def visible?
binding.pry if self.id.to_s =~ /custom/
userid = User.current_userid
store = Vmdb::PermissionStores.instance
auth = store.can?(id) && User.current_user.role_allows_any?(:identifiers => features_recursive)
Expand Down
36 changes: 36 additions & 0 deletions app/presenters/menu/settings_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Menu
class SettingsLoader < YamlLoader
include Singleton

def self.load
instance.load_from_settings
end

def load_from_settings
@sections = []
@items = []
begin
binding.pry
settings = ::Settings.ui.custom_menu
(settings || []).each do |i|
load_custom_item(i)
end
rescue => e # if we encounter an error while loading the menus, we ignore the whole settings
$log.error("Error loading custom menu from settings: #{e}")
$log.error("Settings were: #{settings}")
return [[], []]
end
[@sections, @items]
end

private

def load_custom_item(properties)
if properties['type'] == 'section'
@sections << create_custom_menu_section(properties)
else
@items << create_custom_menu_item(properties)
end
end
end
end
19 changes: 17 additions & 2 deletions app/presenters/menu/yaml_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,29 @@ def load_custom_item(file_name)
end
end

# In case `rbac` is a Hash, convert keys to symbols.
# Example: { :feature => 'vm_explorer', :any => true }
#
# Else assume string and return:
# { :feature => rbac }
#
def parse_rbac_property(rbac)
rbac === Hash ?
rbac.each_with_object({}) { |(k, v), h| h[k.to_sym] = v } :
{ :feature => rbac }
end

def create_custom_menu_item(properties)
rbac = properties['rbac'].each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
item_type = properties.key?('item_type') ? properties['item_type'].to_sym : :default
%w[id name rbac parent].each do |property|
if properties[property].blank?
raise Menu::Manager::InvalidMenuDefinition,
"incomplete definition -- missing #{property}"
end
end

rbac = parse_rbac_property(properties['rbac'])
item_type = properties.key?('item_type') ? properties['item_type'].to_sym : :default

item = Item.new(
properties['id'],
properties['name'],
Expand All @@ -53,6 +67,7 @@ def create_custom_menu_section(properties)
before = properties.key?('before') ? properties['before'].to_sym : nil
section_type = properties.key?('section_type') ? properties['section_type'].to_sym : :default
href = properties.key?('href') ? properties['href'].to_sym : nil
# no parent_id here?
Section.new(properties['id'].to_sym, properties['name'], icon, [], placement, before, section_type, href)
end
end
Expand Down

0 comments on commit b606834

Please sign in to comment.