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

[40027] Delete attribute help text when custom field destroyed

Oliver Günther733374290296
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index 2d46582e473..df9330ca022 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -72,6 +72,7 @@ class CustomField < ApplicationRecord
unless: Proc.new { |cf| cf.max_length.blank? }
before_validation :check_searchability
+ after_destroy :destroy_help_text
# make sure int, float, date, and bool are not searchable
def check_searchability
@@ -323,4 +324,10 @@ class CustomField < ApplicationRecord
.sort
.map { |u| [u.name, u.id.to_s] }
end
+
+ def destroy_help_text
+ AttributeHelpText
+ .where(attribute_name: "custom_field_#{id}")
+ .destroy_all
+ end
end
diff --git a/db/migrate/20211117195121_remove_destroyed_help_texts.rb b/db/migrate/20211117195121_remove_destroyed_help_texts.rb
new file mode 100644
index 00000000000..83b15b74652
--- /dev/null
+++ b/db/migrate/20211117195121_remove_destroyed_help_texts.rb
@@ -0,0 +1,16 @@
+class RemoveDestroyedHelpTexts < ActiveRecord::Migration[6.1]
+ def up
+ custom_field_ids = CustomField
+ .pluck(:id)
+ .map { |id| "custom_field_#{id}"}
+
+ AttributeHelpText
+ .where("attribute_name LIKE 'custom_field_%'")
+ .where.not(attribute_name: custom_field_ids)
+ .destroy_all
+ end
+
+ def down
+ # Nothing to do
+ end
+end
diff --git a/spec/models/attribute_help_text/work_package_spec.rb b/spec/models/attribute_help_text/work_package_spec.rb
index fb1256bd728..04e09392863 100644
--- a/spec/models/attribute_help_text/work_package_spec.rb
+++ b/spec/models/attribute_help_text/work_package_spec.rb
@@ -29,6 +29,18 @@
require 'spec_helper'
describe AttributeHelpText::WorkPackage, type: :model do
+ def create_cf_help_text(custom_field)
+ # Need to clear the request store after every creation as the available attributes are cached
+ RequestStore.clear!
+ FactoryBot.create(:work_package_help_text, attribute_name: "custom_field_#{custom_field.id}")
+ end
+
+ let(:wp_custom_field) { FactoryBot.create :text_wp_custom_field }
+
+ let(:cf_instance) do
+ create_cf_help_text(wp_custom_field)
+ end
+
it_behaves_like 'acts_as_attachable included' do
let(:model_instance) { FactoryBot.create(:work_package_help_text) }
let(:project) { FactoryBot.create(:project) }
@@ -61,17 +73,6 @@ describe AttributeHelpText::WorkPackage, type: :model do
let(:permission) { [] }
let(:static_instance) { FactoryBot.create :work_package_help_text, attribute_name: 'project' }
- def create_cf_help_text(custom_field)
- # Need to clear the request store after every creation as the available attributes are cached
- RequestStore.clear!
- FactoryBot.create(:work_package_help_text, attribute_name: "custom_field_#{custom_field.id}")
- end
-
- let(:cf_instance) do
- custom_field = FactoryBot.create :text_wp_custom_field
- create_cf_help_text(custom_field)
- end
-
subject { FactoryBot.build :work_package_help_text }
before do
@@ -204,4 +205,17 @@ describe AttributeHelpText::WorkPackage, type: :model do
expect(subject.type_caption).to eq I18n.t(:label_work_package)
end
end
+
+ describe 'destroy' do
+ context 'when the custom field is destroyed' do
+ before do
+ cf_instance
+ wp_custom_field.destroy
+ end
+
+ it 'also destroys the instance' do
+ expect { cf_instance.reload }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+ end
end