Commit 81476a98 authored by Mark Chao's avatar Mark Chao

Add method for updating Epic date fields

parent 88601cbb
......@@ -109,6 +109,12 @@ module EE
# Needed to use EntityDateHelper#remaining_days_in_words
alias_attribute(:due_date, :end_date)
def update_dates
self.start_date = compute_start_date
self.due_date = compute_due_date
save if changed?
end
# Earliest start date from issues' milestones
def start_date_from_milestones
epic_issues.joins(issue: :milestone).minimum('milestones.start_date')
......@@ -155,5 +161,15 @@ module EE
def banzai_render_context(field)
super.merge(label_url_method: :group_epics_url)
end
private
def compute_start_date
start_date_is_fixed? ? start_date_fixed : start_date_from_milestones
end
def compute_due_date
due_date_is_fixed? ? due_date_fixed : due_date_from_milestones
end
end
end
......@@ -4,6 +4,15 @@ FactoryBot.define do
group
author
trait :use_fixed_dates do
start_date { Date.new(2010, 1, 1) }
start_date_fixed { Date.new(2010, 1, 1) }
start_date_is_fixed true
end_date { Date.new(2010, 1, 3) }
due_date_fixed { Date.new(2010, 1, 3) }
due_date_is_fixed true
end
factory :labeled_epic do
transient do
labels []
......
......@@ -85,6 +85,18 @@ describe Epic do
end
end
describe '#start_date' do
let(:date) { Date.new(2000, 1, 1) }
context 'is set' do
subject { build(:epic, :use_fixed_dates, start_date: date) }
it 'returns as is' do
expect(subject.start_date).to eq(date)
end
end
end
describe '#start_date_from_milestones' do
subject { create(:epic) }
let(:date) { Date.new(2017, 3, 4) }
......@@ -139,6 +151,35 @@ describe Epic do
end
end
describe '#update_dates' do
context 'fixed date is set' do
subject { create(:epic, :use_fixed_dates, start_date: nil, end_date: nil) }
it 'updates to fixed date' do
subject.update_dates
expect(subject.start_date).to eq(subject.start_date_fixed)
expect(subject.due_date).to eq(subject.due_date_fixed)
end
end
context 'fixed date is not set' do
subject { create(:epic, start_date: nil, end_date: nil) }
let(:start_date) { Date.new(2001, 1, 1) }
let(:due_date) { Date.new(2002, 1, 1) }
it 'updates to milestone dates' do
expect(subject).to receive(:start_date_from_milestones).and_return(start_date)
expect(subject).to receive(:due_date_from_milestones).and_return(due_date)
subject.update_dates
expect(subject.start_date).to eq(start_date)
expect(subject.due_date).to eq(due_date)
end
end
end
describe '#issues' do
let(:user) { create(:user) }
let(:group) { create(:group, :private) }
......
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