user_settings_decorator.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # frozen_string_literal: true
  2. class UserSettingsDecorator
  3. attr_reader :user, :settings
  4. def initialize(user)
  5. @user = user
  6. end
  7. def update(settings)
  8. @settings = settings
  9. process_update
  10. end
  11. private
  12. def process_update
  13. user.settings['notification_emails'] = merged_notification_emails
  14. user.settings['interactions'] = merged_interactions
  15. user.settings['default_privacy'] = default_privacy_preference
  16. user.settings['default_sensitive'] = default_sensitive_preference
  17. user.settings['unfollow_modal'] = unfollow_modal_preference
  18. user.settings['boost_modal'] = boost_modal_preference
  19. user.settings['delete_modal'] = delete_modal_preference
  20. user.settings['auto_play_gif'] = auto_play_gif_preference
  21. user.settings['system_font_ui'] = system_font_ui_preference
  22. user.settings['noindex'] = noindex_preference
  23. end
  24. def merged_notification_emails
  25. user.settings['notification_emails'].merge coerced_settings('notification_emails').to_h
  26. end
  27. def merged_interactions
  28. user.settings['interactions'].merge coerced_settings('interactions').to_h
  29. end
  30. def default_privacy_preference
  31. settings['setting_default_privacy']
  32. end
  33. def default_sensitive_preference
  34. boolean_cast_setting 'setting_default_sensitive'
  35. end
  36. def unfollow_modal_preference
  37. boolean_cast_setting 'setting_unfollow_modal'
  38. end
  39. def boost_modal_preference
  40. boolean_cast_setting 'setting_boost_modal'
  41. end
  42. def delete_modal_preference
  43. boolean_cast_setting 'setting_delete_modal'
  44. end
  45. def system_font_ui_preference
  46. boolean_cast_setting 'setting_system_font_ui'
  47. end
  48. def auto_play_gif_preference
  49. boolean_cast_setting 'setting_auto_play_gif'
  50. end
  51. def noindex_preference
  52. boolean_cast_setting 'setting_noindex'
  53. end
  54. def boolean_cast_setting(key)
  55. settings[key] == '1'
  56. end
  57. def coerced_settings(key)
  58. coerce_values settings.fetch(key, {})
  59. end
  60. def coerce_values(params_hash)
  61. params_hash.transform_values { |x| x == '1' }
  62. end
  63. end