diff --git a/app/models/project_template_field.rb b/app/models/project_template_field.rb index 3a81fa4e..e1cb2c13 100644 --- a/app/models/project_template_field.rb +++ b/app/models/project_template_field.rb @@ -1,5 +1,10 @@ class ProjectTemplateField < ApplicationRecord belongs_to :project_template + + # has to be before the lines below or else the cascade delete would've happened before the check + before_destroy :cannot_delete_if_in_use + before_destroy :cannot_delete_title_field + has_many :project_instance_fields, dependent: :destroy acts_as_list scope: :project_template, add_new_at: :bottom @@ -14,8 +19,6 @@ class ProjectTemplateField < ApplicationRecord validates :position, numericality: { only_integer: true }, allow_nil: true before_validation :force_title_required - before_destroy :cannot_delete_if_in_use - before_destroy :cannot_delete_title_field FIELD_TYPE_LABELS = { 'shorttext' => 'Short Text', @@ -35,17 +38,21 @@ def option_list private def cannot_delete_title_field - return unless is_project_title? - - errors.add(:base, 'Cannot delete the Project Title field') - throw :abort + if destroyed_by_association and destroyed_by_association.active_record.name == "ProjectTemplate" + return + elsif is_project_title? + errors.add(:base, 'Cannot delete the Project Title field') + throw :abort + end end def cannot_delete_if_in_use - return unless project_instance_fields.exists? - - errors.add(:base, "Field “#{label}” is in use and can’t be removed") - throw :abort + if destroyed_by_association and destroyed_by_association.active_record.name == "ProjectTemplate" + return + elsif project_instance_fields.exists? + errors.add(:base, "Field “#{label}” is in use and can’t be removed") + throw :abort + end end def force_title_required diff --git a/db/schema.rb b/db/schema.rb index 7afe4305..45c5a861 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,6 +11,9 @@ # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema[8.0].define(version: 2026_08_29_082339) do + # These are extensions that must be enabled in order to support this database + enable_extension "pg_catalog.plpgsql" + create_table "comments", force: :cascade do |t| t.integer "user_id", null: false t.string "text", null: false