item.rb 1016 B

12345678910111213141516171819202122232425262728293031323334
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ChecklistTemplate::Item < ApplicationModel
  3. include ChecksClientNotification
  4. include HasDefaultModelUserRelations
  5. include ChecklistTemplate::TriggersSubscriptions
  6. include ChecklistTemplate::Item::Assets
  7. belongs_to :checklist_template
  8. after_create :update_checklist
  9. after_destroy :update_checklist
  10. validates :text, presence: { allow_blank: true }
  11. validate :validate_item_count, on: :create
  12. private
  13. def update_checklist
  14. if persisted? && checklist_template.sorted_item_ids.exclude?(id.to_s)
  15. checklist_template.sorted_item_ids << id
  16. end
  17. if !persisted?
  18. checklist_template.sorted_item_ids = checklist_template.sorted_item_ids.reject { |sid| sid.to_s == id.to_s }
  19. end
  20. checklist_template.save!
  21. end
  22. def validate_item_count
  23. return if checklist_template.items.count < 100
  24. errors.add(:base, __('Checklist Template items are limited to 100 items per checklist.'))
  25. end
  26. end