summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorRubén Dávila <rdavila84@gmail.com>2016-02-22 19:08:00 -0500
committerRubén Dávila <rdavila84@gmail.com>2016-03-04 22:37:03 -0500
commita056dfa9a077def4c3ffb958d3f86f7c9d7c2096 (patch)
treed244e57faf58dc2be08c1412c66874c694f8913e /app/models
parent3231ea10b7319f6fe50c0ec1407ddaac69089641 (diff)
downloadgitlab-ce-a056dfa9a077def4c3ffb958d3f86f7c9d7c2096.tar.gz
Refactor GlobalMilestone queries.
Make methods return ActiveRecord Relations instead of Arrays.
Diffstat (limited to 'app/models')
-rw-r--r--app/models/concerns/issuable.rb1
-rw-r--r--app/models/global_milestone.rb26
-rw-r--r--app/models/merge_request.rb1
-rw-r--r--app/models/project.rb1
4 files changed, 15 insertions, 14 deletions
diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb
index 286d6655861..a3c4a3d2776 100644
--- a/app/models/concerns/issuable.rb
+++ b/app/models/concerns/issuable.rb
@@ -29,6 +29,7 @@ module Issuable
scope :assigned, -> { where("assignee_id IS NOT NULL") }
scope :unassigned, -> { where("assignee_id IS NULL") }
scope :of_projects, ->(ids) { where(project_id: ids) }
+ scope :of_milestones, ->(ids) { where(milestone_id: ids) }
scope :opened, -> { with_state(:opened, :reopened) }
scope :only_opened, -> { with_state(:opened) }
scope :only_reopened, -> { with_state(:reopened) }
diff --git a/app/models/global_milestone.rb b/app/models/global_milestone.rb
index 7ee276255a0..e4dd90b631e 100644
--- a/app/models/global_milestone.rb
+++ b/app/models/global_milestone.rb
@@ -28,27 +28,27 @@ class GlobalMilestone
end
def projects
- milestones.map { |milestone| milestone.project }
+ @projects ||= Project.for_milestones(milestones.map(&:id))
end
- def issue_count
- milestones.map { |milestone| milestone.issues.count }.sum
+ def issues_count
+ issues.count
end
def merge_requests_count
- milestones.map { |milestone| milestone.merge_requests.count }.sum
+ merge_requests.count
end
def open_items_count
- milestones.map { |milestone| milestone.open_items_count }.sum
+ opened_issues.count + opened_merge_requests.count
end
def closed_items_count
- milestones.map { |milestone| milestone.closed_items_count }.sum
+ closed_issues.count + closed_merge_requests.count
end
def total_items_count
- milestones.map { |milestone| milestone.total_items_count }.sum
+ issues_count + merge_requests_count
end
def percent_complete
@@ -76,11 +76,11 @@ class GlobalMilestone
end
def issues
- @issues ||= milestones.map(&:issues).flatten.group_by(&:state)
+ @issues ||= Issue.of_milestones(milestones.map(&:id))
end
def merge_requests
- @merge_requests ||= milestones.map(&:merge_requests).flatten.group_by(&:state)
+ @merge_requests ||= MergeRequest.of_milestones(milestones.map(&:id))
end
def participants
@@ -88,19 +88,19 @@ class GlobalMilestone
end
def opened_issues
- issues.values_at("opened", "reopened").compact.flatten
+ issues.opened
end
def closed_issues
- issues['closed']
+ issues.closed
end
def opened_merge_requests
- merge_requests.values_at("opened", "reopened").compact.flatten
+ merge_requests.opened
end
def closed_merge_requests
- merge_requests.values_at("closed", "merged", "locked").compact.flatten
+ merge_requests.with_states(:closed, :merged, :locked)
end
def complete?
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 025b522cf66..f575494e2bf 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -139,7 +139,6 @@ class MergeRequest < ActiveRecord::Base
scope :of_projects, ->(ids) { where(target_project_id: ids) }
scope :opened, -> { with_states(:opened, :reopened) }
scope :merged, -> { with_state(:merged) }
- scope :closed, -> { with_state(:closed) }
scope :closed_and_merged, -> { with_states(:closed, :merged) }
scope :join_project, -> { joins(:target_project) }
diff --git a/app/models/project.rb b/app/models/project.rb
index 148eab692ff..3a28d5d7fee 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -215,6 +215,7 @@ class Project < ActiveRecord::Base
scope :public_only, -> { where(visibility_level: Project::PUBLIC) }
scope :public_and_internal_only, -> { where(visibility_level: Project.public_and_internal_levels) }
scope :non_archived, -> { where(archived: false) }
+ scope :for_milestones, ->(ids) { joins(:milestones).where('milestones.id' => ids) }
state_machine :import_status, initial: :none do
event :import_start do