macro.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 Macro::TriggersSubscriptions
  8. store :perform
  9. validates :perform, 'validations/verify_perform_rules': true
  10. validates :name, presence: true, uniqueness: { case_sensitive: false }
  11. validates :ux_flow_next_up, inclusion: { in: %w[none next_task next_task_on_close next_from_overview] }
  12. has_and_belongs_to_many :groups, after_add: :cache_update, after_remove: :cache_update, class_name: 'Group'
  13. validates :note, length: { maximum: 250 }
  14. sanitized_html :note
  15. collection_push_permission('ticket.agent')
  16. scope :available_in_groups, lambda { |groups|
  17. left_outer_joins(:groups_macros)
  18. .where(groups_macros: { group_id: [nil] + Array(groups) })
  19. .where(active: true)
  20. .distinct
  21. }
  22. ApplicableOn = Struct.new(:result, :blocking_tickets) do
  23. delegate :==, to: :result
  24. delegate :!, to: :result
  25. def error_message
  26. "Macro blocked by: #{blocking_tickets.join(', ')}"
  27. end
  28. end
  29. def applicable_on?(tickets)
  30. tickets = Array(tickets)
  31. return ApplicableOn.new(true, []) if group_ids.blank?
  32. blocking = tickets.reject { |elem| group_ids.include? elem.group_id }
  33. ApplicableOn.new(blocking.none?, blocking)
  34. end
  35. def performable_on?(object, activator_type:)
  36. return false if !active
  37. return true if group_ids.blank?
  38. group_ids.include? object.group_id
  39. end
  40. end