20180223000001_cleanup_user_preferences_notification_sound.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. class CleanupUserPreferencesNotificationSound < ActiveRecord::Migration[5.1]
  2. def up
  3. User.with_permissions('ticket.agent').each do |user|
  4. local_to_h!(user.preferences)
  5. user.save!
  6. end
  7. User.with_permissions('ticket.agent').each do |user|
  8. next if !user.preferences
  9. next if !user.preferences[:notification_sound]
  10. next if !user.preferences[:notification_sound][:enabled]
  11. if user.preferences[:notification_sound][:enabled] == 'true'
  12. user.preferences[:notification_sound][:enabled] = true
  13. user.save!
  14. next
  15. end
  16. next if user.preferences[:notification_sound][:enabled] != 'false'
  17. user.preferences[:notification_sound][:enabled] = false
  18. user.save!
  19. next
  20. end
  21. end
  22. def local_to_h!(value)
  23. if value.class == ActionController::Parameters
  24. value = value.permit!.to_h
  25. end
  26. if value.class == Hash || value.class == ActiveSupport::HashWithIndifferentAccess
  27. value.each_key do |local_key|
  28. value[local_key] = local_to_h!(value[local_key])
  29. end
  30. end
  31. value
  32. end
  33. end