checklist_template.rb 850 B

123456789101112131415161718192021222324252627282930
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ChecklistTemplate < ApplicationModel
  3. include HasDefaultModelUserRelations
  4. include ChecksClientNotification
  5. include ChecklistTemplate::TriggersSubscriptions
  6. include ChecklistTemplate::Assets
  7. include CanChecklistSortedItems
  8. has_many :items, inverse_of: :checklist_template, dependent: :destroy
  9. validates :name, length: { maximum: 250 }
  10. def replace_items!(new_items)
  11. if new_items.count > 100
  12. raise Exceptions::UnprocessableEntity, __('Checklist Template items are limited to 100 items per checklist.')
  13. end
  14. ActiveRecord::Base.transaction do
  15. items.destroy_all
  16. self.sorted_item_ids = new_items
  17. .compact_blank
  18. .map { |elem| items.create! text: elem.strip }
  19. .map(&:id)
  20. save!
  21. end
  22. end
  23. end