Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Allow creating new relations from relations tab

Aaron Contreras2d3d5667200a
diff --git a/app/components/work_package_relations_tab/edit_work_package_relation_dialog_component.rb b/app/components/work_package_relations_tab/edit_work_package_relation_dialog_component.rb
index 606e2bbc2cf..793883f936d 100644
--- a/app/components/work_package_relations_tab/edit_work_package_relation_dialog_component.rb
+++ b/app/components/work_package_relations_tab/edit_work_package_relation_dialog_component.rb
@@ -50,6 +50,10 @@ class WorkPackageRelationsTab::EditWorkPackageRelationDialogComponent < Applicat
def dialog_title
relation_label = t("#{I18N_NAMESPACE}.relations.#{relation.label_for(work_package)}_singular")
- t("#{I18N_NAMESPACE}.label_edit_x", x: relation_label)
+ if relation.persisted?
+ t("#{I18N_NAMESPACE}.label_edit_x", x: relation_label)
+ else
+ t("#{I18N_NAMESPACE}.label_add_x", x: relation_label)
+ end
end
end
diff --git a/app/components/work_package_relations_tab/edit_work_package_relation_form_component.html.erb b/app/components/work_package_relations_tab/edit_work_package_relation_form_component.html.erb
index ba0b5a7a396..df414b26bfa 100644
--- a/app/components/work_package_relations_tab/edit_work_package_relation_form_component.html.erb
+++ b/app/components/work_package_relations_tab/edit_work_package_relation_form_component.html.erb
@@ -2,8 +2,7 @@
<%= primer_form_with(
id: FORM_ID,
model: @relation,
- method: :patch,
- url: work_package_relation_path(@work_package, @relation),
+ **submit_url_options,
data: { turbo: true,
controller: STIMULUS_CONTROLLER,
application_target: "dynamic" }
@@ -20,15 +19,40 @@
target_work_package = related_work_package
stimulus_controller = STIMULUS_CONTROLLER
i18n_namespace = I18N_NAMESPACE
+ dialog_id = DIALOG_ID
not_parent_child_relation = not_parent_child_relation?
+ to_id_field_test_selector = TO_ID_FIELD_TEST_SELECTOR
+ to_id_field_value = relation.to.present? ? "#{target_work_package.type.name.upcase} ##{target_work_package.id} - #{target_work_package.subject}" : nil
render_inline_form(f) do |my_form|
- my_form.text_field(
- name: :to_id,
- label: WorkPackage.model_name.human,
- visually_hide_label: false,
- value: "#{target_work_package.type.name.upcase} ##{target_work_package.id} - #{target_work_package.subject}",
- readonly: true
- )
+ if relation.persisted?
+ my_form.text_field(
+ name: :to_id,
+ label: WorkPackage.model_name.human,
+ visually_hide_label: false,
+ value: to_id_field_value,
+ readonly: true
+ )
+ else
+ my_form.hidden(
+ name: :relation_type,
+ value: relation.relation_type
+ )
+
+ my_form.work_package_autocompleter(
+ name: :to_id,
+ label: WorkPackage.model_name.human,
+ visually_hide_label: false,
+ autocomplete_options: {
+ resource: 'work_packages',
+ searchKey: 'subjectOrId',
+ openDirectly: false,
+ focusDirectly: false,
+ dropdownPosition: 'bottom',
+ appendTo: "##{dialog_id}",
+ data: { test_selector: to_id_field_test_selector}
+ }
+ )
+ end
if not_parent_child_relation
my_form.text_field(
diff --git a/app/components/work_package_relations_tab/edit_work_package_relation_form_component.rb b/app/components/work_package_relations_tab/edit_work_package_relation_form_component.rb
index 74ca83c8d69..f1a28e88d30 100644
--- a/app/components/work_package_relations_tab/edit_work_package_relation_form_component.rb
+++ b/app/components/work_package_relations_tab/edit_work_package_relation_form_component.rb
@@ -33,7 +33,9 @@ class WorkPackageRelationsTab::EditWorkPackageRelationFormComponent < Applicatio
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
+ DIALOG_ID = "edit-work-package-relation-dialog"
FORM_ID = "edit-work-package-relation-form"
+ TO_ID_FIELD_TEST_SELECTOR = "work-package-relation-form-to-id"
STIMULUS_CONTROLLER = "work-packages--relations-tab--relation-form"
I18N_NAMESPACE = "work_package_relations_tab"
@@ -57,4 +59,14 @@ class WorkPackageRelationsTab::EditWorkPackageRelationFormComponent < Applicatio
@relation
end
end
+
+ def submit_url_options
+ if @relation.persisted?
+ { method: :patch,
+ url: work_package_relation_path(@work_package, @relation) }
+ else
+ { method: :post,
+ url: work_package_relations_path(@work_package) }
+ end
+ end
end
diff --git a/app/components/work_package_relations_tab/index_component.html.erb b/app/components/work_package_relations_tab/index_component.html.erb
index 1765c8201d1..338ed8a43dd 100644
--- a/app/components/work_package_relations_tab/index_component.html.erb
+++ b/app/components/work_package_relations_tab/index_component.html.erb
@@ -7,14 +7,23 @@
end
end
action_bar.with_column do
- render(Primer::Alpha::ActionMenu.new(menu_id: "relations-list-action-menu")) do |menu|
+ render(Primer::Alpha::ActionMenu.new(test_selector: NEW_RELATION_ACTION_MENU,
+ menu_id: NEW_RELATION_ACTION_MENU)) do |menu|
menu.with_show_button do |button|
button.with_leading_visual_icon(icon: :"plus")
button.with_trailing_action_icon(icon: :"triangle-down")
t(:label_relation)
end
Relation::TYPES.each do |relation_type, type_configuration_hash|
- menu.with_item(label: t(type_configuration_hash[:name]).capitalize) do |item|
+ label_key = "#{I18N_NAMESPACE}.relations.#{type_configuration_hash[:name]}_singular"
+ menu.with_item(
+ label: t(label_key).capitalize,
+ href: new_relation_path(relation_type:),
+ test_selector: new_button_test_selector(relation_type:),
+ content_arguments: {
+ data: { turbo_stream: true }
+ }
+ ) do |item|
item.with_description.with_content(t("#{I18N_NAMESPACE}.relations.#{relation_type}_description"))
end
end
diff --git a/app/components/work_package_relations_tab/index_component.rb b/app/components/work_package_relations_tab/index_component.rb
index 981631e9e9a..8bcabd303f7 100644
--- a/app/components/work_package_relations_tab/index_component.rb
+++ b/app/components/work_package_relations_tab/index_component.rb
@@ -2,6 +2,7 @@
class WorkPackageRelationsTab::IndexComponent < ApplicationComponent
FRAME_ID = "work-package-relations-tab-content"
+ NEW_RELATION_ACTION_MENU = "new-relation-action-menu"
I18N_NAMESPACE = "work_package_relations_tab"
include ApplicationHelper
include OpPrimer::ComponentHelpers
@@ -55,6 +56,20 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponent
end
end
+ def new_relation_path(relation_type:)
+ raise ArgumentError, "Invalid relation type: #{relation_type}" unless Relation::TYPES.key?(relation_type)
+
+ if relation_type == Relation::TYPE_CHILD
+ raise NotImplementedError, "Child relations are not supported yet"
+ else
+ new_work_package_relation_path(work_package, relation_type:)
+ end
+ end
+
+ def new_button_test_selector(relation_type:)
+ "op-new-relation-button-#{relation_type}"
+ end
+
def row_test_selector(item)
"op-relation-row-#{item.id}"
end
diff --git a/app/components/work_package_relations_tab/index_component.sass b/app/components/work_package_relations_tab/index_component.sass
index 2adb6f50b0d..e02e680c021 100644
--- a/app/components/work_package_relations_tab/index_component.sass
+++ b/app/components/work_package_relations_tab/index_component.sass
@@ -4,6 +4,6 @@
// We reference an ID as one is required to be specified for the action menu list.
// It can't be nested inside the BEM model as it's placed as a #top-layer element.
-#relations-list-action-menu-list
+#new-relation-action-menu-list
max-height: 380px
max-width: 280px
diff --git a/app/controllers/work_package_relations_controller.rb b/app/controllers/work_package_relations_controller.rb
index 3c0489390ac..5d5c6b60fc4 100644
--- a/app/controllers/work_package_relations_controller.rb
+++ b/app/controllers/work_package_relations_controller.rb
@@ -33,9 +33,21 @@ class WorkPackageRelationsController < ApplicationController
include OpTurbo::DialogStreamHelper
before_action :set_work_package
- before_action :set_relation
+ before_action :set_relation, except: %i[new create]
before_action :authorize
+ def new
+ @relation = @work_package.relations.build(
+ from: @work_package,
+ relation_type: params[:relation_type]
+ )
+
+ respond_with_dialog(
+ WorkPackageRelationsTab::EditWorkPackageRelationDialogComponent
+ .new(work_package: @work_package, relation: @relation)
+ )
+ end
+
def edit
respond_with_dialog(
WorkPackageRelationsTab::EditWorkPackageRelationDialogComponent
@@ -43,11 +55,27 @@ class WorkPackageRelationsController < ApplicationController
)
end
+ def create
+ service_result = Relations::CreateService.new(user: current_user)
+ .call(create_relation_params)
+
+ if service_result.success?
+ @work_package.reload
+ component = WorkPackageRelationsTab::IndexComponent.new(work_package: @work_package,
+ relations: @work_package.relations,
+ children: @work_package.children)
+ replace_via_turbo_stream(component:)
+ respond_with_turbo_streams
+ else
+ respond_with_turbo_streams(status: :unprocessable_entity)
+ end
+ end
+
def update
service_result = Relations::UpdateService
.new(user: current_user,
model: @relation)
- .call(relation_params)
+ .call(update_relation_params)
if service_result.success?
@work_package.reload
@@ -90,7 +118,13 @@ class WorkPackageRelationsController < ApplicationController
@relation = @work_package.relations.find(params[:id])
end
- def relation_params
+ def create_relation_params
+ params.require(:relation)
+ .permit(:relation_type, :to_id, :description)
+ .merge(from_id: @work_package.id)
+ end
+
+ def update_relation_params
params.require(:relation)
.permit(:description)
end
diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rb
index 0ca990fe50e..672643b8c9e 100644
--- a/config/initializers/permissions.rb
+++ b/config/initializers/permissions.rb
@@ -231,7 +231,9 @@ Rails.application.reloader.to_prepare do
contract_actions: { work_packages: %i[read] }
wpt.permission :add_work_packages,
- {},
+ {
+ work_package_relations: %i[new create]
+ },
permissible_on: :project,
dependencies: :view_work_packages,
contract_actions: { work_packages: %i[create] }
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 63f954d81fb..929dbdf0936 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -711,6 +711,7 @@ en:
no_results_title_text: There are currently no relations available.
blankslate_heading: "No relations"
blankslate_description: "This work package does not yet have any relations."
+ label_add_x: "Add %{x}"
label_edit_x: "Edit %{x}"
label_add_description: "Add description"
relations:
@@ -734,19 +735,28 @@ en:
blocks_description: "The related work package cannot be closed until this one is closed first"
label_blocked_singular: "blocked by"
label_blocked_plural: "blocked by"
+ label_blocked_by_singular: "blocked by"
+ label_blocked__by_plural: "blocked by"
blocked_description: "This work package cannot be closed until the related one is closed first"
+ blocked_by_description: "This work package cannot be closed until the related one is closed first"
label_duplicates_singular: "duplicates"
label_duplicates_plural: "duplicates"
duplicates_description: "This is a copy of the related work package"
label_duplicated_singular: "duplicated by"
label_duplicated_plural: "duplicated by"
+ label_duplicated_by_singular: "duplicated by"
+ label_duplicated_by_plural: "duplicated by"
+ duplicated_by_description: "The related work package is a copy of this."
duplicated_description: "The related work package is a copy of this."
label_includes_singular: "includes"
label_includes_plural: "includes"
includes_description: "Marks the related work package as including this one with no additional effect"
label_partof_singular: "part of"
label_partof_plural: "part of"
+ label_part_of_singular: "part of"
+ label_part_of_plural: "part of"
partof_description: "Marks the related work package as being part of this one with no additional effect"
+ part_of_description: "Marks the related work package as being part of this one with no additional effect"
label_requires_singular: "requires"
label_requires_plural: "requires"
requires_description: "Marks the related work package as a requirement to this one"
diff --git a/config/routes.rb b/config/routes.rb
index ebf546bf71a..8df31ef1219 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -603,7 +603,7 @@ Rails.application.routes.draw do
as: :work_package_progress
end
resources :relations_tab, only: %i[index], controller: "work_package_relations_tab"
- resources :relations, only: %i[edit update destroy], controller: "work_package_relations"
+ resources :relations, only: %i[new create edit update destroy], controller: "work_package_relations"
get "/export_dialog" => "work_packages#export_dialog", on: :collection, as: "export_dialog"
get :show_conflict_flash_message, on: :collection # we don't need a specific work package for this
diff --git a/spec/controllers/work_package_relations_controller_spec.rb b/spec/controllers/work_package_relations_controller_spec.rb
index 3a620ddcba1..423d0a4d98d 100644
--- a/spec/controllers/work_package_relations_controller_spec.rb
+++ b/spec/controllers/work_package_relations_controller_spec.rb
@@ -35,6 +35,7 @@ RSpec.describe WorkPackageRelationsController do
shared_let(:project) { create(:project) }
shared_let(:work_package) { create(:work_package, project:) }
shared_let(:related_work_package) { create(:work_package, project:) }
+ shared_let(:unrelated_work_package) { create(:work_package, project:) }
shared_let(:relation) do
create(:relation,
from: work_package,
@@ -47,6 +48,33 @@ RSpec.describe WorkPackageRelationsController do
current_user { user }
+ describe "GET /work_packages/:work_package_id/relations/new" do
+ let(:new_relation) do
+ build(:relation,
+ from: work_package,
+ to: nil,
+ relation_type: Relation::TYPE_RELATES)
+ end
+
+ before do
+ allow(WorkPackageRelationsTab::EditWorkPackageRelationDialogComponent)
+ .to receive(:new)
+ .and_call_original
+ allow(controller).to receive(:respond_with_dialog).and_call_original
+ end
+
+ it "renders the relations new dialog" do
+ get("new",
+ params: { work_package_id: work_package.id,
+ relation_type: Relation::TYPE_RELATES },
+ as: :turbo_stream)
+
+ expect(WorkPackageRelationsTab::EditWorkPackageRelationDialogComponent)
+ .to have_received(:new)
+ expect(controller).to have_received(:respond_with_dialog)
+ end
+ end
+
describe "GET /work_packages/:work_package_id/relations/:id/edit" do
before do
allow(WorkPackageRelationsTab::EditWorkPackageRelationDialogComponent)
@@ -71,6 +99,30 @@ RSpec.describe WorkPackageRelationsController do
end
end
+ describe "POST /work_packages/:work_package_id/relations" do
+ before do
+ allow(WorkPackageRelationsTab::IndexComponent).to receive(:new).and_call_original
+ allow(controller).to receive(:replace_via_turbo_stream).and_call_original
+ end
+
+ it "creates the relation" do
+ post("create",
+ params: { work_package_id: work_package.id,
+ relation: { to_id: unrelated_work_package.id,
+ relation_type: Relation::TYPE_RELATES } },
+ as: :turbo_stream)
+
+ expect(response).to be_successful
+
+ new_relation = Relation.last
+
+ expect(WorkPackageRelationsTab::IndexComponent).to have_received(:new)
+ .with(work_package:, relations: [relation, new_relation], children:)
+ expect(controller).to have_received(:replace_via_turbo_stream)
+ .with(component: an_instance_of(WorkPackageRelationsTab::IndexComponent))
+ end
+ end
+
describe "PATCH /work_packages/:work_package_id/relations/:id" do
before do
relation.update!(description: "Old relation description")
diff --git a/spec/features/work_packages/details/relations/primerized_relations_spec.rb b/spec/features/work_packages/details/relations/primerized_relations_spec.rb
index 799e765609e..4d6eaf3de47 100644
--- a/spec/features/work_packages/details/relations/primerized_relations_spec.rb
+++ b/spec/features/work_packages/details/relations/primerized_relations_spec.rb
@@ -30,6 +30,8 @@ require "spec_helper"
RSpec.describe "Primerized work package relations tab",
:js, :with_cuprite do
+ include Components::Autocompleter::NgSelectAutocompleteHelpers
+
let(:user) { create(:admin) }
let(:project) { create(:project) }
let(:work_package) { create(:work_package, project:) }
@@ -45,6 +47,7 @@ RSpec.describe "Primerized work package relations tab",
let(:to1) { create(:work_package, type: type1, project:) }
let(:to2) { create(:work_package, type: type2, project:) }
+ let(:to3) { create(:work_package, type: type1, project:) }
let(:from1) { create(:work_package, type: type1, project:) }
let!(:relation1) do
@@ -186,6 +189,7 @@ RSpec.describe "Primerized work package relations tab",
within "##{WorkPackageRelationsTab::EditWorkPackageRelationDialogComponent::DIALOG_ID}" do
wait_for_network_idle
expect(page).to have_text("Edit successor (after)")
+ expect(page).to have_field("Work package", readonly: true)
expect(page).to have_button("Add description")
expect(page).to have_field("Description", visible: :hidden)
@@ -213,6 +217,7 @@ RSpec.describe "Primerized work package relations tab",
within "##{WorkPackageRelationsTab::EditWorkPackageRelationDialogComponent::DIALOG_ID}" do
wait_for_network_idle
expect(page).to have_text("Edit successor (after)")
+ expect(page).to have_field("Work package", readonly: true)
expect(page).to have_no_button("Add description")
expect(page).to have_field("Description", visible: :visible, with: "Discovered relations have descriptions!")
@@ -239,4 +244,44 @@ RSpec.describe "Primerized work package relations tab",
end
end
end
+
+ describe "creating a relation" do
+ it "renders the new relation form for the selected type and creates the relation" do
+ scroll_to_element relations_panel
+
+ relations_panel.find("[data-test-selector='new-relation-action-menu']").click
+
+ within page.find_by_id("new-relation-action-menu-list") do # Primer appends "list" to the menu id automatically
+ click_link_or_button "Successor (after)"
+ end
+
+ wait_for_reload
+
+ within "##{WorkPackageRelationsTab::EditWorkPackageRelationFormComponent::DIALOG_ID}" do
+ expect(page).to have_text("Add successor (after)")
+ expect(page).to have_button("Add description")
+
+ autocomplete_field = page.find("[data-test-selector='work-package-relation-form-to-id']")
+ select_autocomplete(autocomplete_field,
+ query: to3.subject,
+ results_selector: "body")
+
+ click_link_or_button "Add description"
+
+ fill_in "Description", with: "Discovered relations have descriptions!"
+
+ click_link_or_button "Save"
+ end
+
+ wait_for_reload
+
+ within relations_panel do
+ new_relation = Relation.last
+ expect(new_relation.to).to eq(to3)
+ new_relation_row = page.find("[data-test-selector='op-relation-row-#{new_relation.id}']")
+ expect(new_relation_row).to have_text(to3.subject)
+ expect(new_relation_row).to have_text("Discovered relations have descriptions!")
+ end
+ end
+ end
end