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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[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.rbindex 2d46582e473..df9330ca022 100644--- a/app/models/custom_field.rb+++ b/app/models/custom_field.rb@@ -72,6 +72,7 @@ class CustomField < ApplicationRecordunless: 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 searchabledef 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+ endenddiff --git a/db/migrate/20211117195121_remove_destroyed_help_texts.rb b/db/migrate/20211117195121_remove_destroyed_help_texts.rbnew file mode 100644index 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+enddiff --git a/spec/models/attribute_help_text/work_package_spec.rb b/spec/models/attribute_help_text/work_package_spec.rbindex 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' dolet(:model_instance) { FactoryBot.create(:work_package_help_text) }let(:project) { FactoryBot.create(:project) }@@ -61,17 +73,6 @@ describe AttributeHelpText::WorkPackage, type: :model dolet(: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 doexpect(subject.type_caption).to eq I18n.t(:label_work_package)endend++ 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+ endend