20180226085743_issue_1660_fix_tree_select_configurations.rb 992 B

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