Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions app/models/project_template_field.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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',
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading