20180226085743_issue_1660_fix_tree_select_configurations.rb 991 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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