Skip to content

Commit 8a08f71

Browse files
committed
[#71059] Merge sprint goal forms
Use one configurable form for owned and shared sprint goal fields.
1 parent 9b05338 commit 8a08f71

6 files changed

Lines changed: 43 additions & 166 deletions

File tree

modules/backlogs/app/components/backlogs/sprint_form_component.html.erb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ See COPYRIGHT and LICENSE files for more details.
6262
end
6363
flex.with_row do
6464
f.fields_for(:goal, goal) do |goal_fields|
65-
render goal_form_class.new(goal_fields, disabled: !can_edit_goal?)
65+
render Backlogs::Sprints::GoalForm.new(
66+
goal_fields,
67+
label: goal_label,
68+
caption: goal_caption,
69+
disabled: !can_edit_goal?
70+
)
6671
end
6772
end
6873
end

modules/backlogs/app/components/backlogs/sprint_form_component.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,18 @@ def goal
8080
sprint.goals.find_or_initialize_by(project:)
8181
end
8282

83-
def goal_form_class
83+
def goal_label
8484
if shared_sprint?
85-
Backlogs::Sprints::SharedGoalForm
85+
I18n.t("backlogs.sprint_form.goal_for_this_project_label", attribute: Sprint.human_attribute_name(:goal))
8686
else
87-
Backlogs::Sprints::OwnedGoalForm
87+
Sprint.human_attribute_name(:goal)
8888
end
8989
end
9090

91+
def goal_caption
92+
I18n.t("backlogs.sprint_form.goal_caption") if shared_sprint?
93+
end
94+
9195
private
9296

9397
def http_verb

modules/backlogs/app/forms/backlogs/sprints/owned_goal_form.rb renamed to modules/backlogs/app/forms/backlogs/sprints/goal_form.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@
3030

3131
module Backlogs
3232
module Sprints
33-
class OwnedGoalForm < ApplicationForm
33+
class GoalForm < ApplicationForm
3434
extend Dry::Initializer
3535

36+
option :label
37+
option :caption, optional: true
3638
option :disabled, default: -> { false }
3739

3840
form do |f|
3941
f.text_field(
4042
name: :text,
41-
label: Sprint.human_attribute_name(:goal),
43+
label:,
44+
caption:,
4245
disabled:,
4346
maxlength: SprintGoal::TEXT_MAX_LENGTH
4447
)

modules/backlogs/app/forms/backlogs/sprints/shared_goal_form.rb

Lines changed: 0 additions & 53 deletions
This file was deleted.

modules/backlogs/spec/forms/backlogs/sprints/owned_goal_form_spec.rb renamed to modules/backlogs/spec/forms/backlogs/sprints/goal_form_spec.rb

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,29 @@
3030

3131
require "rails_helper"
3232

33-
RSpec.describe Backlogs::Sprints::OwnedGoalForm, type: :forms do
33+
RSpec.describe Backlogs::Sprints::GoalForm, type: :forms do
3434
include ViewComponent::TestHelpers
3535

3636
let(:project) { create(:project) }
3737
let(:sprint) { create(:sprint, project:) }
38-
let(:disabled) { false }
3938
let(:goal) { sprint.goals.find_or_initialize_by(project:) }
39+
let(:label) { Sprint.human_attribute_name(:goal) }
40+
let(:caption) { nil }
41+
let(:disabled) { false }
4042
let(:form_arguments) { { url: "/foo", model: sprint, scope: :sprint } }
4143

4244
def render_form
4345
render_in_view_context(
4446
described_class,
4547
form_arguments,
4648
goal,
49+
label,
50+
caption,
4751
disabled
48-
) do |described_class, form_arguments, goal, disabled|
52+
) do |described_class, form_arguments, goal, label, caption, disabled|
4953
primer_form_with(**form_arguments) do |f|
5054
f.fields_for(:goal, goal) do |goal_fields|
51-
render(described_class.new(goal_fields, disabled:))
55+
render(described_class.new(goal_fields, label:, caption:, disabled:))
5256
end
5357
end
5458
end
@@ -59,8 +63,8 @@ def render_form
5963
page
6064
end
6165

62-
it "renders the goal field" do
63-
expect(rendered_form).to have_field(Sprint.human_attribute_name(:goal), disabled: false)
66+
it "renders the goal field with the provided label" do
67+
expect(rendered_form).to have_field(label, disabled: false)
6468
end
6569

6670
it "renders the goal text field under the goal command param" do
@@ -73,23 +77,35 @@ def render_form
7377
)
7478
end
7579

76-
it "does not render the shared sprint caption" do
80+
it "does not render a blank caption" do
7781
expect(rendered_form).to have_no_text(I18n.t("backlogs.sprint_form.goal_caption"))
7882
end
7983

84+
context "with a caption" do
85+
let(:label) do
86+
I18n.t("backlogs.sprint_form.goal_for_this_project_label", attribute: Sprint.human_attribute_name(:goal))
87+
end
88+
let(:caption) { I18n.t("backlogs.sprint_form.goal_caption") }
89+
90+
it "renders the project-specific label and caption" do
91+
expect(rendered_form).to have_field(label, disabled: false)
92+
expect(rendered_form).to have_text(caption)
93+
end
94+
end
95+
8096
context "when a goal exists for the project" do
8197
let!(:goal) { create(:sprint_goal, sprint:, project:, text: "Ship dashboard") }
8298

8399
it "renders the goal value" do
84-
expect(rendered_form).to have_field(Sprint.human_attribute_name(:goal), with: "Ship dashboard")
100+
expect(rendered_form).to have_field(label, with: "Ship dashboard")
85101
end
86102
end
87103

88104
context "when disabled" do
89105
let(:disabled) { true }
90106

91107
it "renders the goal field as disabled" do
92-
expect(rendered_form).to have_field(Sprint.human_attribute_name(:goal), disabled: true)
108+
expect(rendered_form).to have_field(label, disabled: true)
93109
end
94110
end
95111
end

modules/backlogs/spec/forms/backlogs/sprints/shared_goal_form_spec.rb

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)