Defect #44366 » 0001-Delete-workflow-rules-referring-to-an-issue-custom-f.patch
| app/models/issue_custom_field.rb | ||
|---|---|---|
| 24 | 24 |
safe_attributes 'project_ids', |
| 25 | 25 |
'tracker_ids' |
| 26 | 26 | |
| 27 |
before_destroy :delete_workflow_rules |
|
| 28 | ||
| 27 | 29 |
def type_name |
| 28 | 30 |
:label_issue_plural |
| 29 | 31 |
end |
| 30 | 32 | |
| 33 |
# Deletes workflow rules that refer to this custom field |
|
| 34 |
def delete_workflow_rules |
|
| 35 |
WorkflowPermission.where(:field_name => id.to_s).delete_all |
|
| 36 |
end |
|
| 37 | ||
| 31 | 38 |
def visible_by?(project, user=User.current) |
| 32 | 39 |
super || roles.intersect?(user.roles_for_project(project)) |
| 33 | 40 |
end |
| db/migrate/20260827002435_delete_orphaned_workflow_rules_of_custom_fields.rb | ||
|---|---|---|
| 1 |
class DeleteOrphanedWorkflowRulesOfCustomFields < ActiveRecord::Migration[8.1] |
|
| 2 |
def up |
|
| 3 |
field_names = WorkflowPermission.where.not(field_name: nil).distinct.pluck(:field_name) |
|
| 4 |
custom_field_ids = field_names.grep(/\A\d+\z/).map(&:to_i) |
|
| 5 |
orphaned_ids = custom_field_ids - CustomField.where(id: custom_field_ids).pluck(:id) |
|
| 6 |
if orphaned_ids.any? |
|
| 7 |
WorkflowPermission.where(field_name: orphaned_ids.map(&:to_s)).delete_all |
|
| 8 |
end |
|
| 9 |
end |
|
| 10 | ||
| 11 |
def down |
|
| 12 |
# no-op |
|
| 13 |
end |
|
| 14 |
end |
|
| test/unit/issue_custom_field_test.rb | ||
|---|---|---|
| 43 | 43 |
field.save! |
| 44 | 44 |
assert_equal 0, field.roles.count |
| 45 | 45 |
end |
| 46 | ||
| 47 |
def test_destroy_should_delete_workflow_rules |
|
| 48 |
field = IssueCustomField.create!(:name => 'Field', :field_format => 'string') |
|
| 49 |
WorkflowPermission.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :field_name => field.id.to_s, :rule => 'required') |
|
| 50 |
assert_equal 1, WorkflowPermission.where(:field_name => field.id.to_s).count |
|
| 51 | ||
| 52 |
field.destroy |
|
| 53 |
assert_equal 0, WorkflowPermission.where(:field_name => field.id.to_s).count |
|
| 54 |
end |
|
| 46 | 55 |
end |
- « Previous
- 1
- 2
- Next »