]> source.dussan.org Git - redmine.git/commitdiff
Optimize query models by replacing `map` with `pluck` (#39837).
authorGo MAEDA <maeda@farend.jp>
Fri, 29 Dec 2023 08:00:23 +0000 (08:00 +0000)
committerGo MAEDA <maeda@farend.jp>
Fri, 29 Dec 2023 08:00:23 +0000 (08:00 +0000)
Patch by Go MAEDA (@maeda).

git-svn-id: https://svn.redmine.org/redmine/trunk@22575 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/group.rb
app/models/issue_query.rb
app/models/query.rb
app/models/time_entry_query.rb
app/models/user_query.rb

index 1cfc6b56c82228187ca88abbccadf825391a01dd..7b2b4342a274b6ae873f3d5fb279a26f91b79167 100644 (file)
@@ -47,18 +47,12 @@ class Group < Principal
     'custom_fields',
     :if => lambda {|group, user| user.admin? && !group.builtin?})
 
+  alias_attribute :name, :lastname
+
   def to_s
     name.to_s
   end
 
-  def name
-    lastname
-  end
-
-  def name=(arg)
-    self.lastname = arg
-  end
-
   def builtin_type
     nil
   end
index 037d5a25753e2eca389ac9e93acd84160144a408..b18117cc92385ae5a9a4478aad9bc1ab678dd40d 100644 (file)
@@ -159,7 +159,8 @@ class IssueQuery < Query
     )
     add_available_filter(
       "priority_id",
-      :type => :list_with_history, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s]}
+      :type => :list_with_history,
+      :values => IssuePriority.pluck(:name, :id).map {|name, id| [name, id.to_s]}
     )
     add_available_filter(
       "author_id",
@@ -171,11 +172,13 @@ class IssueQuery < Query
     )
     add_available_filter(
       "member_of_group",
-      :type => :list_optional, :values => lambda {Group.givable.visible.collect {|g| [g.name, g.id.to_s]}}
+      :type => :list_optional,
+      :values => lambda {Group.givable.visible.pluck(:name, :id).map {|name, id| [name, id.to_s]}}
     )
     add_available_filter(
       "assigned_to_role",
-      :type => :list_optional, :values => lambda {Role.givable.collect {|r| [r.name, r.id.to_s]}}
+      :type => :list_optional,
+      :values => lambda {Role.givable.pluck(:name, :id).map {|name, id| [name, id.to_s]}}
     )
     add_available_filter(
       "fixed_version_id",
@@ -195,7 +198,7 @@ class IssueQuery < Query
     add_available_filter(
       "category_id",
       :type => :list_optional_with_history,
-      :values => lambda {project.issue_categories.collect{|s| [s.name, s.id.to_s]}}
+      :values => lambda {project.issue_categories.pluck(:name, :id).map {|name, id| [name, id.to_s]}}
     ) if project
     add_available_filter "subject", :type => :text
     add_available_filter "description", :type => :text
index 31ffe6a13d4a5764167baf9b5a05674c121055fe..9f3ce4c086eeef0e7e7e87be98cf17184a56b777 100644 (file)
@@ -589,7 +589,7 @@ class Query < ActiveRecord::Base
   end
 
   def subproject_values
-    project.descendants.visible.collect{|s| [s.name, s.id.to_s]}
+    project.descendants.visible.pluck(:name, :id).map {|name, id| [name, id.to_s]}
   end
 
   def principals
@@ -651,7 +651,7 @@ class Query < ActiveRecord::Base
     else
       statuses = IssueStatus.all.sorted
     end
-    statuses.collect{|s| [s.name, s.id.to_s]}
+    statuses.pluck(:name, :id).map {|name, id| [name, id.to_s]}
   end
 
   def watcher_values
@@ -996,7 +996,7 @@ class Query < ActiveRecord::Base
 
       if field == 'project_id' || (self.type == 'ProjectQuery' && %w[id parent_id].include?(field))
         if v.delete('mine')
-          v += User.current.memberships.map {|m| m.project_id.to_s}
+          v += User.current.memberships.pluck(:project_id).map(&:to_s)
         end
         if v.delete('bookmarks')
           v += User.current.bookmarked_project_ids
index 28cf11438f86f4f10e6abd93abaf741a3c395c9e..bd7b72a7ae08e1a0fbb1424041a66f980e9f6b7d 100644 (file)
@@ -80,7 +80,7 @@ class TimeEntryQuery < Query
       "issue.category_id",
       :type => :list_optional,
       :name => l("label_attribute_of_issue", :name => l(:field_category)),
-      :values => lambda {project.issue_categories.collect{|s| [s.name, s.id.to_s]}}
+      :values => lambda {project.issue_categories.pluck(:name, :id).map {|name, id| [name, id.to_s]}}
     ) if project
     add_available_filter(
       "issue.subject",
index 2ee71cc5454dd5a7541ba6c447b58b2ac786d965..3e1cb2a74031dd2ce97eb4f21c75862baa905481 100644 (file)
@@ -45,7 +45,7 @@ class UserQuery < Query
       type: :list_optional, values: ->{ auth_sources_values }
     add_available_filter "is_member_of_group",
       type: :list_optional,
-      values: ->{ Group.givable.visible.map {|g| [g.name, g.id.to_s] } }
+      values: ->{ Group.givable.visible.pluck(:name, :id).map {|name, id| [name, id.to_s]} }
     if Setting.twofa?
       add_available_filter "twofa_scheme",
         type: :list_optional,
@@ -65,9 +65,7 @@ class UserQuery < Query
   end
 
   def auth_sources_values
-    AuthSource.order(name: :asc).to_a.map do |auth_source|
-      [auth_source.name, auth_source.id]
-    end
+    AuthSource.order(name: :asc).pluck(:name, :id)
   end
 
   def user_statuses_values
@@ -131,7 +129,7 @@ class UserQuery < Query
 
   def sql_for_is_member_of_group_field(field, operator, value)
     if ["*", "!*"].include? operator
-      value = Group.givable.map(&:id)
+      value = Group.givable.ids
     end
 
     e = operator.start_with?("!") ? "NOT EXISTS" : "EXISTS"