Ensure milestones passed to GlobalMilestone is an ActiveRecord::Relation

This commit is contained in:
Ahmad Sherif 2016-09-23 13:34:39 +02:00
parent b8bfe50a50
commit b5132118cd
2 changed files with 7 additions and 15 deletions

View file

@ -8,7 +8,8 @@ class GlobalMilestone
milestones = milestones.group_by(&:title)
milestones.map do |title, milestones|
new(title, milestones)
milestones_relation = Milestone.where(id: milestones.map(&:id))
new(title, milestones_relation)
end
end
@ -61,11 +62,11 @@ class GlobalMilestone
end
def participants
@participants ||= milestones_relation.includes(:participants).map(&:participants).flatten.compact.uniq
@participants ||= milestones.includes(:participants).map(&:participants).flatten.compact.uniq
end
def labels
@labels ||= GlobalLabel.build_collection(milestones_relation.includes(:labels).map(&:labels).flatten)
@labels ||= GlobalLabel.build_collection(milestones.includes(:labels).map(&:labels).flatten)
.sort_by!(&:title)
end
@ -89,14 +90,4 @@ class GlobalMilestone
end
end
end
private
def milestones_relation
@milestones_relation ||= if milestones.is_a?(ActiveRecord::Relation)
milestones
else
Milestone.where(id: milestones.map(&:id))
end
end
end

View file

@ -50,8 +50,9 @@ describe GlobalMilestone, models: true do
milestone1_project2,
milestone1_project3,
]
milestones_relation = Milestone.where(id: milestones.map(&:id))
@global_milestone = GlobalMilestone.new(milestone1_project1.title, milestones)
@global_milestone = GlobalMilestone.new(milestone1_project1.title, milestones_relation)
end
it 'has exactly one group milestone' do
@ -67,7 +68,7 @@ describe GlobalMilestone, models: true do
let(:milestone) { create(:milestone, title: "git / test", project: project1) }
it 'strips out slashes and spaces' do
global_milestone = GlobalMilestone.new(milestone.title, [milestone])
global_milestone = GlobalMilestone.new(milestone.title, Milestone.where(id: milestone.id))
expect(global_milestone.safe_title).to eq('git-test')
end