Skip to content

Commit

Permalink
Refactor quadicon settings logic under My Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
skateman committed Jun 6, 2018
1 parent 8d51312 commit 817ded5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 98 deletions.
17 changes: 1 addition & 16 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,7 @@ def pp_choices

# Default UI settings
DEFAULT_SETTINGS = {
:quadicons => { # Show quad icons, by resource type
:ems => true,
:ems_cloud => true,
:ems_container => true,
:ems_network => true,
:ems_physical_infra => true,
:ems_storage => true,
:host => true,
:miq_template => true,
:physical_rack => true,
:physical_server => true,
:physical_switch => true,
:service => true,
:storage => true,
:vm => true
},
:quadicons => Hash.new(true), # Show quad icons by resource type, true by default
:views => { # List view setting, by resource type
:authkeypaircloud => "list",
:availabilityzone => "list",
Expand Down
18 changes: 4 additions & 14 deletions app/controllers/configuration_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -541,21 +541,11 @@ def get_form_vars
@edit = session[:edit]
case @tabform
when "ui_1" # Visual Settings tab
@edit[:new][:quadicons][:ems] = params[:quadicons_ems] == "true" if params[:quadicons_ems]
@edit[:new][:quadicons][:ems_cloud] = params[:quadicons_ems_cloud] == "true" if params[:quadicons_ems_cloud]
@edit[:new][:quadicons][:ems_container] = params[:quadicons_ems_container] == "true" if params[:quadicons_ems_container]
@edit[:new][:quadicons][:host] = params[:quadicons_host] == "true" if params[:quadicons_host]
@edit[:new][:quadicons][:vm] = params[:quadicons_vm] == "true" if params[:quadicons_vm]
@edit[:new][:quadicons][:physical_server] = params[:quadicons_physical_server] == "true" if params[:quadicons_physical_server]
@edit[:new][:quadicons][:physical_switch] = params[:quadicons_physical_switch] == "true" if params[:quadicons_physical_switch]
@edit[:new][:quadicons][:ems_physical_infra] = params[:quadicons_ems_physical_infra] == "true" if params[:quadicons_ems_physical_infra]
@edit[:new][:quadicons][:ems_network] = params[:quadicons_ems_network] == "true" if params[:quadicons_ems_network]
@edit[:new][:quadicons][:ems_storage] = params[:quadicons_ems_storage] == "true" if params[:quadicons_ems_storage]
@edit[:new][:quadicons][:miq_template] = params[:quadicons_miq_template] == "true" if params[:quadicons_miq_template]
if ::Settings.product.proto # Hide behind proto setting - Sprint 34
@edit[:new][:quadicons][:service] = params[:quadicons_service] == "true" if params[:quadicons_service]
view_context.allowed_quadicons.each_key do |key|
param = "quadicons_#{key}".to_sym
@edit[:new][:quadicons][key] = params[param] == "true" if params[param]
end
@edit[:new][:quadicons][:storage] = params[:quadicons_storage] == "true" if params[:quadicons_storage]

@edit[:new][:perpage][:grid] = params[:perpage_grid].to_i if params[:perpage_grid]
@edit[:new][:perpage][:tile] = params[:perpage_tile].to_i if params[:perpage_tile]
@edit[:new][:perpage][:list] = params[:perpage_list].to_i if params[:perpage_list]
Expand Down
28 changes: 28 additions & 0 deletions app/helpers/configuration_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
module ConfigurationHelper
include_concern 'ConfigurationViewHelper'

# Model class name comparison method that sorts provider models first
def compare_decorator_class(a, b)
if a.to_s.starts_with?('ManageIQ::Providers') == b.to_s.starts_with?('ManageIQ::Providers')
a.to_s <=> b.to_s
elsif a.to_s.starts_with?('ManageIQ::Providers')
-1
elsif b.to_s.starts_with?('ManageIQ::Providers')
1
end
end

# Returns with a hash of allowed quadicons for the current user
def allowed_quadicons
MiqDecorator.descendants # Get all the decorator classes
.select { |klass| klass.method_defined?(:quadicon) } # Select only the decorators that define a quadicon
.sort(&method(:compare_decorator_class))
.map do |decorator|
# Get the model name by removing Decorator from the class name
klass = decorator.to_s.chomp('Decorator')
# Retrieve the related root feature node
feature = klass.constantize.model_name.singular_route_key.to_sym
# Just return nil if the feature is not allowed for the current user
next unless role_allows?(:feature => feature, :any => true)

[klass.demodulize.underscore.to_sym, ui_lookup(:model => klass)]
end.compact.to_h
end
end
20 changes: 2 additions & 18 deletions app/helpers/quadicon_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,6 @@ def self.machine_state(state_str)
MACHINE_STATE_QUADRANT[state_str.try(:downcase)] || {}
end

def self.settings_key(klass, layout)
if klass.base_model.to_s.underscore == "ext_management_system"
case layout
when "ems_infra"
:ems
when "ems_block_storage", "ems_object_storage", "ems_storage"
:ems_storage
when "ems_physical_infra", "ems_cloud", "ems_network", "ems_container"
layout.to_sym
end
elsif klass.name.demodulize.starts_with?("Physical") && klass.base_model.name != klass.name.demodulize
klass.name.demodulize.underscore.to_sym
else
klass.base_model.name.underscore.to_sym
end
end

# Change the bottom-right quadrant of the quadicon with the policy simulation result
def policy_sim(quad, result)
if quad.try(:[], :bottom_right)
Expand Down Expand Up @@ -115,8 +98,9 @@ def quadicon_output(quadicon)
end

def quadicon_hash(item)
settings_key = item.class.try(:decorate).try(:to_s).try(:chomp, 'Decorator').try(:demodulize).try(:underscore).try(:to_sym)
# Quadicons should be displayed when explicitly set or when the user is on the policy simulation screen
quad_method = if settings(:quadicons, QuadiconHelper.settings_key(item.class, @layout)) || !!@policy_sim
quad_method = if settings(:quadicons, settings_key) || !!@policy_sim
:quadicon
else
:single_quad
Expand Down
34 changes: 8 additions & 26 deletions app/views/configuration/_ui_1.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,14 @@
%fieldset
%h3
= _('Grid/Tile Icons')
-# render condition title check_box label & checked T/F
-# Host Item is commented (condition set as false) until we have host item quads
- [[role_allows?(:feature => "ems_infra_show_list"), _("Infrastructure Provider"), "ems"],
[role_allows?(:feature => "ems_cloud_show_list"), _("Cloud Provider"), "ems_cloud"],
[role_allows?(:feature => "ems_container_show_list"), _("Containers Provider"), "ems_container"],
[role_allows?(:feature => "ems_physical_infra_show_list"), _("Physical Infra Provider"), "ems_physical_infra"],
[role_allows?(:feature => "ems_network_show_list"), _("Network Provider"), "ems_network"],
[role_allows?(:feature => "ems_block_storage_show_list") && role_allows?(:feature => "ems_object_storage_show_list"), _("Storage Provider"), "ems_storage"],
[role_allows?(:feature => "host_show_list"), _("Host"), "host"],
[role_allows?(:feature => "storage_show_list"), _("Datastores"), "storage"],
[true, _("VM"), "vm"],
[role_allows?(:feature => "physical_switch_show_list"), _("Physical Switch"), "physical_switch"],
[role_allows?(:feature => "physical_server_show_list"), _("Physical Server"), "physical_server"],
[true, _("Template"), "miq_template"]].each do |icons_checkbox|
- if icons_checkbox[0]
.form-group
%label.col-md-3.control-label
= _("Show %{title} Quadrants") % {:title => icons_checkbox[1]}
.col-md-8
= check_box_tag("quadicons_#{icons_checkbox[2]}",
true,
@edit[:new][:quadicons][icons_checkbox[2].to_sym], :data => {:on_text => _('Yes'), :off_text => _('No')})
:javascript
miqInitBootstrapSwitch("quadicons_#{icons_checkbox[2]}", "#{url}")
- allowed_quadicons.each do |key, name|
.form-group
%label.col-md-3.control-label
= _("Show %{title} Quadrants") % {:title => name}
.col-md-8
= check_box_tag("quadicons_#{key}", true, @edit[:new][:quadicons][key], :data => {:on_text => _('Yes'), :off_text => _('No')})
:javascript
miqInitBootstrapSwitch("quadicons_#{key}", "#{url}");
.form-group
%label.col-md-3.control-label
= _('Truncate Long Text')
Expand All @@ -46,8 +30,6 @@
@edit[:new][:display][:quad_truncate]),
:class => "selectpicker")

:javascript
miqSelectPickerEvent("quad_truncate", "#{url}")
%fieldset
%h3
= _('Start Page')
Expand Down
24 changes: 0 additions & 24 deletions spec/helpers/quadicon_helper_spec.rb

This file was deleted.

0 comments on commit 817ded5

Please sign in to comment.