From 2f947f7f31f960f8b297a764d4b4d070b1b250f8 Mon Sep 17 00:00:00 2001 From: akumari Date: Thu, 22 Feb 2024 15:27:04 +0530 Subject: [PATCH] Fixes #37458 - Fix Style/CaseLikeIf cop --- app/controllers/filters_controller.rb | 5 +++-- app/helpers/audits_helper.rb | 14 ++++++++------ app/helpers/compute_resources_vms_helper.rb | 5 +++-- app/helpers/hosts_helper.rb | 5 +++-- app/helpers/layout_helper.rb | 5 +++-- app/helpers/templates_helper.rb | 5 +++-- .../compute_resources/foreman/model/vmware.rb | 5 +++-- app/models/operatingsystems/windows.rb | 5 +++-- app/models/taxonomy.rb | 5 +++-- app/models/usergroup_member.rb | 10 ++++++---- app/presenters/setting_presenter.rb | 7 ++++--- app/registries/foreman/setting_manager.rb | 5 +++-- app/services/foreman/parameter_filter.rb | 7 ++++--- app/services/foreman/parameters/caster.rb | 7 ++++--- app/services/name_synchronizer.rb | 5 +++-- app/services/setting_registry.rb | 5 +++-- config/initializers/active_support_extensions.rb | 5 +++-- lib/fog_extensions/openstack/server.rb | 5 +++-- test/unit/tasks/seeds_test.rb | 5 +++-- 19 files changed, 68 insertions(+), 47 deletions(-) diff --git a/app/controllers/filters_controller.rb b/app/controllers/filters_controller.rb index d1ea33a88bb..44040ca86a6 100644 --- a/app/controllers/filters_controller.rb +++ b/app/controllers/filters_controller.rb @@ -67,12 +67,13 @@ def setup_search_options @original_search_parameter = params[:search] params[:search] ||= "" params.each do |param, value| - if param =~ /role_id$/ + case param + when /role_id$/ if (role = Role.find_by_id(value)).present? query = "role_id = #{role.id}" params[:search] += query unless params[:search].include? query end - elsif param =~ /(\w+)_id$/ + when /(\w+)_id$/ if value.present? query = "#{Regexp.last_match(1)} = #{value}" params[:search] += query unless params[:search].include? query diff --git a/app/helpers/audits_helper.rb b/app/helpers/audits_helper.rb index 686b64d535e..acc34adefea 100644 --- a/app/helpers/audits_helper.rb +++ b/app/helpers/audits_helper.rb @@ -65,13 +65,14 @@ def details(audit, path = audits_path(:search => "id=#{audit.id}")) return [] unless audit.audited_changes.present? audit.audited_changes.map do |name, change| next if change.nil? || change.to_s.empty? - if name == 'template' + case name + when 'template' (_("Template content changed %s") % (link_to 'view diff', path)).html_safe if audit_template? audit - elsif name == "password_changed" + when "password_changed" _("Password has been changed") - elsif name == "owner_id" || name == "owner_type" + when "owner_id", "owner_type" _("Owner changed to %s") % (audit.revision.owner rescue _('N/A')) - elsif name == 'global_status' + when 'global_status' base = audit.audited_changes.values[0] from = HostStatus::Global.new(base[0]).to_label to = HostStatus::Global.new(base[1]).to_label @@ -285,9 +286,10 @@ def find_associated_records_using_key(key, change, audit) end if association_class - if key =~ /_ids$/ + case key + when /_ids$/ association_class&.where(id: change)&.index_by(&:id) - elsif key =~ /_id$/ + when /_id$/ association_class&.find(change) end elsif auditable_class.respond_to?('audit_hook_to_find_records') diff --git a/app/helpers/compute_resources_vms_helper.rb b/app/helpers/compute_resources_vms_helper.rb index c1abf3b7d53..502e19948aa 100644 --- a/app/helpers/compute_resources_vms_helper.rb +++ b/app/helpers/compute_resources_vms_helper.rb @@ -96,10 +96,11 @@ def common_available_actions(vm, authorizer = nil) def openstack_available_actions(vm, authorizer = nil) actions = [] - if vm.state == 'ACTIVE' + case vm.state + when 'ACTIVE' actions << vm_power_action(vm, authorizer) actions << vm_pause_action(vm, authorizer) - elsif vm.state == 'PAUSED' + when 'PAUSED' actions << vm_pause_action(vm, authorizer) else actions << vm_power_action(vm, authorizer) diff --git a/app/helpers/hosts_helper.rb b/app/helpers/hosts_helper.rb index 0465c9f2b7a..65b3052c657 100644 --- a/app/helpers/hosts_helper.rb +++ b/app/helpers/hosts_helper.rb @@ -86,9 +86,10 @@ def last_report_tooltip(record) end def host_owner_column(host) - if host.owner_type == 'User' + case host.owner_type + when 'User' icon_text('user', host.owner, :kind => 'fa') - elsif host.owner_type == 'Usergroup' + when 'Usergroup' icon_text('users', host.owner, :kind => 'fa') end end diff --git a/app/helpers/layout_helper.rb b/app/helpers/layout_helper.rb index 77848fed8f0..90f762d51c8 100644 --- a/app/helpers/layout_helper.rb +++ b/app/helpers/layout_helper.rb @@ -115,13 +115,14 @@ def javascript_include_tag(*params, **kwargs) # @deprecated Previously provided by webpack-rails def webpack_asset_paths(plugin_name, extension: 'js') - if extension == 'js' + case extension + when 'js' Foreman::Deprecation.deprecation_warning('3.12', '`webpack_asset_paths` is deprecated, use `content_for(:javascripts) { webpacked_plugins_js_for(plugin_name) }` instead.') [{ source: 'webpack_asset_paths', webpacked: webpacked_plugins_js_for(plugin_name.to_sym), }] - elsif extension == 'css' + when 'css' Foreman::Deprecation.deprecation_warning('3.12', '`webpack_asset_paths` is deprecated and not needed for css assets.') nil end diff --git a/app/helpers/templates_helper.rb b/app/helpers/templates_helper.rb index e8ae7b1af18..011a7d06e74 100644 --- a/app/helpers/templates_helper.rb +++ b/app/helpers/templates_helper.rb @@ -75,10 +75,11 @@ def template_input_f(f, options = {}) textarea_f(f, :value, options.merge(rows: 2, class: input.hidden_value? ? 'masked-input' : '')) else input_type = input.value_type - if input_type == 'date' + case input_type + when 'date' input_type = 'dateTime' options[:label_help] ||= 'Format is yyyy-MM-dd HH-mm-ss' - elsif input_type == 'search' + when 'search' input_type = 'autocomplete' resource_type = input.resource_type&.tableize options[:data] = { diff --git a/app/models/compute_resources/foreman/model/vmware.rb b/app/models/compute_resources/foreman/model/vmware.rb index 02be3f1160c..bea826c8aa6 100644 --- a/app/models/compute_resources/foreman/model/vmware.rb +++ b/app/models/compute_resources/foreman/model/vmware.rb @@ -794,10 +794,11 @@ def client :vsphere_expected_pubkey_hash => pubkey_hash ) rescue => e - if e.message =~ /The remote system presented a public key with hash (\w+) but we're expecting a hash of/ + case e.message + when /The remote system presented a public key with hash (\w+) but we're expecting a hash of/ raise Foreman::FingerprintException.new( N_("The remote system presented a public key with hash %s but we're expecting a different hash. If you are sure the remote system is authentic, go to the compute resource edit page, press the 'Test Connection' or 'Load Datacenters' button and submit"), Regexp.last_match(1)) - elsif e.message =~ /Cannot complete login due to an incorrect user name or password./ + when /Cannot complete login due to an incorrect user name or password./ raise Foreman::UsernameOrPasswordException.new( N_("Can not load datacenters due to an incorrect user name or password.")) else diff --git a/app/models/operatingsystems/windows.rb b/app/models/operatingsystems/windows.rb index 337dada8890..eca850063bf 100644 --- a/app/models/operatingsystems/windows.rb +++ b/app/models/operatingsystems/windows.rb @@ -25,9 +25,10 @@ def boot_files_uri(medium_provider) pxe_dir = "" PXEFILES.values.collect do |img| - if img =~ /boot.sdi/i || img =~ /bcd/i + case img + when /boot.sdi/i, /bcd/i pxe_dir = "boot" - elsif img =~ /boot.wim/i + when /boot.wim/i pxe_dir = "sources" else pxe_dir = "" diff --git a/app/models/taxonomy.rb b/app/models/taxonomy.rb index c95da775c99..3cf4ffadbd3 100644 --- a/app/models/taxonomy.rb +++ b/app/models/taxonomy.rb @@ -61,9 +61,10 @@ def self.inherited(child) default_scope -> { order(:title) } scope :completer_scope, lambda { |opts| - if opts[:controller] == 'organizations' + case opts[:controller] + when 'organizations' Organization.completer_scope opts - elsif opts[:controller] == 'locations' + when 'locations' Location.completer_scope opts end } diff --git a/app/models/usergroup_member.rb b/app/models/usergroup_member.rb index c5e606cd0cb..860f321e0d8 100644 --- a/app/models/usergroup_member.rb +++ b/app/models/usergroup_member.rb @@ -113,9 +113,10 @@ def find_all_affected_users end def find_all_affected_users_for(member) - if member.is_a?(User) + case member + when User [member] - elsif member.is_a?(Usergroup) + when Usergroup [member.users + member.usergroups.map { |g| find_all_affected_users_for(g) }] else raise ArgumentError, "Unknown member type #{member}" @@ -130,9 +131,10 @@ def find_all_affected_memberships end def find_all_affected_memberships_for(member, direction = :usergroups) - if member.is_a?(User) + case member + when User [member.usergroup_member] - elsif member.is_a?(Usergroup) + when Usergroup [member.usergroup_members.user_memberships + member.send(direction).map { |g| find_all_affected_memberships_for(g, direction) }] else diff --git a/app/presenters/setting_presenter.rb b/app/presenters/setting_presenter.rb index d3e49d2eca1..508b3c725ca 100644 --- a/app/presenters/setting_presenter.rb +++ b/app/presenters/setting_presenter.rb @@ -85,12 +85,13 @@ def matches_search_query?(query) raise ::Foreman::Exception.new N_('Unsupported search operators :and / :or') end - if query =~ /name\s*=\s*(\S+)/ + case query + when /name\s*=\s*(\S+)/ name == tokenized.last || full_name == tokenized.last - elsif query =~ /name\s*~\s*(\S+)/ + when /name\s*~\s*(\S+)/ search_value = tokenized.last name.include?(search_value) || full_name&.include?(search_value) - elsif query =~ /description\s*~\s*(\S+)/ + when /description\s*~\s*(\S+)/ search_value = tokenized.last description.include? search_value else diff --git a/app/registries/foreman/setting_manager.rb b/app/registries/foreman/setting_manager.rb index 7262d7bde91..e262ec80d55 100644 --- a/app/registries/foreman/setting_manager.rb +++ b/app/registries/foreman/setting_manager.rb @@ -139,9 +139,10 @@ def setting(name, default:, description:, type:, full_name: nil, collection: nil end def _inline_validates(name, validations) - if validations.is_a?(Regexp) + case validations + when Regexp validations = { format: { with: validations } } - elsif validations.is_a?(Symbol) + when Symbol validations = { validations => true } end validates(name, validations) diff --git a/app/services/foreman/parameter_filter.rb b/app/services/foreman/parameter_filter.rb index 46d0b570bde..5f7c7ec6c1c 100644 --- a/app/services/foreman/parameter_filter.rb +++ b/app/services/foreman/parameter_filter.rb @@ -82,11 +82,12 @@ def accessible_attributes(context) private def expand_nested(filter, context) - if filter.is_a?(ParameterFilter) + case filter + when ParameterFilter filter.filter(Context.new(:nested, context.controller_name, context.action)) - elsif filter.is_a?(Hash) + when Hash filter.transform_values { |v| expand_nested(v, context) } - elsif filter.is_a?(Array) + when Array filter.map { |v| expand_nested(v, context) } else filter diff --git a/app/services/foreman/parameters/caster.rb b/app/services/foreman/parameters/caster.rb index 0c9cf8df1a4..492fda5cc81 100644 --- a/app/services/foreman/parameters/caster.rb +++ b/app/services/foreman/parameters/caster.rb @@ -64,11 +64,12 @@ def cast_integer return value.to_i if value.is_a?(Numeric) if value.is_a?(String) - if value =~ /^0x[0-9a-f]+$/i + case value + when /^0x[0-9a-f]+$/i value.to_i(16) - elsif value =~ /^0[0-7]+$/ + when /^0[0-7]+$/ value.to_i(8) - elsif value =~ /^-?\d+$/ + when /^-?\d+$/ value.to_i else raise TypeError diff --git a/app/services/name_synchronizer.rb b/app/services/name_synchronizer.rb index 91715c41c8b..d63511ade6d 100644 --- a/app/services/name_synchronizer.rb +++ b/app/services/name_synchronizer.rb @@ -1,9 +1,10 @@ class NameSynchronizer def initialize(object) - if object.is_a?(Host::Base) + case object + when Host::Base @host = object @interface = @host.primary_interface - elsif object.is_a?(Nic::Base) + when Nic::Base @interface = object @host = @interface.host else diff --git a/app/services/setting_registry.rb b/app/services/setting_registry.rb index 00c81b7a081..6bb201dbc9e 100644 --- a/app/services/setting_registry.rb +++ b/app/services/setting_registry.rb @@ -34,10 +34,11 @@ def complete_value def complete_value_from_db(field, val) count = 20 - if field.field == :name + case field.field + when :name results = @registry.filter_map { |set| ((set.full_name =~ /\s/) ? "\"#{set.full_name.gsub('"', '\"')}\"" : set.full_name) if set.name.include?(val) || set.full_name&.include?(val) } results.first(count) - elsif field.field == :description + when :description [] else raise ScopedSearch::QueryNotSupported, "Value '#{val}' is not valid for field '#{field.field}'" diff --git a/config/initializers/active_support_extensions.rb b/config/initializers/active_support_extensions.rb index c5614bc8a7e..e25d983d938 100644 --- a/config/initializers/active_support_extensions.rb +++ b/config/initializers/active_support_extensions.rb @@ -11,13 +11,14 @@ def deep_to_hash end def deep_convert_value(value, options = {}) - if value.is_a? HashWithIndifferentAccess + case value + when HashWithIndifferentAccess if options[:for] == :to_hash value.deep_to_hash else value.nested_under_indifferent_access end - elsif value.is_a?(Array) + when Array value = value.dup unless options[:for] == :assignment value.map! { |e| deep_convert_value(e, options) } else diff --git a/lib/fog_extensions/openstack/server.rb b/lib/fog_extensions/openstack/server.rb index 31d8d51fe14..a0aa2695659 100644 --- a/lib/fog_extensions/openstack/server.rb +++ b/lib/fog_extensions/openstack/server.rb @@ -10,9 +10,10 @@ def to_s end def start - if state.downcase == 'paused' + case state.downcase + when 'paused' service.unpause_server(id) - elsif state.downcase == 'suspended' + when 'suspended' service.resume_server(id) else service.start_server(id) diff --git a/test/unit/tasks/seeds_test.rb b/test/unit/tasks/seeds_test.rb index da4e6ccbd17..071b7685427 100644 --- a/test/unit/tasks/seeds_test.rb +++ b/test/unit/tasks/seeds_test.rb @@ -64,9 +64,10 @@ def seed(*seed_files) requirements = Template.parse_metadata(template)['require'] || [] # skip templates that require plugins that aren't available next unless SeedHelper.send(:test_template_requirements, tmpl, requirements) - if tmpl =~ /partition_tables_templates/ + case tmpl + when /partition_tables_templates/ assert Ptable.unscoped.where(:template => template).any?, "No partition table containing #{tmpl}" - elsif tmpl =~ /report_templates/ + when /report_templates/ assert ReportTemplate.unscoped.where(:template => template).any?, "No report template containing #{tmpl}" else assert ProvisioningTemplate.unscoped.where(:template => template).any?, "No template containing #{tmpl}"