Skip to content

Commit 2efe1d9

Browse files
committed
explicitly handle deleted project phases
1 parent 65789f3 commit 2efe1d9

File tree

3 files changed

+101
-8
lines changed

3 files changed

+101
-8
lines changed

config/locales/en.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,12 +1673,13 @@ en:
16731673
work_package: "Work packages"
16741674
project_phase:
16751675
activated: "activated"
1676-
deactivated: "deactivated"
16771676
added_date: "set to %{date}"
16781677
changed_date: "changed from %{from} to %{to}"
1679-
removed_date: "date deleted %{date}"
1680-
phase_and_one_gate: "%{phase_message}. %{gate_message}"
1678+
deactivated: "deactivated"
1679+
deleted_project_phase: "Deleted project phase"
16811680
phase_and_both_gates: "%{phase_message}. %{start_gate_message}, and %{finish_gate_message}"
1681+
phase_and_one_gate: "%{phase_message}. %{gate_message}"
1682+
removed_date: "date deleted %{date}"
16821683

16831684
# common attributes of all models
16841685
attributes:

lib/open_project/journal_formatter/project_phase_definition.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,21 @@ class OpenProject::JournalFormatter::ProjectPhaseDefinition < JournalFormatter::
3232
private
3333

3434
def associated_object_name(object)
35-
if phase_active?(object)
35+
if object.nil?
36+
I18n.t(:"activity.project_phase.deleted_project_phase")
37+
elsif phase_active?(object)
3638
super
3739
else
3840
"#{super} (#{I18n.t(:label_inactive)})"
3941
end
4042
end
4143

4244
def phase_active?(definition)
43-
phase = @journal.journable.project.phases.detect { |phase| phase.definition_id == definition.id } if definition
44-
45-
phase&.active
45+
@journal
46+
.journable
47+
.project
48+
.phases
49+
.detect { |phase| phase.definition_id == definition.id }
50+
&.active
4651
end
4752
end

spec/lib/open_project/journal_formatter/project_phase_definition_spec.rb

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@
3434
describe "#render" do
3535
let(:project_phase) { build_stubbed(:project_phase) }
3636
let(:other_project_phase) { build_stubbed(:project_phase) }
37-
let(:project_phases) { [project_phase, other_project_phase] }
37+
let(:project_phases) { [project_phase, other_project_phase].compact }
3838
let(:project) { build_stubbed(:project, phases: project_phases) }
3939
let(:work_package) { build_stubbed(:work_package, project:) }
4040
let(:journal) { build_stubbed(:work_package_journal, journable: work_package) }
4141
let(:instance) { described_class.new(journal) }
4242

4343
before do
44+
allow(Project::PhaseDefinition)
45+
.to receive(:find_by)
46+
.and_return(nil)
47+
4448
project_phases.each do |phase|
4549
allow(Project::PhaseDefinition)
4650
.to receive(:find_by)
@@ -173,5 +177,88 @@
173177

174178
it_behaves_like "renders project phase definition change"
175179
end
180+
181+
context "when setting a phase whose definition is deleted" do
182+
let(:old_value) { nil }
183+
let(:new_value) { -1.to_s }
184+
let(:expected) do
185+
I18n.t(:text_journal_set_to,
186+
label: "<strong>Project phase</strong>",
187+
value: "<i>#{I18n.t(:"activity.project_phase.deleted_project_phase")}</i>")
188+
end
189+
190+
it_behaves_like "renders project phase definition change"
191+
end
192+
193+
context "when changing between two phases whose definition is deleted" do
194+
let(:old_value) { -1.to_s }
195+
let(:new_value) { -2.to_s }
196+
let(:expected) do
197+
I18n.t(:text_journal_changed_plain,
198+
label: "<strong>Project phase</strong>",
199+
linebreak: nil,
200+
old: "<i>#{I18n.t(:"activity.project_phase.deleted_project_phase")}</i>",
201+
new: "<i>#{I18n.t(:"activity.project_phase.deleted_project_phase")}</i>")
202+
end
203+
204+
it_behaves_like "renders project phase definition change"
205+
end
206+
207+
context "when deleting a phase whose definition is deleted" do
208+
let(:old_value) { -1.to_s }
209+
let(:new_value) { nil }
210+
let(:expected) do
211+
I18n.t(:text_journal_deleted,
212+
label: "<strong>Project phase</strong>",
213+
old: "<strike><i>#{I18n.t(:"activity.project_phase.deleted_project_phase")}</i></strike>")
214+
end
215+
216+
it_behaves_like "renders project phase definition change"
217+
end
218+
219+
context "when changing between an active and an inactive phase" do
220+
let(:project_phase) { build_stubbed(:project_phase, active: true) }
221+
let(:other_project_phase) { build_stubbed(:project_phase, active: false) }
222+
let(:old_value) { other_project_phase.definition_id.to_s }
223+
let(:new_value) { project_phase.definition_id.to_s }
224+
let(:expected) do
225+
I18n.t(:text_journal_changed_plain,
226+
label: "<strong>Project phase</strong>",
227+
linebreak: nil,
228+
old: "<i>#{other_project_phase.name} (Inactive)</i>",
229+
new: "<i>#{project_phase.name}</i>")
230+
end
231+
232+
it_behaves_like "renders project phase definition change"
233+
end
234+
235+
context "when changing between an active and a phase not configured in the project" do
236+
let(:project) { build_stubbed(:project, phases: [other_project_phase]) }
237+
let(:old_value) { other_project_phase.definition_id.to_s }
238+
let(:new_value) { project_phase.definition_id.to_s }
239+
let(:expected) do
240+
I18n.t(:text_journal_changed_plain,
241+
label: "<strong>Project phase</strong>",
242+
linebreak: nil,
243+
old: "<i>#{other_project_phase.name}</i>",
244+
new: "<i>#{project_phase.name} (Inactive)</i>")
245+
end
246+
247+
it_behaves_like "renders project phase definition change"
248+
end
249+
250+
context "when changing between an active and a deleted phase" do
251+
let(:old_value) { other_project_phase.definition_id.to_s }
252+
let(:new_value) { -1.to_s }
253+
let(:expected) do
254+
I18n.t(:text_journal_changed_plain,
255+
label: "<strong>Project phase</strong>",
256+
linebreak: nil,
257+
old: "<i>#{other_project_phase.name}</i>",
258+
new: "<i>#{I18n.t(:"activity.project_phase.deleted_project_phase")}</i>")
259+
end
260+
261+
it_behaves_like "renders project phase definition change"
262+
end
176263
end
177264
end

0 commit comments

Comments
 (0)