[40027] Delete attribute help text when custom field destroyed

https://community.openproject.org/work_packages/40027
pull/9870/head
Oliver Günther 3 years ago
parent bea6c64f31
commit 7333742902
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 7
      app/models/custom_field.rb
  2. 16
      db/migrate/20211117195121_remove_destroyed_help_texts.rb
  3. 36
      spec/models/attribute_help_text/work_package_spec.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

@ -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

@ -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

Loading…
Cancel
Save