20180806000001_fixed_twitter_ticket_article_preferences7.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. class FixedTwitterTicketArticlePreferences7 < 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 || sub_level.class == Twitter::Geo
  20. value[sub_key] = sub_level.to_h
  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. if article.preferences[:twitter]&.key?(:geo) && article.preferences[:twitter][:geo].nil?
  30. article.preferences[:twitter][:geo] = {}
  31. changed = true
  32. end
  33. if article.preferences[:twitter]&.key?(:place) && article.preferences[:twitter][:place].nil?
  34. article.preferences[:twitter][:place] = {}
  35. changed = true
  36. end
  37. next if !changed
  38. article.save!
  39. end
  40. end
  41. end