Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Allow sub-tasks at any depth and fix a ticket's parent after create

Guido Boyke393ea1321c99
diff --git a/app/models/ticket.rb b/app/models/ticket.rb
index 8429155..41d844b 100644
--- a/app/models/ticket.rb
+++ b/app/models/ticket.rb
@@ -102,11 +102,14 @@ class Ticket < ApplicationRecord
has_many :time_sessions, dependent: :nullify
- # Sub-tasks: self-referential, exactly ONE level deep (parent -> children, no grandchildren).
- # The one-level invariant is enforced by the validations below. `dependent: :nullify`
- # ORPHANS children on parent delete (they survive as top-level tickets), mirrored at the
- # DB level by the FK on_delete: :nullify — destroying a child's logged time/comments
- # would be data loss (sub-tasks design, premise 4).
+ # Sub-tasks: self-referential, any depth (parent -> children -> grandchildren ...). A ticket
+ # gets its parent when it is created and keeps it: the validations below refuse any later
+ # change, so a cycle cannot be built (a new ticket has no descendants to close one with).
+ # `dependent: :nullify` ORPHANS children on parent delete (they survive as top-level
+ # tickets, their own children stay attached), mirrored at the DB level by the FK
+ # on_delete: :nullify; it writes past validations, which is why the fixed parent does not
+ # block it. Destroying a child's logged time/comments would be data loss (sub-tasks
+ # design, premise 4).
belongs_to :parent, class_name: 'Ticket', optional: true
has_many :children, class_name: 'Ticket', foreign_key: :parent_id, inverse_of: :parent, dependent: :nullify
@@ -127,11 +130,11 @@ class Ticket < ApplicationRecord
# every write through SaveTicket. acts_as_tenant scopes the `parent` load, so a foreign-org
# parent loads as nil -> rejected as :not_found (cross-tenant is a subset of cross-project,
# since project_ids are globally unique, but we reject not-found explicitly rather than
- # letting `optional: true` swallow a dangling reference).
+ # letting `optional: true` swallow a dangling reference). Depth is not limited; instead the
+ # parent is fixed once the ticket exists (parent_unchanged), which keeps the tree acyclic.
validate :parent_not_self
validate :parent_loadable_and_same_project
- validate :parent_is_top_level
- validate :no_parent_when_has_children
+ validate :parent_unchanged, on: :update
include PgSearch::Model
pg_search_scope :search_full_text, using: {
@@ -516,14 +519,10 @@ class Ticket < ApplicationRecord
end
end
- # One level: the parent must itself be top-level (no grandchildren).
- def parent_is_top_level
- errors.add(:parent_id, :must_be_top_level) if parent && parent.parent_id.present?
- end
-
- # One level: a ticket that already has children cannot itself become a child.
- def no_parent_when_has_children
- errors.add(:parent_id, :has_children) if parent_id.present? && children.exists?
+ # A saved ticket keeps its parent: setting, replacing and clearing it are all refused.
+ # Only a new ticket (no descendants yet) picks a parent, so no write can close a cycle.
+ def parent_unchanged
+ errors.add(:parent_id, :cannot_change) if will_save_change_to_parent_id?
end
end
diff --git a/config/locales/models.de.yml b/config/locales/models.de.yml
index 82613fa..7dfbd2c 100644
--- a/config/locales/models.de.yml
+++ b/config/locales/models.de.yml
@@ -152,8 +152,7 @@ de:
cannot_be_self: "darf nicht auf sich selbst verweisen"
not_found: "übergeordnete Aufgabe nicht gefunden"
different_project: "übergeordnete Aufgabe muss im selben Projekt liegen"
- must_be_top_level: "übergeordnete Aufgabe darf selbst keine Unteraufgabe sein"
- has_children: "Aufgaben mit Unteraufgaben können nicht selbst zur Unteraufgabe werden"
+ cannot_change: "kann nach dem Anlegen nicht mehr geändert werden"
reference:
self_reference: "Eine Referenz darf nicht auf sich selbst zeigen"
# Whole sentences on :base, so the message reads without an attribute name.
diff --git a/config/locales/models.en.yml b/config/locales/models.en.yml
index 2939d65..ec43596 100644
--- a/config/locales/models.en.yml
+++ b/config/locales/models.en.yml
@@ -144,8 +144,7 @@ en:
cannot_be_self: "may not point to itself"
not_found: "parent task not found"
different_project: "parent task must be in the same project"
- must_be_top_level: "parent task may not itself be a sub-task"
- has_children: "tasks with sub-tasks cannot become a sub-task themselves"
+ cannot_change: "cannot be changed once the ticket exists"
reference:
self_reference: "A reference may not point to itself"
# Whole sentences on :base, so the message reads without an attribute name.
diff --git a/spec/architecture/website_feature_claims_guard_spec.rb b/spec/architecture/website_feature_claims_guard_spec.rb
index 59afc0b..b014da0 100644
--- a/spec/architecture/website_feature_claims_guard_spec.rb
+++ b/spec/architecture/website_feature_claims_guard_spec.rb
@@ -187,18 +187,22 @@ RSpec.describe 'Mechanisms behind the claims on lalabase.com' do
end
describe 'the other chapters' do
- # "Unteraufgaben gehen genau eine Ebene tief."
- it 'refuses a second level of sub-tasks' do
+ # "Unteraufgaben gehen so tief, wie du sie brauchst. Der Fortschritt zählt nur die
+ # direkte Ebene."
+ it 'allows sub-tasks at any depth and counts only the direct level in the progress' do
organisation = create(:organisation)
ActsAsTenant.with_tenant(organisation) do
project = create(:project, organisation: organisation)
- parent = create(:ticket, project: project, organisation: organisation)
- child = create(:ticket, project: project, organisation: organisation, parent: parent)
+ root = create(:ticket, project: project, organisation: organisation)
+ child = create(:ticket, project: project, organisation: organisation, parent: root)
+ grandchild = create(:ticket, project: project, organisation: organisation, parent: child)
+ great_grandchild = build(:ticket, project: project, organisation: organisation, parent: grandchild)
- grandchild = build(:ticket, project: project, organisation: organisation, parent: child)
+ expect(great_grandchild).to be_valid
+ great_grandchild.save!
- expect(grandchild).not_to be_valid
+ expect(root.children_progress).to include(total: 1)
end
end
diff --git a/spec/models/ticket_subtasks_spec.rb b/spec/models/ticket_subtasks_spec.rb
index fa500f4..60d517d 100644
--- a/spec/models/ticket_subtasks_spec.rb
+++ b/spec/models/ticket_subtasks_spec.rb
@@ -2,8 +2,8 @@
require 'rails_helper'
-# Sub-tasks: self-referential parent_id, exactly ONE level deep, same project + org.
-# Covers the four invariants, children_progress, the delete-orphan contract and top_level.
+# Sub-tasks: self-referential parent_id, any depth, same project + org, parent fixed after create.
+# Covers the invariants, children_progress, the delete-orphan contract and top_level.
RSpec.describe Ticket, 'sub-tasks', type: :model do
let(:organisation) { create(:organisation) }
let(:project) do
@@ -24,7 +24,7 @@ RSpec.describe Ticket, 'sub-tasks', type: :model do
options: { 'allowed_values' => allowed_values })
end
- describe 'one-level invariant validations' do
+ describe 'invariant validations' do
let(:parent) { create(:ticket, project: project) }
it 'accepts a child in the same project' do
@@ -64,18 +64,77 @@ RSpec.describe Ticket, 'sub-tasks', type: :model do
expect(child.errors[:parent_id]).to include(I18n.t('activerecord.errors.models.ticket.attributes.parent_id.different_project'))
end
- it 'rejects a grandchild (parent must be top-level)' do
+ it 'accepts a grandchild and a great-grandchild (depth is unlimited)' do
child = create(:ticket, project: project, parent: parent)
- grandchild = build(:ticket, project: project, parent: child)
- expect(grandchild).not_to be_valid
- expect(grandchild.errors[:parent_id]).to include(I18n.t('activerecord.errors.models.ticket.attributes.parent_id.must_be_top_level'))
+ grandchild = create(:ticket, project: project, parent: child)
+ great_grandchild = build(:ticket, project: project, parent: grandchild)
+
+ expect(great_grandchild).to be_valid
+ great_grandchild.save!
+ expect(great_grandchild.reload.parent_id).to eq(grandchild.id)
+ expect(grandchild.reload.parent_id).to eq(child.id)
+ end
+ end
+
+ describe 'the parent is fixed once the ticket exists' do
+ let(:cannot_change) { I18n.t('activerecord.errors.models.ticket.attributes.parent_id.cannot_change') }
+
+ it 'refuses giving a parent to a saved childless top-level ticket' do
+ ticket = create(:ticket, project: project)
+ ticket.parent = create(:ticket, project: project)
+
+ expect(ticket).not_to be_valid
+ expect(ticket.errors[:parent_id]).to include(cannot_change)
+ expect(ticket.reload.parent_id).to be_nil
+ end
+
+ it 'refuses moving a saved child to another parent' do
+ first_parent = create(:ticket, project: project)
+ child = create(:ticket, project: project, parent: first_parent)
+ child.parent = create(:ticket, project: project)
+
+ expect(child).not_to be_valid
+ expect(child.errors[:parent_id]).to include(cannot_change)
+ expect(child.reload.parent_id).to eq(first_parent.id)
end
- it 'rejects giving a parent to a ticket that already has children' do
- create(:ticket, project: project, parent: parent) # parent now has a child
+ it 'refuses detaching a saved child (parent to nil is a change as well)' do
+ parent = create(:ticket, project: project)
+ child = create(:ticket, project: project, parent: parent)
+ child.parent_id = nil
+
+ expect(child).not_to be_valid
+ expect(child.errors[:parent_id]).to include(cannot_change)
+ expect(child.reload.parent_id).to eq(parent.id)
+ end
+
+ it 'refuses giving a parent to a ticket that already has children' do
+ parent = create(:ticket, project: project)
+ create(:ticket, project: project, parent: parent)
parent.parent = create(:ticket, project: project)
+
expect(parent).not_to be_valid
- expect(parent.errors[:parent_id]).to include(I18n.t('activerecord.errors.models.ticket.attributes.parent_id.has_children'))
+ expect(parent.errors[:parent_id]).to include(cannot_change)
+ end
+
+ it 'cannot close a cycle A -> B -> A through the model' do
+ a = create(:ticket, project: project)
+ b = create(:ticket, project: project, parent: a)
+ a.parent = b
+
+ expect(a.save).to be(false)
+ expect(a.errors[:parent_id]).to include(cannot_change)
+ expect(a.reload.parent_id).to be_nil
+ expect(b.reload.parent_id).to eq(a.id)
+ end
+
+ it 'still saves other changes of a ticket with a parent' do
+ parent = create(:ticket, project: project)
+ child = create(:ticket, project: project, parent: parent)
+ child.title = 'Renamed'
+
+ expect(child.save).to be(true)
+ expect(child.reload.parent_id).to eq(parent.id)
end
end
@@ -128,6 +187,15 @@ RSpec.describe Ticket, 'sub-tasks', type: :model do
expect(parent.children_progress).to eq(done: 2, total: 2, percent: 100)
end
+ it 'counts only direct children, not grandchildren' do
+ create_status_def(project, [['Offen', ['is_new']], ['Erledigt', ['is_completed']]])
+ child = create(:ticket, project: project, parent: parent, custom_attributes: { 'status' => 'Offen' })
+ create(:ticket, project: project, parent: child, custom_attributes: { 'status' => 'Erledigt' })
+ create(:ticket, project: project, parent: child, custom_attributes: { 'status' => 'Erledigt' })
+
+ expect(parent.children_progress).to eq(done: 0, total: 1, percent: 0)
+ end
+
it 'is scoped to the parent project: a status another project marks closed does NOT count' do
# The done-calc must use closed_status_values_for([parent.project_id]), not a global lookup.
# Here project has no closed status; only a sibling project marks 'Erledigt' as completed.
@@ -165,6 +233,22 @@ RSpec.describe Ticket, 'sub-tasks', type: :model do
end
end
+ describe 'deleting the middle ticket of three levels' do
+ it 'makes its child top-level and leaves the grandchild on that child' do
+ root = create(:ticket, project: project)
+ middle = create(:ticket, project: project, parent: root)
+ child = create(:ticket, project: project, parent: middle)
+ grandchild = create(:ticket, project: project, parent: child)
+
+ middle.destroy
+
+ # dependent: :nullify writes with update_all, past the fixed-parent validation.
+ expect(child.reload.parent_id).to be_nil
+ expect(grandchild.reload.parent_id).to eq(child.id)
+ expect(Ticket.exists?(root.id)).to be(true)
+ end
+ end
+
describe 'defaults' do
it 'has no parent by default (parent_id stays nil on a fresh ticket)' do
expect(create(:ticket, project: project).parent_id).to be_nil
diff --git a/spec/requests/api/v1/write_endpoints_spec.rb b/spec/requests/api/v1/write_endpoints_spec.rb
index 5d3810e..0ef315b 100644
--- a/spec/requests/api/v1/write_endpoints_spec.rb
+++ b/spec/requests/api/v1/write_endpoints_spec.rb
@@ -376,7 +376,7 @@ RSpec.describe 'API V1 Write Endpoints', :ci_guard, type: :request do
ActsAsTenant.without_tenant { expect(Ticket.where(parent_id: foreign_parent.id)).to be_empty }
end
- it 'returns 422 when the parent is itself a sub-ticket (one level only)' do
+ it 'creates a grandchild when the parent is itself a sub-ticket (depth is unlimited)' do
child = ActsAsTenant.with_tenant(organisation) do
create(:ticket, project: project, organisation: organisation, creator: user, parent: ticket)
end
@@ -385,7 +385,10 @@ RSpec.describe 'API V1 Write Endpoints', :ci_guard, type: :request do
params: { ticket: { title: 'Grandchild', parent_id: child.id } },
headers: auth_headers
- expect(response).to have_http_status(:unprocessable_content)
+ expect(response).to have_http_status(:created)
+ body = JSON.parse(response.body)
+ expect(body.dig('data', 'parent_id')).to eq(child.id)
+ ActsAsTenant.with_tenant(organisation) { expect(Ticket.find(body.dig('data', 'id')).parent_id).to eq(child.id) }
end
# --- mapper hardening (save_ticket_create_params) -------------------
diff --git a/spec/requests/organisations/tickets_subtasks_spec.rb b/spec/requests/organisations/tickets_subtasks_spec.rb
index cc3a63b..e4465db 100644
--- a/spec/requests/organisations/tickets_subtasks_spec.rb
+++ b/spec/requests/organisations/tickets_subtasks_spec.rb
@@ -2,10 +2,11 @@
require 'rails_helper'
-# Web create path for sub-tasks: parent_id is permitted, but the same-project / one-level
-# guards live in the model. These specs prove a valid parent attaches the child and that a
-# tampered parent_id (cross-project, cross-tenant) is rejected — no child is created.
-RSpec.describe 'Tickets sub-tasks (create)', type: :request do
+# Web paths for sub-tasks: parent_id is permitted, but the same-project / fixed-parent
+# guards live in the model. These specs prove a valid parent attaches the child, that a
+# tampered parent_id (cross-project, cross-tenant) is rejected (no child is created), and
+# that an update cannot change the parent of a saved ticket (no cycle can be built).
+RSpec.describe 'Tickets sub-tasks (create)', :ci_guard, type: :request do
let(:organisation) { create(:organisation) }
let(:user) { create(:user) }
let(:project) do
@@ -61,4 +62,47 @@ RSpec.describe 'Tickets sub-tasks (create)', type: :request do
params: { ticket: { title: 'Cross-Tenant', parent_id: foreign_parent.id } }
end.not_to(change { ActsAsTenant.with_tenant(organisation) { Ticket.where(project: project).count } })
end
+
+ describe 'PATCH with a changed parent_id' do
+ let(:cannot_change) { I18n.t('activerecord.errors.models.ticket.attributes.parent_id.cannot_change') }
+
+ def parent_id_of(ticket)
+ ActsAsTenant.with_tenant(organisation) { ticket.reload.parent_id }
+ end
+
+ it 'leaves the parent of a top-level ticket unchanged and answers with the refusal' do
+ ticket = ActsAsTenant.with_tenant(organisation) { create(:ticket, project: project, creator: user) }
+
+ patch organisation_project_ticket_path(organisation, project, ticket),
+ params: { ticket: { title: ticket.title, parent_id: parent.id } }
+
+ expect(response).to have_http_status(:found)
+ expect(flash[:notice]).to be_nil
+ expect(flash[:alert]).to include(cannot_change)
+ expect(parent_id_of(ticket)).to be_nil
+ end
+
+ it 'cannot close a cycle by giving a parent its own child as parent' do
+ child = ActsAsTenant.with_tenant(organisation) { create(:ticket, project: project, creator: user, parent: parent) }
+
+ patch organisation_project_ticket_path(organisation, project, parent),
+ params: { ticket: { title: parent.title, parent_id: child.id } }
+
+ expect(response).to have_http_status(:found)
+ expect(flash[:notice]).to be_nil
+ expect(flash[:alert]).to include(cannot_change)
+ expect(parent_id_of(parent)).to be_nil
+ expect(parent_id_of(child)).to eq(parent.id)
+ end
+
+ it 'does not detach a child through an empty parent_id' do
+ child = ActsAsTenant.with_tenant(organisation) { create(:ticket, project: project, creator: user, parent: parent) }
+
+ patch organisation_project_ticket_path(organisation, project, child),
+ params: { ticket: { title: child.title, parent_id: '' } }
+
+ expect(flash[:notice]).to be_nil
+ expect(parent_id_of(child)).to eq(parent.id)
+ end
+ end
end
diff --git a/spec/services/actors/move_ticket_hierarchy_spec.rb b/spec/services/actors/move_ticket_hierarchy_spec.rb
index 9840e4a..c957ff4 100644
--- a/spec/services/actors/move_ticket_hierarchy_spec.rb
+++ b/spec/services/actors/move_ticket_hierarchy_spec.rb
@@ -47,6 +47,29 @@ RSpec.describe Actors::MoveTicket, 'hierarchy guard', type: :service do
expect(child.reload.project_id).to eq(source_project.id)
end
+ context 'with a tree three levels deep' do
+ let!(:middle) { create(:ticket, project: source_project, organisation: organisation, creator: user, parent: parent) }
+ let!(:grandchild) do
+ create(:ticket, project: source_project, organisation: organisation, creator: user, parent: middle)
+ end
+
+ it 'blocks moving the grandchild and leaves the tree in place' do
+ result = described_class.new(ticket: grandchild, target_project: target_project, current_user: user).call
+
+ expect(result.success?).to be(false)
+ expect(result.code).to eq(:has_hierarchy)
+ expect(grandchild.reload.project_id).to eq(source_project.id)
+ end
+
+ it 'blocks moving the middle ticket (it has a parent and a child) and leaves the tree in place' do
+ result = described_class.new(ticket: middle, target_project: target_project, current_user: user).call
+
+ expect(result.success?).to be(false)
+ expect(result.code).to eq(:has_hierarchy)
+ expect(Ticket.where(id: [parent.id, middle.id, grandchild.id]).pluck(:project_id)).to eq([source_project.id] * 3)
+ end
+ end
+
it 'still moves a normal ticket without parent or children (regression guard)' do
plain = create(:ticket, project: source_project, organisation: organisation, creator: user)