diff options
author | Heinrich Lee Yu <heinrich@gitlab.com> | 2018-12-17 20:59:23 +0800 |
---|---|---|
committer | Heinrich Lee Yu <hleeyu@gmail.com> | 2018-12-31 11:09:17 +0800 |
commit | 2490cfeeb2f8426b1a8f4e24bd0297e41a870ca2 (patch) | |
tree | e91cad8148af518f45f0e1e7fb021e4622b785b3 /app | |
parent | 030c7068fc8127e1ff8afa7c8daffee034c7b929 (diff) | |
download | gitlab-ce-2490cfeeb2f8426b1a8f4e24bd0297e41a870ca2.tar.gz |
Refactor Milestone.for_projects_and_groups
Refactored to use Rails 5 ActiveRecord `or` so that it can handle ids,
objects, array of objects, or relations properly.
Diffstat (limited to 'app')
-rw-r--r-- | app/finders/issuable_finder.rb | 2 | ||||
-rw-r--r-- | app/models/milestone.rb | 13 |
2 files changed, 8 insertions, 7 deletions
diff --git a/app/finders/issuable_finder.rb b/app/finders/issuable_finder.rb index a144db9fcd1..1a69ec85d18 100644 --- a/app/finders/issuable_finder.rb +++ b/app/finders/issuable_finder.rb @@ -155,7 +155,7 @@ class IssuableFinder elsif group [group] elsif current_user - Gitlab::GroupHierarchy.new(current_user.authorized_groups, current_user.groups).all_groups + Gitlab::ObjectHierarchy.new(current_user.authorized_groups, current_user.groups).all_objects else [] end diff --git a/app/models/milestone.rb b/app/models/milestone.rb index 30b5884d405..7c397f3499f 100644 --- a/app/models/milestone.rb +++ b/app/models/milestone.rb @@ -38,13 +38,14 @@ class Milestone < ActiveRecord::Base scope :closed, -> { with_state(:closed) } scope :for_projects, -> { where(group: nil).includes(:project) } - scope :for_projects_and_groups, -> (project_ids, group_ids) do - conditions = [] + scope :for_projects_and_groups, -> (projects, groups) do + projects = projects.compact if projects.is_a? Array + projects = [] if projects.nil? - conditions << arel_table[:project_id].in(project_ids) if project_ids&.compact&.any? - conditions << arel_table[:group_id].in(group_ids) if group_ids&.compact&.any? + groups = groups.compact if groups.is_a? Array + groups = [] if groups.nil? - where(conditions.reduce(:or)) + where(project: projects).or(where(group: groups)) end scope :order_by_name_asc, -> { order(Arel::Nodes::Ascending.new(arel_table[:title].lower)) } @@ -132,7 +133,7 @@ class Milestone < ActiveRecord::Base def self.upcoming_ids(projects, groups) rel = unscoped - .for_projects_and_groups(projects&.map(&:id), groups&.map(&:id)) + .for_projects_and_groups(projects, groups) .active.where('milestones.due_date > NOW()') if Gitlab::Database.postgresql? |