20140723110551_adopt_xpath_in_website_agent.rb 829 B

123456789101112131415161718192021222324252627282930
  1. class AdoptXpathInWebsiteAgent < ActiveRecord::Migration
  2. class Agent < ActiveRecord::Base
  3. include JSONSerializedField
  4. json_serialize :options
  5. end
  6. def up
  7. Agent.where(type: 'Agents::WebsiteAgent').each do |agent|
  8. extract = agent.options['extract']
  9. next unless extract.is_a?(Hash) && extract.all? { |name, detail|
  10. detail.key?('xpath') || detail.key?('css')
  11. }
  12. agent.options_will_change!
  13. agent.options['extract'].each { |name, extraction|
  14. case
  15. when extraction.delete('text')
  16. extraction['value'] = './/text()'
  17. when attr = extraction.delete('attr')
  18. extraction['value'] = "@#{attr}"
  19. end
  20. }
  21. agent.save!
  22. end
  23. end
  24. def down
  25. raise ActiveRecord::IrreversibleMigration, "Cannot revert this migration"
  26. end
  27. end