macro.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Macro < ApplicationModel
  3. include ChecksClientNotification
  4. include ChecksHtmlSanitized
  5. include CanSeed
  6. include HasCollectionUpdate
  7. include HasSearchIndexBackend
  8. include CanSelector
  9. include CanSearch
  10. include Macro::TriggersSubscriptions
  11. store :perform
  12. validates :perform, 'validations/verify_perform_rules': true
  13. validates :name, presence: true, uniqueness: { case_sensitive: false }
  14. validates :ux_flow_next_up, inclusion: { in: %w[none next_task next_task_on_close next_from_overview] }
  15. has_and_belongs_to_many :groups, after_add: :cache_update, after_remove: :cache_update, class_name: 'Group'
  16. validates :note, length: { maximum: 250 }
  17. sanitized_html :note
  18. collection_push_permission('ticket.agent')
  19. scope :available_in_groups, lambda { |groups|
  20. left_outer_joins(:groups_macros)
  21. .where(groups_macros: { group_id: [nil] + Array(groups) })
  22. .where(active: true)
  23. .distinct
  24. }
  25. ApplicableOn = Struct.new(:result, :blocking_tickets) do
  26. delegate :==, to: :result
  27. delegate :!, to: :result
  28. def error_message
  29. "Macro blocked by: #{blocking_tickets.join(', ')}"
  30. end
  31. end
  32. def applicable_on?(tickets)
  33. tickets = Array(tickets)
  34. return ApplicableOn.new(true, []) if group_ids.blank?
  35. blocking = tickets.reject { |elem| group_ids.include? elem.group_id }
  36. ApplicableOn.new(blocking.none?, blocking)
  37. end
  38. def performable_on?(object, activator_type:)
  39. return false if !active
  40. return true if group_ids.blank?
  41. group_ids.include? object.group_id
  42. end
  43. end