From 817ded55e3edb75525846ab0b5b8cb84e26941bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Hal=C3=A1sz?= Date: Mon, 4 Jun 2018 14:41:03 +0200 Subject: [PATCH] Refactor quadicon settings logic under My Settings --- app/controllers/application_controller.rb | 17 +---------- app/controllers/configuration_controller.rb | 18 +++-------- app/helpers/configuration_helper.rb | 28 +++++++++++++++++ app/helpers/quadicon_helper.rb | 20 ++---------- app/views/configuration/_ui_1.html.haml | 34 +++++---------------- spec/helpers/quadicon_helper_spec.rb | 24 --------------- 6 files changed, 43 insertions(+), 98 deletions(-) delete mode 100644 spec/helpers/quadicon_helper_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 73af9240f03..83eb2c090b8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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", diff --git a/app/controllers/configuration_controller.rb b/app/controllers/configuration_controller.rb index cdc11531a30..6bdacdaf663 100644 --- a/app/controllers/configuration_controller.rb +++ b/app/controllers/configuration_controller.rb @@ -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] diff --git a/app/helpers/configuration_helper.rb b/app/helpers/configuration_helper.rb index fb522ca86d1..c91ae3b85ee 100644 --- a/app/helpers/configuration_helper.rb +++ b/app/helpers/configuration_helper.rb @@ -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 diff --git a/app/helpers/quadicon_helper.rb b/app/helpers/quadicon_helper.rb index 2e156c612a6..c0e888e1eef 100644 --- a/app/helpers/quadicon_helper.rb +++ b/app/helpers/quadicon_helper.rb @@ -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) @@ -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 diff --git a/app/views/configuration/_ui_1.html.haml b/app/views/configuration/_ui_1.html.haml index a7d955439b5..ca48c75c25d 100644 --- a/app/views/configuration/_ui_1.html.haml +++ b/app/views/configuration/_ui_1.html.haml @@ -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') @@ -46,8 +30,6 @@ @edit[:new][:display][:quad_truncate]), :class => "selectpicker") - :javascript - miqSelectPickerEvent("quad_truncate", "#{url}") %fieldset %h3 = _('Start Page') diff --git a/spec/helpers/quadicon_helper_spec.rb b/spec/helpers/quadicon_helper_spec.rb deleted file mode 100644 index 7377d232e7e..00000000000 --- a/spec/helpers/quadicon_helper_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -describe QuadiconHelper do - describe '.settings_key' do - subject { described_class.settings_key(klass, layout) } - - { - :ems => [ManageIQ::Providers::Vmware::InfraManager, 'ems_infra'], - :ems_physical_infra => [ManageIQ::Providers::Lenovo::PhysicalInfraManager, 'ems_physical_infra'], - :ems_cloud => [ManageIQ::Providers::Openstack::CloudManager, 'ems_cloud'], - :ems_network => [ManageIQ::Providers::Amazon::NetworkManager, 'ems_network'], - :ems_container => [ManageIQ::Providers::Openshift::ContainerManager, 'ems_container'], - :physical_switch => [ManageIQ::Providers::Lenovo::PhysicalInfraManager::PhysicalSwitch, nil], - :switch => [Switch, nil] - }.each do |result, input| - context "class is #{input.first} with #{input.last} as layout" do - let(:klass) { input.first } - let(:layout) { input.last } - - it "returns with #{result}" do - expect(subject).to eq(result) - end - end - end - end -end