Commit a056dfa9 authored by Rubén Dávila's avatar Rubén Dávila

Refactor GlobalMilestone queries.

Make methods return ActiveRecord Relations instead of Arrays.
parent 3231ea10
...@@ -29,6 +29,7 @@ module Issuable ...@@ -29,6 +29,7 @@ module Issuable
scope :assigned, -> { where("assignee_id IS NOT NULL") } scope :assigned, -> { where("assignee_id IS NOT NULL") }
scope :unassigned, -> { where("assignee_id IS NULL") } scope :unassigned, -> { where("assignee_id IS NULL") }
scope :of_projects, ->(ids) { where(project_id: ids) } scope :of_projects, ->(ids) { where(project_id: ids) }
scope :of_milestones, ->(ids) { where(milestone_id: ids) }
scope :opened, -> { with_state(:opened, :reopened) } scope :opened, -> { with_state(:opened, :reopened) }
scope :only_opened, -> { with_state(:opened) } scope :only_opened, -> { with_state(:opened) }
scope :only_reopened, -> { with_state(:reopened) } scope :only_reopened, -> { with_state(:reopened) }
......
...@@ -28,27 +28,27 @@ class GlobalMilestone ...@@ -28,27 +28,27 @@ class GlobalMilestone
end end
def projects def projects
milestones.map { |milestone| milestone.project } @projects ||= Project.for_milestones(milestones.map(&:id))
end end
def issue_count def issues_count
milestones.map { |milestone| milestone.issues.count }.sum issues.count
end end
def merge_requests_count def merge_requests_count
milestones.map { |milestone| milestone.merge_requests.count }.sum merge_requests.count
end end
def open_items_count def open_items_count
milestones.map { |milestone| milestone.open_items_count }.sum opened_issues.count + opened_merge_requests.count
end end
def closed_items_count def closed_items_count
milestones.map { |milestone| milestone.closed_items_count }.sum closed_issues.count + closed_merge_requests.count
end end
def total_items_count def total_items_count
milestones.map { |milestone| milestone.total_items_count }.sum issues_count + merge_requests_count
end end
def percent_complete def percent_complete
...@@ -76,11 +76,11 @@ class GlobalMilestone ...@@ -76,11 +76,11 @@ class GlobalMilestone
end end
def issues def issues
@issues ||= milestones.map(&:issues).flatten.group_by(&:state) @issues ||= Issue.of_milestones(milestones.map(&:id))
end end
def merge_requests def merge_requests
@merge_requests ||= milestones.map(&:merge_requests).flatten.group_by(&:state) @merge_requests ||= MergeRequest.of_milestones(milestones.map(&:id))
end end
def participants def participants
...@@ -88,19 +88,19 @@ class GlobalMilestone ...@@ -88,19 +88,19 @@ class GlobalMilestone
end end
def opened_issues def opened_issues
issues.values_at("opened", "reopened").compact.flatten issues.opened
end end
def closed_issues def closed_issues
issues['closed'] issues.closed
end end
def opened_merge_requests def opened_merge_requests
merge_requests.values_at("opened", "reopened").compact.flatten merge_requests.opened
end end
def closed_merge_requests def closed_merge_requests
merge_requests.values_at("closed", "merged", "locked").compact.flatten merge_requests.with_states(:closed, :merged, :locked)
end end
def complete? def complete?
......
...@@ -139,7 +139,6 @@ class MergeRequest < ActiveRecord::Base ...@@ -139,7 +139,6 @@ class MergeRequest < ActiveRecord::Base
scope :of_projects, ->(ids) { where(target_project_id: ids) } scope :of_projects, ->(ids) { where(target_project_id: ids) }
scope :opened, -> { with_states(:opened, :reopened) } scope :opened, -> { with_states(:opened, :reopened) }
scope :merged, -> { with_state(:merged) } scope :merged, -> { with_state(:merged) }
scope :closed, -> { with_state(:closed) }
scope :closed_and_merged, -> { with_states(:closed, :merged) } scope :closed_and_merged, -> { with_states(:closed, :merged) }
scope :join_project, -> { joins(:target_project) } scope :join_project, -> { joins(:target_project) }
......
...@@ -215,6 +215,7 @@ class Project < ActiveRecord::Base ...@@ -215,6 +215,7 @@ class Project < ActiveRecord::Base
scope :public_only, -> { where(visibility_level: Project::PUBLIC) } scope :public_only, -> { where(visibility_level: Project::PUBLIC) }
scope :public_and_internal_only, -> { where(visibility_level: Project.public_and_internal_levels) } scope :public_and_internal_only, -> { where(visibility_level: Project.public_and_internal_levels) }
scope :non_archived, -> { where(archived: false) } scope :non_archived, -> { where(archived: false) }
scope :for_milestones, ->(ids) { joins(:milestones).where('milestones.id' => ids) }
state_machine :import_status, initial: :none do state_machine :import_status, initial: :none do
event :import_start do event :import_start do
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
.row .row
.col-sm-6 .col-sm-6
= link_to issues_dashboard_path(milestone_title: milestone.title) do = link_to issues_dashboard_path(milestone_title: milestone.title) do
= pluralize milestone.issue_count, 'Issue' = pluralize milestone.issues_count, 'Issue'
&middot; &middot;
= link_to merge_requests_dashboard_path(milestone_title: milestone.title) do = link_to merge_requests_dashboard_path(milestone_title: milestone.title) do
= pluralize milestone.merge_requests_count, 'Merge Request' = pluralize milestone.merge_requests_count, 'Merge Request'
......
...@@ -52,7 +52,7 @@ ...@@ -52,7 +52,7 @@
%li.active %li.active
= link_to '#tab-issues', 'data-toggle' => 'tab' do = link_to '#tab-issues', 'data-toggle' => 'tab' do
Issues Issues
%span.badge= @milestone.issue_count %span.badge= @milestone.issues_count
%li %li
= link_to '#tab-merge-requests', 'data-toggle' => 'tab' do = link_to '#tab-merge-requests', 'data-toggle' => 'tab' do
Merge Requests Merge Requests
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
.row .row
.col-sm-6 .col-sm-6
= link_to issues_group_path(@group, milestone_title: milestone.title) do = link_to issues_group_path(@group, milestone_title: milestone.title) do
= pluralize milestone.issue_count, 'Issue' = pluralize milestone.issues_count, 'Issue'
&middot; &middot;
= link_to merge_requests_group_path(@group, milestone_title: milestone.title) do = link_to merge_requests_group_path(@group, milestone_title: milestone.title) do
= pluralize milestone.merge_requests_count, 'Merge Request' = pluralize milestone.merge_requests_count, 'Merge Request'
......
...@@ -58,7 +58,7 @@ ...@@ -58,7 +58,7 @@
%li.active %li.active
= link_to '#tab-issues', 'data-toggle' => 'tab' do = link_to '#tab-issues', 'data-toggle' => 'tab' do
Issues Issues
%span.badge= @milestone.issue_count %span.badge= @milestone.issues_count
%li %li
= link_to '#tab-merge-requests', 'data-toggle' => 'tab' do = link_to '#tab-merge-requests', 'data-toggle' => 'tab' do
Merge Requests Merge Requests
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment