From: Go MAEDA Date: Tue, 17 Jan 2023 01:38:27 +0000 (+0000) Subject: Ruby 2.7: Fix RuboCop offense Performance/MapCompact (#38134). X-Git-Tag: 5.1.0~275 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0b6f4c6811a4a4e823674e3bb8b970ee46a9194b;p=redmine.git Ruby 2.7: Fix RuboCop offense Performance/MapCompact (#38134). git-svn-id: https://svn.redmine.org/redmine/trunk@22055 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/Gemfile b/Gemfile index 44f448396..39089ade2 100644 --- a/Gemfile +++ b/Gemfile @@ -63,7 +63,7 @@ database_file = File.join(File.dirname(__FILE__), "config/database.yml") if File.exist?(database_file) yaml_config = ERB.new(IO.read(database_file)).result database_config = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(yaml_config) : YAML.load(yaml_config) - adapters = database_config.values.map {|c| c['adapter']}.compact.uniq + adapters = database_config.values.filter_map {|c| c['adapter']}.uniq if adapters.any? adapters.each do |adapter| case adapter diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4382bb3bc..6bda01088 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -405,7 +405,7 @@ class ApplicationController < ActionController::Base raise ActiveRecord::RecordNotFound if @issues.empty? raise Unauthorized unless @issues.all?(&:visible?) - @projects = @issues.collect(&:project).compact.uniq + @projects = @issues.filter_map(&:project).uniq @project = @projects.first if @projects.size == 1 rescue ActiveRecord::RecordNotFound render_404 diff --git a/app/controllers/context_menus_controller.rb b/app/controllers/context_menus_controller.rb index 848e8de2a..b3f775156 100644 --- a/app/controllers/context_menus_controller.rb +++ b/app/controllers/context_menus_controller.rb @@ -74,7 +74,7 @@ class ContextMenusController < ApplicationController @time_entry = @time_entries.first end - @projects = @time_entries.collect(&:project).compact.uniq + @projects = @time_entries.filter_map(&:project).uniq @project = @projects.first if @projects.size == 1 @activities = @projects.map(&:activities).reduce(:&) diff --git a/app/controllers/timelog_controller.rb b/app/controllers/timelog_controller.rb index 46c278664..d4caca0f6 100644 --- a/app/controllers/timelog_controller.rb +++ b/app/controllers/timelog_controller.rb @@ -280,7 +280,7 @@ class TimelogController < ApplicationController raise ActiveRecord::RecordNotFound if @time_entries.empty? raise Unauthorized unless @time_entries.all? {|t| t.editable_by?(User.current)} - @projects = @time_entries.collect(&:project).compact.uniq + @projects = @time_entries.filter_map(&:project).uniq @project = @projects.first if @projects.size == 1 rescue ActiveRecord::RecordNotFound render_404 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 72d9d8efd..43f32fbc7 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1503,7 +1503,7 @@ module ApplicationHelper # Render the error messages for the given objects def error_messages_for(*objects) - objects = objects.map {|o| o.is_a?(String) ? instance_variable_get("@#{o}") : o}.compact + objects = objects.filter_map {|o| o.is_a?(String) ? instance_variable_get("@#{o}") : o} errors = objects.map {|o| o.errors.full_messages}.flatten render_error_messages(errors) end diff --git a/app/helpers/issue_relations_helper.rb b/app/helpers/issue_relations_helper.rb index 02ff9d427..4323ea730 100644 --- a/app/helpers/issue_relations_helper.rb +++ b/app/helpers/issue_relations_helper.rb @@ -33,7 +33,7 @@ module IssueRelationsHelper end messages.map do |message, items| - ids = items.map(&:issue_to_id).compact + ids = items.filter_map(&:issue_to_id) if ids.empty? message else diff --git a/app/helpers/members_helper.rb b/app/helpers/members_helper.rb index e7e2e09e0..0e542768c 100644 --- a/app/helpers/members_helper.rb +++ b/app/helpers/members_helper.rb @@ -50,13 +50,13 @@ module MembersHelper # Returns inheritance information for an inherited member role def render_role_inheritance(member, role) - content = member.role_inheritance(role).map do |h| + content = member.role_inheritance(role).filter_map do |h| if h.is_a?(Project) l(:label_inherited_from_parent_project) elsif h.is_a?(Group) l(:label_inherited_from_group, :name => h.name.to_s) end - end.compact.uniq + end.uniq if content.present? content_tag('em', content.join(", "), :class => "info") diff --git a/app/helpers/queries_helper.rb b/app/helpers/queries_helper.rb index 86b647b83..d40ee2587 100644 --- a/app/helpers/queries_helper.rb +++ b/app/helpers/queries_helper.rb @@ -233,7 +233,7 @@ module QueriesHelper value = column.value_object(item) content = if value.is_a?(Array) - values = value.collect {|v| column_value(column, item, v)}.compact + values = value.filter_map {|v| column_value(column, item, v)} safe_join(values, ', ') else column_value(column, item, value) @@ -286,7 +286,7 @@ module QueriesHelper def csv_content(column, item) value = column.value_object(item) if value.is_a?(Array) - value.collect {|v| csv_value(column, item, v)}.compact.join(', ') + value.filter_map {|v| csv_value(column, item, v)}.join(', ') else csv_value(column, item, value) end diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index 2a338799e..719761265 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -52,7 +52,7 @@ module RepositoriesHelper end def render_changeset_changes - changes = @changeset.filechanges.limit(1000).reorder('path').collect do |change| + changes = @changeset.filechanges.limit(1000).reorder('path').filter_map do |change| case change.action when 'A' # Detects moved/copied files @@ -66,7 +66,7 @@ module RepositoriesHelper else change end - end.compact + end tree = {} changes.each do |change| diff --git a/app/models/member.rb b/app/models/member.rb index 46a81afc0..21c623408 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -120,8 +120,7 @@ class Member < ActiveRecord::Base def role_inheritance(role) member_roles. select {|mr| mr.role_id == role.id && mr.inherited_from.present?}. - map {|mr| mr.inherited_from_member_role.try(:member)}. - compact. + filter_map {|mr| mr.inherited_from_member_role.try(:member)}. map {|m| m.project == project ? m.principal : m.project} end diff --git a/app/models/query.rb b/app/models/query.rb index e25824bd0..bd670205b 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -787,9 +787,9 @@ class Query < ActiveRecord::Base return [] if available_columns.empty? # preserve the column_names order - cols = (has_default_columns? ? default_columns_names : column_names).collect do |name| + cols = (has_default_columns? ? default_columns_names : column_names).filter_map do |name| available_columns.find {|col| col.name == name} - end.compact + end available_columns.select(&:frozen?) | cols end diff --git a/app/models/repository.rb b/app/models/repository.rb index dc7783aff..be10fa3cf 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -436,7 +436,7 @@ class Repository < ActiveRecord::Base # commits.to_a.sort! {|x, y| x.last <=> y.last} changes = Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", id). select("committer, user_id, count(*) as count").group("committer, user_id") - user_ids = changesets.map(&:user_id).compact.uniq + user_ids = changesets.filter_map(&:user_id).uniq authors_names = User.where(:id => user_ids).inject({}) do |memo, user| memo[user.id] = user.to_s memo diff --git a/app/models/repository/git.rb b/app/models/repository/git.rb index 2463ab80a..2c77c173d 100644 --- a/app/models/repository/git.rb +++ b/app/models/repository/git.rb @@ -213,7 +213,7 @@ class Repository::Git < Repository private :save_revisions def save_revision(rev) - parents = (rev.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact + parents = (rev.parents || []).filter_map{|rp| find_changeset_by_name(rp)} changeset = Changeset.create( :repository => self, diff --git a/app/models/repository/mercurial.rb b/app/models/repository/mercurial.rb index 30cd59fd5..a152950c9 100644 --- a/app/models/repository/mercurial.rb +++ b/app/models/repository/mercurial.rb @@ -193,9 +193,9 @@ class Repository::Mercurial < Repository (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i| scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re| transaction do - parents = (re.parents || []).collect do |rp| + parents = (re.parents || []).filter_map do |rp| find_changeset_by_name(scmid_for_inserting_db(rp)) - end.compact + end cs = Changeset.create(:repository => self, :revision => re.revision, :scmid => scmid_for_inserting_db(re.scmid), diff --git a/app/models/repository/subversion.rb b/app/models/repository/subversion.rb index c6355cff1..373d250bc 100644 --- a/app/models/repository/subversion.rb +++ b/app/models/repository/subversion.rb @@ -42,7 +42,7 @@ class Repository::Subversion < Repository def latest_changesets(path, rev, limit=10) revisions = scm.revisions(path, rev, nil, :limit => limit) if revisions - identifiers = revisions.collect(&:identifier).compact + identifiers = revisions.filter_map(&:identifier) changesets.where(:revision => identifiers).reorder("committed_on DESC").includes(:repository, :user).to_a else [] @@ -97,7 +97,7 @@ class Repository::Subversion < Repository entries_with_identifier = entries.select {|entry| entry.lastrev && entry.lastrev.identifier.present?} - identifiers = entries_with_identifier.map {|entry| entry.lastrev.identifier}.compact.uniq + identifiers = entries_with_identifier.filter_map {|entry| entry.lastrev.identifier}.uniq if identifiers.any? changesets_by_identifier = changesets.where(:revision => identifiers). diff --git a/app/models/role.rb b/app/models/role.rb index b73f97d4c..e67f30054 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -122,7 +122,7 @@ class Role < ActiveRecord::Base end def permissions=(perms) - perms = perms.collect {|p| p.to_sym unless p.blank?}.compact.uniq if perms + perms = perms.filter_map {|p| p.to_sym unless p.blank?}.uniq if perms write_attribute(:permissions, perms) end diff --git a/lib/redmine/field_format.rb b/lib/redmine/field_format.rb index 55800dd6b..e74dc20d7 100644 --- a/lib/redmine/field_format.rb +++ b/lib/redmine/field_format.rb @@ -868,7 +868,7 @@ module Redmine def possible_values_records(custom_field, object=nil) if object.is_a?(Array) - projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq + projects = object.filter_map {|o| o.respond_to?(:project) ? o.project : nil}.uniq projects.map {|project| possible_values_records(custom_field, project)}.reduce(:&) || [] elsif object.respond_to?(:project) && object.project scope = object.project.users @@ -934,7 +934,7 @@ module Redmine def possible_values_records(custom_field, object=nil, all_statuses=false) if object.is_a?(Array) - projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq + projects = object.filter_map {|o| o.respond_to?(:project) ? o.project : nil}.uniq projects.map {|project| possible_values_records(custom_field, project)}.reduce(:&) || [] elsif object.respond_to?(:project) && object.project scope = object.project.shared_versions diff --git a/lib/redmine/helpers/gantt.rb b/lib/redmine/helpers/gantt.rb index 448d3190e..3614829db 100644 --- a/lib/redmine/helpers/gantt.rb +++ b/lib/redmine/helpers/gantt.rb @@ -198,7 +198,7 @@ module Redmine # Returns the distinct versions of the issues that belong to +project+ def project_versions(project) - project_issues(project).collect(&:fixed_version).compact.uniq + project_issues(project).filter_map(&:fixed_version).uniq end # Returns the issues that belong to +project+ and are assigned to +version+ diff --git a/lib/redmine/helpers/time_report.rb b/lib/redmine/helpers/time_report.rb index 79ac6b020..c6e98ebf1 100644 --- a/lib/redmine/helpers/time_report.rb +++ b/lib/redmine/helpers/time_report.rb @@ -49,7 +49,7 @@ module Redmine @scope.includes(:activity). reorder(nil). group(@criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns). - joins(@criteria.collect{|criteria| @available_criteria[criteria][:joins]}.compact). + joins(@criteria.filter_map{|criteria| @available_criteria[criteria][:joins]}). sum(:hours).each do |hash, hours| h = {'hours' => hours} (@criteria + time_columns).each_with_index do |name, i| diff --git a/lib/redmine/my_page.rb b/lib/redmine/my_page.rb index 23885fea1..95af87a4c 100644 --- a/lib/redmine/my_page.rb +++ b/lib/redmine/my_page.rb @@ -48,11 +48,11 @@ module Redmine def self.block_options(blocks_in_use=[]) options = [] blocks.each do |block, block_options| - indexes = blocks_in_use.map do |n| + indexes = blocks_in_use.filter_map do |n| if n =~ /\A#{block}(__(\d+))?\z/ $2.to_i end - end.compact + end occurs = indexes.size block_id = indexes.any? ? "#{block}__#{indexes.max + 1}" : block diff --git a/lib/redmine/scm/adapters/abstract_adapter.rb b/lib/redmine/scm/adapters/abstract_adapter.rb index 7d211ad4f..ebd00d55f 100644 --- a/lib/redmine/scm/adapters/abstract_adapter.rb +++ b/lib/redmine/scm/adapters/abstract_adapter.rb @@ -329,7 +329,7 @@ module Redmine end def revisions - revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact) + revisions ||= Revisions.new(filter_map{|entry| entry.lastrev}) end end diff --git a/lib/redmine/search.rb b/lib/redmine/search.rb index 16c264ec5..83afe2c65 100644 --- a/lib/redmine/search.rb +++ b/lib/redmine/search.rb @@ -82,9 +82,9 @@ module Redmine klass = scope.singularize.camelcase.constantize results_by_scope[scope] += klass.search_results_from_ids(scope_and_ids.map(&:last)) end - result_ids_to_load.map do |scope, id| + result_ids_to_load.filter_map do |scope, id| results_by_scope[scope].detect {|record| record.id == id} - end.compact + end end # Returns the results ids, sorted by rank diff --git a/test/functional/issues_controller_test.rb b/test/functional/issues_controller_test.rb index 5078dbf44..81fef052f 100644 --- a/test/functional/issues_controller_test.rb +++ b/test/functional/issues_controller_test.rb @@ -1259,7 +1259,7 @@ class IssuesControllerTest < Redmine::ControllerTest get(:index, :params => {:sort => 'assigned_to'}) assert_response :success - assignees = issues_in_list.map(&:assigned_to).compact + assignees = issues_in_list.filter_map(&:assigned_to) assert_equal assignees.sort, assignees assert_select 'table.issues.sort-by-assigned-to.sort-asc' end @@ -1268,7 +1268,7 @@ class IssuesControllerTest < Redmine::ControllerTest get(:index, :params => {:sort => 'assigned_to:desc'}) assert_response :success - assignees = issues_in_list.map(&:assigned_to).compact + assignees = issues_in_list.filter_map(&:assigned_to) assert_equal assignees.sort.reverse, assignees assert_select 'table.issues.sort-by-assigned-to.sort-desc' end diff --git a/test/unit/query_test.rb b/test/unit/query_test.rb index 6c05c9b95..c2cbf6fee 100644 --- a/test/unit/query_test.rb +++ b/test/unit/query_test.rb @@ -1940,13 +1940,13 @@ class QueryTest < ActiveSupport::TestCase q.sort_criteria = [[c.name.to_s, 'asc']] issues = q.issues values = - issues.collect do |i| + issues.filter_map do |i| begin Kernel.Float(i.custom_value_for(c.custom_field).to_s) rescue nil end - end.compact + end assert !values.empty? assert_equal values.sort, values end