20230802112821_regex_operator_renaming.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class RegexOperatorRenaming < ActiveRecord::Migration[6.1]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. update_time_accounting_selector
  7. update_core_workflows
  8. end
  9. private
  10. OPERATOR_MAPPING = {
  11. 'regex match' => 'matches regex',
  12. 'regex mismatch' => 'does not match regex',
  13. }.freeze
  14. def update_time_accounting_selector
  15. selector = Setting.get('time_accounting_selector')
  16. return if selector.blank?
  17. return if selector[:condition].blank?
  18. selector[:condition].each do |_key, value|
  19. next if value[:operator].blank?
  20. next if OPERATOR_MAPPING.keys.exclude?(value[:operator])
  21. value[:operator] = OPERATOR_MAPPING[value[:operator]]
  22. end
  23. Setting.set('time_accounting_selector', selector)
  24. end
  25. def update_core_workflows
  26. CoreWorkflow.in_batches.each_record do |workflow|
  27. next if workflow.condition_saved.blank? && workflow.condition_selected.blank?
  28. update_condition(workflow, :condition_saved)
  29. update_condition(workflow, :condition_selected)
  30. workflow.save!
  31. end
  32. end
  33. def update_condition(workflow, condition_type)
  34. return if !regex_operators_used?(workflow, condition_type)
  35. workflow[condition_type.to_sym].each_value do |condition|
  36. next if condition[:operator].blank?
  37. next if OPERATOR_MAPPING.keys.exclude?(condition[:operator])
  38. condition[:operator] = OPERATOR_MAPPING[condition[:operator]]
  39. end
  40. end
  41. def regex_operators_used?(workflow, condition_type)
  42. workflow[condition_type.to_sym].values.any? do |condition|
  43. condition.key?(:operator) && condition[:operator].present? && OPERATOR_MAPPING.key?(condition[:operator])
  44. end
  45. end
  46. end