Skip to content

Commit ba069df

Browse files
committed
add duration to project phase
1 parent 95f2d23 commit ba069df

File tree

8 files changed

+24
-50
lines changed

8 files changed

+24
-50
lines changed

app/forms/projects/life_cycles/form.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ def multi_value_life_cycle_input(form)
5555
value = [model.start_date, model.finish_date].compact.join(" - ")
5656

5757
input_attributes = { name: :date_range, value: }
58-
if model.working_days_count
58+
if model.duration
5959
input_attributes[:caption] =
60-
I18n.t("project_phase.working_days_count", count: model.working_days_count)
60+
I18n.t("project_phase.duration", count: model.duration)
6161
end
6262

6363
form.range_date_picker **base_input_attributes, **input_attributes

app/models/project/phase.rb

+1-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Project::Phase < ApplicationRecord
3333
class_name: "Project::PhaseDefinition"
3434
has_many :work_packages, inverse_of: :project_phase, dependent: :nullify
3535

36+
validates :duration, presence: true, if: :range_set?
3637
validate :validate_date_range
3738

3839
delegate :name,
@@ -54,12 +55,6 @@ def visible(user = User.current)
5455
end
5556
end
5657

57-
def working_days_count
58-
return nil if not_set?
59-
60-
Day.working.from_range(from: start_date, to: finish_date).count
61-
end
62-
6358
def date_range=(range)
6459
case range
6560
when String

app/services/journals/create_service/project_phase.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,16 @@ def insert_sql
4949
phase_id,
5050
start_date,
5151
finish_date,
52-
active
52+
active,
53+
duration
5354
)
5455
SELECT
5556
#{id_from_inserted_journal_sql},
5657
project_phases.id,
5758
project_phases.start_date,
5859
project_phases.finish_date,
59-
project_phases.active
60+
project_phases.active,
61+
project_phases.duration
6062
FROM project_phases
6163
WHERE
6264
#{only_if_created_sql}
@@ -84,6 +86,7 @@ def changes_sql
8486
phases.start_date IS DISTINCT FROM project_phase_journals.start_date
8587
OR phases.finish_date IS DISTINCT FROM project_phase_journals.finish_date
8688
OR phases.active IS DISTINCT FROM project_phase_journals.active
89+
OR phases.duration IS DISTINCT FROM project_phase_journals.duration
8790
SQL
8891
end
8992
end

config/locales/en.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ en:
429429
work_package_priorities:
430430
new_label: "New priority"
431431
project_phase:
432-
working_days_count:
432+
duration:
433433
one: "Duration: %{count} working day"
434434
other: "Duration: %{count} working days"
435435
lists:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class AddDurationToProjectPhases < ActiveRecord::Migration[8.0]
4+
def change
5+
add_column :project_phases, :duration, :integer, null: true
6+
end
7+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class AddDurationToProjectPhaseJournals < ActiveRecord::Migration[8.0]
4+
def change
5+
add_column :project_phase_journals, :duration, :integer, null: true
6+
end
7+
end

spec/factories/project_phase_factory.rb

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
start_date { Date.current - 2.days }
3838
finish_date { Date.current + 2.days }
39+
duration { 5 }
3940

4041
trait :skip_validate do
4142
to_create { |instance| instance.save(validate: false) }

spec/models/project/phase_spec.rb

-39
Original file line numberDiff line numberDiff line change
@@ -124,43 +124,4 @@
124124
expect(subject.errors[:date_range]).to be_empty
125125
end
126126
end
127-
128-
describe "#working_days_count" do
129-
it "returns nil if not_set? is true" do
130-
allow(Day).to receive(:working)
131-
132-
subject.start_date = nil
133-
subject.finish_date = nil
134-
135-
expect(subject.working_days_count).to be_nil
136-
expect(Day).not_to have_received(:working)
137-
end
138-
139-
it "returns the correct number of days if start_date and finish_date are the same" do
140-
subject.start_date = Time.zone.today
141-
subject.finish_date = Time.zone.today
142-
expect(subject.working_days_count).to eq(1)
143-
end
144-
145-
it "returns the correct number of days for a valid date range" do
146-
subject.start_date = Date.parse("2024-11-25")
147-
subject.finish_date = Date.parse("2024-11-27")
148-
expect(subject.working_days_count).to eq(3)
149-
end
150-
151-
it "calls the Day.working.from_range method with the right arguments" do
152-
subject.start_date = Date.parse("2024-11-25")
153-
subject.finish_date = Date.parse("2024-11-27")
154-
155-
allow(Day).to receive(:working).and_return(Day)
156-
allow(Day).to receive(:from_range)
157-
.with(from: subject.start_date, to: subject.finish_date)
158-
.and_return([])
159-
160-
expect(subject.working_days_count).to eq(0)
161-
162-
expect(Day).to have_received(:working).with(no_args)
163-
expect(Day).to have_received(:from_range).with(from: subject.start_date, to: subject.finish_date)
164-
end
165-
end
166127
end

0 commit comments

Comments
 (0)