Commit d96585f5 authored by Eagllus's avatar Eagllus

Moving state_count to Milestone model and related tests

By moving and improving state_count the functions in GlobalMilestone
are no longer used.
parent 074fafe9
...@@ -34,66 +34,6 @@ class GlobalMilestone ...@@ -34,66 +34,6 @@ class GlobalMilestone
new(title, child_milestones) new(title, child_milestones)
end end
def self.states_count(projects, groups = nil)
legacy_group_milestones_count = legacy_group_milestone_states_count(projects)
group_milestones_count = groups_milestone_state_count(groups)
legacy_group_milestones_count.merge(group_milestones_count) do |k, legacy_group_milestones_count, group_milestones_count|
legacy_group_milestones_count + group_milestones_count
end
end
def self.groups_milestone_state_count(groups)
return STATE_COUNT_HASH unless groups
return self.group_milestones_states_count(groups) unless groups.respond_to?(:each)
milestone_states = STATE_COUNT_HASH
groups.each do |group|
group_milestones_count = self.group_milestones_states_count(group)
milestone_states = milestone_states.merge(group_milestones_count) do |k, milestone_state, group_milestones_count|
milestone_state + group_milestones_count
end
end
milestone_states
end
def self.group_milestones_states_count(group)
return STATE_COUNT_HASH unless group
params = { group_ids: [group.id], state: 'all' }
relation = MilestonesFinder.new(params).execute # rubocop: disable CodeReuse/Finder
grouped_by_state = relation.reorder(nil).group(:state).count
{
opened: grouped_by_state['active'] || 0,
closed: grouped_by_state['closed'] || 0,
all: grouped_by_state.values.sum
}
end
# Counts the legacy group milestones which must be grouped by title
def self.legacy_group_milestone_states_count(projects)
return STATE_COUNT_HASH unless projects
params = { project_ids: projects.map(&:id), state: 'all' }
relation = MilestonesFinder.new(params).execute # rubocop: disable CodeReuse/Finder
project_milestones_by_state_and_title = relation.reorder(nil).group(:state, :title).count
opened = count_by_state(project_milestones_by_state_and_title, 'active')
closed = count_by_state(project_milestones_by_state_and_title, 'closed')
all = project_milestones_by_state_and_title.map { |(_, title), _| title }.uniq.count
{
opened: opened,
closed: closed,
all: all
}
end
def self.count_by_state(milestones_by_state_and_title, state) def self.count_by_state(milestones_by_state_and_title, state)
milestones_by_state_and_title.count do |(milestone_state, _), _| milestones_by_state_and_title.count do |(milestone_state, _), _|
milestone_state == state milestone_state == state
......
...@@ -170,6 +170,22 @@ class Milestone < ActiveRecord::Base ...@@ -170,6 +170,22 @@ class Milestone < ActiveRecord::Base
sorted.with_order_id_desc sorted.with_order_id_desc
end end
def self.states_count(projects, groups = nil)
return STATE_COUNT_HASH unless projects || groups
counts = Milestone
.for_projects_and_groups(projects&.map(&:id), groups&.map(&:id))
.reorder(nil)
.group(:state)
.count
{
opened: counts['active'] || 0,
closed: counts['closed'] || 0,
all: counts.values.sum
}
end
## ##
# Returns the String necessary to reference this Milestone in Markdown. Group # Returns the String necessary to reference this Milestone in Markdown. Group
# milestones only support name references, and do not support cross-project # milestones only support name references, and do not support cross-project
......
...@@ -92,41 +92,6 @@ describe GlobalMilestone do ...@@ -92,41 +92,6 @@ describe GlobalMilestone do
end end
end end
describe '.states_count' do
context 'when the projects have milestones' do
before do
create(:closed_milestone, title: 'Active Group Milestone', project: project3)
create(:active_milestone, title: 'Active Group Milestone', project: project1)
create(:active_milestone, title: 'Active Group Milestone', project: project2)
create(:closed_milestone, title: 'Closed Group Milestone', project: project1)
create(:closed_milestone, title: 'Closed Group Milestone', project: project2)
create(:closed_milestone, title: 'Closed Group Milestone', project: project3)
end
it 'returns the quantity of global milestones in each possible state' do
expected_count = { opened: 1, closed: 2, all: 2 }
count = described_class.states_count(Project.all)
expect(count).to eq(expected_count)
end
end
context 'when the projects do not have milestones' do
before do
project1
end
it 'returns 0 as the quantity of global milestones in each state' do
expected_count = { opened: 0, closed: 0, all: 0 }
count = described_class.states_count(Project.all)
expect(count).to eq(expected_count)
end
end
end
describe '#initialize' do describe '#initialize' do
let(:milestone1_project1) { create(:milestone, title: "Milestone v1.2", project: project1) } let(:milestone1_project1) { create(:milestone, title: "Milestone v1.2", project: project1) }
let(:milestone1_project2) { create(:milestone, title: "Milestone v1.2", project: project2) } let(:milestone1_project2) { create(:milestone, title: "Milestone v1.2", project: project2) }
......
...@@ -348,4 +348,39 @@ describe Milestone do ...@@ -348,4 +348,39 @@ describe Milestone do
end end
end end
end end
describe '.states_count' do
context 'when the projects have milestones' do
let(:project_1) { create(:project) }
let(:project_2) { create(:project) }
let(:project_3) { create(:project) }
before do
create(:closed_milestone, title: 'Active Group Milestone', project: project_3)
create(:active_milestone, title: 'Active Group Milestone', project: project_1)
create(:active_milestone, title: 'Active Group Milestone', project: project_2)
create(:closed_milestone, title: 'Closed Group Milestone', project: project_1)
create(:closed_milestone, title: 'Closed Group Milestone', project: project_2)
create(:closed_milestone, title: 'Closed Group Milestone', project: project_3)
end
it 'returns the quantity of milestones in each possible state' do
expected_count = { opened: 5, closed: 4, all: 9 }
count = described_class.states_count(Project.all)
expect(count).to eq(expected_count)
end
end
context 'when the projects do not have milestones' do
it 'returns 0 as the quantity of global milestones in each state' do
expected_count = { opened: 0, closed: 0, all: 0 }
count = described_class.states_count([project])
expect(count).to eq(expected_count)
end
end
end
end end
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