Skip to content

Commit

Permalink
Fixes #37458 - Fix Style/CaseLikeIf cop
Browse files Browse the repository at this point in the history
  • Loading branch information
archanaserver authored and ekohl committed Aug 1, 2024
1 parent d7a8a1e commit 2f947f7
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 47 deletions.
5 changes: 3 additions & 2 deletions app/controllers/filters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions app/helpers/audits_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
5 changes: 3 additions & 2 deletions app/helpers/compute_resources_vms_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions app/helpers/hosts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/helpers/layout_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/helpers/templates_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down
5 changes: 3 additions & 2 deletions app/models/compute_resources/foreman/model/vmware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/models/operatingsystems/windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
5 changes: 3 additions & 2 deletions app/models/taxonomy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions app/models/usergroup_member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions app/presenters/setting_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/registries/foreman/setting_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions app/services/foreman/parameter_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions app/services/foreman/parameters/caster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/services/name_synchronizer.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/services/setting_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}'"
Expand Down
5 changes: 3 additions & 2 deletions config/initializers/active_support_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/fog_extensions/openstack/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions test/unit/tasks/seeds_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 2f947f7

Please sign in to comment.