]> source.dussan.org Git - redmine.git/commitdiff
Ruby 2.7: Fix RuboCop offense Performance/MapCompact (#38134).
authorGo MAEDA <maeda@farend.jp>
Tue, 17 Jan 2023 01:38:27 +0000 (01:38 +0000)
committerGo MAEDA <maeda@farend.jp>
Tue, 17 Jan 2023 01:38:27 +0000 (01:38 +0000)
git-svn-id: https://svn.redmine.org/redmine/trunk@22055 e93f8b46-1217-0410-a6f0-8f06a7374b81

24 files changed:
Gemfile
app/controllers/application_controller.rb
app/controllers/context_menus_controller.rb
app/controllers/timelog_controller.rb
app/helpers/application_helper.rb
app/helpers/issue_relations_helper.rb
app/helpers/members_helper.rb
app/helpers/queries_helper.rb
app/helpers/repositories_helper.rb
app/models/member.rb
app/models/query.rb
app/models/repository.rb
app/models/repository/git.rb
app/models/repository/mercurial.rb
app/models/repository/subversion.rb
app/models/role.rb
lib/redmine/field_format.rb
lib/redmine/helpers/gantt.rb
lib/redmine/helpers/time_report.rb
lib/redmine/my_page.rb
lib/redmine/scm/adapters/abstract_adapter.rb
lib/redmine/search.rb
test/functional/issues_controller_test.rb
test/unit/query_test.rb

diff --git a/Gemfile b/Gemfile
index 44f44839676c2a33cfa0cfd0cd8b5478a8639634..39089ade2ef6191927714ff775448039beedd881 100644 (file)
--- 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
index 4382bb3bccaf6a37bb48508d97ccb52961bc3162..6bda0108890fe874de3f5f104d212f92e3a8419d 100644 (file)
@@ -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
index 848e8de2a83da2572036975c1d1fc0e239088cbc..b3f7751567d540f601b7c99a713166a4284ed072 100644 (file)
@@ -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(:&)
 
index 46c2786647114eecfb2f3b2abb681d57992a98b2..d4caca0f6a64ce03f0f5aeee8950fa67e567cc35 100644 (file)
@@ -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
index 72d9d8efdb85e61e73484f53df9c4b4691d6dcfd..43f32fbc72790c898cd5d9d751902c9a7a9f05d3 100644 (file)
@@ -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
index 02ff9d4270c209f47ab7b966bc64569a63c3fa6b..4323ea73098432f180d9c9f19d10f4cb49c86280 100644 (file)
@@ -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
index e7e2e09e023e20e13e4ad6be57a79e16aee2c475..0e542768c3aba264540607c2e10c3c2eb6d461b1 100644 (file)
@@ -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")
index 86b647b8369a65b26892445317e21d448c0ea930..d40ee2587dd9d099e62a6e9bd6328af3e3fa87f6 100644 (file)
@@ -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
index 2a338799e95ce43b3a607c95cbd08deff67ce26c..7197612651e0be1ac6b1984273d075c54315a2c9 100644 (file)
@@ -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|
index 46a81afc071f37151029dab0122736a37f413e3b..21c62340862cad17460aceca64dd7ee942560999 100644 (file)
@@ -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
 
index e25824bd087ca8d95a5e438eee47a2e897001672..bd670205b869de4283fa0bdac7d9385eae88483d 100644 (file)
@@ -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
 
index dc7783aff8dc3242064a43afb2b1c275045e45b3..be10fa3cfa05a7937b306ac721f6e55594b38d34 100644 (file)
@@ -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
index 2463ab80a2726d03e31bb7176af35e2f97178a4f..2c77c173d8fc5f4e6467e23110833bdd28372d2e 100644 (file)
@@ -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,
index 30cd59fd54001aa34294453ea7d5d0f727aa85aa..a152950c913726b7c12baa8f7e62bef5fc049030 100644 (file)
@@ -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),
index c6355cff1c1215ad3a984f566c7ebb7afdc0fd4c..373d250bc199e283856bee84fcbf53637e14321c 100644 (file)
@@ -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).
index b73f97d4cd6248a6f9ad13293f817d1f01186a32..e67f30054da54bbe1d02abdabf7fc4cfce771425 100644 (file)
@@ -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
 
index 55800dd6bd549392a2027b2b0ee0d3763f5b5355..e74dc20d704a2b6f285c18dfcb96da964b4e82c0 100644 (file)
@@ -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
index 448d3190e2e417e8b0b00731f350995651d6412a..3614829db894553a1231efab6438c22e2e9201e4 100644 (file)
@@ -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+
index 79ac6b0203ed13f3fcd0252aaf17f295df0c1412..c6e98ebf1ca43921286b2b4975c67c301739036a 100644 (file)
@@ -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|
index 23885fea1046121e41a46d0b73d52eb7632d48c7..95af87a4c6ee78d28a755767a46f50750a8fa982 100644 (file)
@@ -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
index 7d211ad4f4257656079c79a3df44a9224914d172..ebd00d55f2950ad09fc494df2582a72f108a59a5 100644 (file)
@@ -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
 
index 16c264ec5a6fde8937fe218ece845697831a6690..83afe2c6540e8457973f6508126a0013b69260c2 100644 (file)
@@ -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
index 5078dbf449a6b0c66b76c44867ca74282208c38e..81fef052f43c521da8b20525e2174748156713e7 100644 (file)
@@ -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
index 6c05c9b955a37a35cd355e332ed2921123a42853..c2cbf6feefa110b592a597f1fb2aefbce44793cb 100644 (file)
@@ -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