diff options
author | Heinrich Lee Yu <heinrich@gitlab.com> | 2018-12-17 15:59:23 +0300 |
---|---|---|
committer | Heinrich Lee Yu <hleeyu@gmail.com> | 2018-12-31 06:09:17 +0300 |
commit | 2490cfeeb2f8426b1a8f4e24bd0297e41a870ca2 (patch) | |
tree | e91cad8148af518f45f0e1e7fb021e4622b785b3 /app/models/milestone.rb | |
parent | 030c7068fc8127e1ff8afa7c8daffee034c7b929 (diff) |
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/models/milestone.rb')
-rw-r--r-- | app/models/milestone.rb | 13 |
1 files changed, 7 insertions, 6 deletions
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? |