item.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Checklist::Item < ApplicationModel
  3. include ChecksClientNotification
  4. include HasHistory
  5. include HasDefaultModelUserRelations
  6. include Checklist::Item::Assets
  7. attr_accessor :initial_clone
  8. belongs_to :checklist
  9. belongs_to :ticket, optional: true
  10. scope :for_user, ->(user) { joins(checklist: :ticket).where(tickets: { group: user.group_ids_access('read') }) }
  11. before_validation :detect_ticket_reference, on: %i[create update], unless: :initial_clone
  12. validate :detect_ticket_loop_reference, on: %i[create update], unless: -> { ticket.blank? || checklist.blank? }
  13. validate :validate_item_count, on: :create
  14. after_create :update_checklist
  15. after_update :update_checklist
  16. after_update :history_update_checked, if: -> { saved_change_to_checked? }
  17. after_destroy :update_checklist
  18. validates :text, presence: { allow_blank: true }
  19. history_attributes_ignored :checked
  20. def history_log_attributes
  21. {
  22. related_o_id: checklist.ticket_id,
  23. related_history_object: 'Ticket',
  24. }
  25. end
  26. def history_create
  27. history_log('created', created_by_id, { value_to: text })
  28. end
  29. def history_update_checked
  30. history_log('checklist_item_checked', updated_by_id, {
  31. value_from: text,
  32. value_to: checked.to_s,
  33. })
  34. end
  35. def history_destroy
  36. history_log('removed', updated_by_id, { value_to: text })
  37. end
  38. def notify_clients_data_attributes
  39. {
  40. id: id,
  41. updated_at: updated_at,
  42. updated_by_id: updated_by_id,
  43. }
  44. end
  45. def incomplete?
  46. return ticket_incomplete? if ticket.present?
  47. !checked
  48. end
  49. private
  50. def update_checklist
  51. if persisted?
  52. checklist.sorted_item_ids |= [id.to_s]
  53. else
  54. checklist.sorted_item_ids -= [id.to_s]
  55. end
  56. checklist.updated_at = Time.zone.now
  57. checklist.updated_by_id = UserInfo.current_user_id || updated_by_id
  58. checklist.save!
  59. end
  60. def detect_ticket_reference
  61. return if changes.key?(:ticket_id)
  62. ticket = Ticket::Number.check(text)
  63. return if ticket.blank?
  64. self.ticket = ticket
  65. end
  66. def detect_ticket_loop_reference
  67. return if ticket.id != checklist.ticket.id
  68. errors.add(:ticket, __('reference must not be the checklist ticket.'))
  69. end
  70. def validate_item_count
  71. return if checklist.items.count < 100
  72. errors.add(:base, __('Checklist items are limited to 100 items per checklist.'))
  73. end
  74. def ticket_incomplete?
  75. # Consider the following ticket state types as incomplete:
  76. # - closed
  77. # - merged
  78. !ticket.state.state_type.name.match?(%r{^(closed|merged)$}i)
  79. end
  80. end