trigger.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Trigger < ApplicationModel
  3. include ChecksConditionValidation
  4. include ChecksHtmlSanitized
  5. include CanSeed
  6. include Trigger::Assets
  7. store :condition
  8. store :perform
  9. validates :name, presence: true, uniqueness: { case_sensitive: false }
  10. validates :perform, 'validations/verify_perform_rules': true
  11. validates :activator, presence: true, inclusion: { in: %w[action time] }
  12. validates :execution_condition_mode, presence: true, inclusion: { in: %w[selective always] }
  13. validates :note, length: { maximum: 250 }
  14. sanitized_html :note
  15. scope :activated_by, ->(activator) { where(active: true, activator: activator) }
  16. def performed_on(object, activator_type:)
  17. return if !time_based?
  18. history_scope(object, activator_type:).create sourceable_name: name
  19. end
  20. def performable_on?(object, activator_type:)
  21. return if !time_based?
  22. already_notified_cutoff = Time.use_zone(Setting.get('timezone_default')) { Time.current.beginning_of_day }
  23. !history_scope(object, activator_type:).exists?(['created_at > ?', already_notified_cutoff])
  24. end
  25. def condition_changes_required?
  26. activator == 'action' && execution_condition_mode == 'selective'
  27. end
  28. private
  29. def time_based?
  30. activator == 'time'
  31. end
  32. def history_scope(object, activator_type:)
  33. History
  34. .where(
  35. history_object_id: History.object_lookup(object.class.name).id,
  36. o_id: object.id,
  37. history_type_id: History.type_lookup('time_trigger_performed').id,
  38. sourceable: self,
  39. value_from: activator_type
  40. )
  41. end
  42. end