You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.7 KiB

  1. # frozen_string_literal: true
  2. module Admin
  3. class SettingsController < BaseController
  4. ADMIN_SETTINGS = %w(
  5. site_contact_username
  6. site_contact_email
  7. site_title
  8. site_description
  9. site_extended_description
  10. site_terms
  11. open_registrations
  12. closed_registrations_message
  13. open_deletion
  14. timeline_preview
  15. show_staff_badge
  16. bootstrap_timeline_accounts
  17. thumbnail
  18. hero
  19. min_invite_role
  20. activity_api_enabled
  21. peers_api_enabled
  22. show_known_fediverse_at_about_page
  23. ).freeze
  24. BOOLEAN_SETTINGS = %w(
  25. open_registrations
  26. open_deletion
  27. timeline_preview
  28. show_staff_badge
  29. activity_api_enabled
  30. peers_api_enabled
  31. show_known_fediverse_at_about_page
  32. ).freeze
  33. UPLOAD_SETTINGS = %w(
  34. thumbnail
  35. hero
  36. ).freeze
  37. def edit
  38. authorize :settings, :show?
  39. @admin_settings = Form::AdminSettings.new
  40. end
  41. def update
  42. authorize :settings, :update?
  43. settings_params.each do |key, value|
  44. if UPLOAD_SETTINGS.include?(key)
  45. upload = SiteUpload.where(var: key).first_or_initialize(var: key)
  46. upload.update(file: value)
  47. else
  48. setting = Setting.where(var: key).first_or_initialize(var: key)
  49. setting.update(value: value_for_update(key, value))
  50. end
  51. end
  52. flash[:notice] = I18n.t('generic.changes_saved_msg')
  53. redirect_to edit_admin_settings_path
  54. end
  55. private
  56. def settings_params
  57. params.require(:form_admin_settings).permit(ADMIN_SETTINGS)
  58. end
  59. def value_for_update(key, value)
  60. if BOOLEAN_SETTINGS.include?(key)
  61. value == '1'
  62. else
  63. value
  64. end
  65. end
  66. end
  67. end