20180226085743_issue_1660_fix_tree_select_configurations.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Issue1660FixTreeSelectConfigurations < ActiveRecord::Migration[5.1]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. return if attributes.blank?
  7. attributes.each do |attribute|
  8. next if attribute.data_option.blank?
  9. next if attribute.data_option[:options].blank?
  10. fixed_options = fix(attribute.data_option[:options])
  11. attribute.data_option[:options] = fixed_options
  12. attribute.save!
  13. end
  14. end
  15. private
  16. def attributes
  17. @attributes ||= ObjectManager::Attribute.where(data_type: 'tree_select')
  18. end
  19. def fix(options, namespace = nil)
  20. options.tap do |ref|
  21. ref.each do |option|
  22. option_namespace = Array(namespace.dup)
  23. option_namespace.push(option['name'])
  24. option['value'] = option_namespace.join('::')
  25. next if option['children'].blank?
  26. option['children'] = fix(option['children'], option_namespace)
  27. end
  28. end
  29. end
  30. end