20161124061256_convert_website_agent_template_for_merge.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. class ConvertWebsiteAgentTemplateForMerge < ActiveRecord::Migration[5.0]
  2. def up
  3. Agents::WebsiteAgent.find_each do |agent|
  4. extract = agent.options['extract'].presence
  5. template = agent.options['template'].presence
  6. next unless extract.is_a?(Hash) && template.is_a?(Hash)
  7. (extract.keys - template.keys).each do |key|
  8. extract[key]['hidden'] = true
  9. end
  10. template.delete_if { |key, value|
  11. extract.key?(key) &&
  12. value.match(/\A\{\{\s*#{Regexp.quote(key)}\s*\}\}\z/)
  13. }
  14. agent.save!(validate: false)
  15. end
  16. end
  17. def down
  18. Agents::WebsiteAgent.find_each do |agent|
  19. extract = agent.options['extract'].presence
  20. template = agent.options['template'].presence
  21. next unless extract.is_a?(Hash) && template.is_a?(Hash)
  22. (extract.keys - template.keys).each do |key|
  23. unless extract[key].delete('hidden').in?([true, 'true'])
  24. template[key] = "{{ #{key} }}"
  25. end
  26. end
  27. agent.save!(validate: false)
  28. end
  29. end
  30. end