20170908000001_fixed_twitter_ticket_article_preferences4.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. class FixedTwitterTicketArticlePreferences4 < ActiveRecord::Migration[5.0]
  2. def up
  3. # return if it's a new setup
  4. return if !Setting.find_by(name: 'system_init_done')
  5. # find article preferences with Twitter::NullObject and replace it with nill to prevent elasticsearch index issue
  6. article_type_ids = Ticket::Article::Type.where(name: ['twitter status', 'twitter direct-message']).pluck(:id)
  7. article_ids = Ticket::Article.where(type_id: article_type_ids).pluck(:id)
  8. article_ids.each do |article_id|
  9. article = Ticket::Article.find(article_id)
  10. next if !article.preferences
  11. changed = false
  12. article.preferences.each_value do |value|
  13. next if value.class != ActiveSupport::HashWithIndifferentAccess
  14. value.each do |sub_key, sub_level|
  15. if sub_level.class == NilClass
  16. value[sub_key] = nil
  17. next
  18. end
  19. if sub_level.class == Twitter::Place
  20. value[sub_key] = sub_level.attrs
  21. changed = true
  22. next
  23. end
  24. next if sub_level.class != Twitter::NullObject
  25. value[sub_key] = nil
  26. changed = true
  27. end
  28. end
  29. next if !changed
  30. article.save!
  31. end
  32. end
  33. end