admin_settings.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # frozen_string_literal: true
  2. class Form::AdminSettings
  3. include ActiveModel::Model
  4. KEYS = %i(
  5. site_contact_username
  6. site_contact_email
  7. site_title
  8. site_short_description
  9. site_description
  10. site_extended_description
  11. site_terms
  12. registrations_mode
  13. closed_registrations_message
  14. open_deletion
  15. timeline_preview
  16. show_staff_badge
  17. enable_bootstrap_timeline_accounts
  18. bootstrap_timeline_accounts
  19. theme
  20. min_invite_role
  21. activity_api_enabled
  22. peers_api_enabled
  23. show_known_fediverse_at_about_page
  24. preview_sensitive_media
  25. custom_css
  26. profile_directory
  27. thumbnail
  28. hero
  29. mascot
  30. spam_check_enabled
  31. trends
  32. trendable_by_default
  33. show_domain_blocks
  34. show_domain_blocks_rationale
  35. noindex
  36. ).freeze
  37. BOOLEAN_KEYS = %i(
  38. open_deletion
  39. timeline_preview
  40. show_staff_badge
  41. enable_bootstrap_timeline_accounts
  42. activity_api_enabled
  43. peers_api_enabled
  44. show_known_fediverse_at_about_page
  45. preview_sensitive_media
  46. profile_directory
  47. spam_check_enabled
  48. trends
  49. trendable_by_default
  50. noindex
  51. ).freeze
  52. UPLOAD_KEYS = %i(
  53. thumbnail
  54. hero
  55. mascot
  56. ).freeze
  57. attr_accessor(*KEYS)
  58. validates :site_short_description, :site_description, html: { wrap_with: :p }
  59. validates :site_extended_description, :site_terms, :closed_registrations_message, html: true
  60. validates :registrations_mode, inclusion: { in: %w(open approved none) }
  61. validates :min_invite_role, inclusion: { in: %w(disabled user moderator admin) }
  62. validates :site_contact_email, :site_contact_username, presence: true
  63. validates :site_contact_username, existing_username: true
  64. validates :bootstrap_timeline_accounts, existing_username: { multiple: true }
  65. validates :show_domain_blocks, inclusion: { in: %w(disabled users all) }
  66. validates :show_domain_blocks_rationale, inclusion: { in: %w(disabled users all) }
  67. def initialize(_attributes = {})
  68. super
  69. initialize_attributes
  70. end
  71. def save
  72. return false unless valid?
  73. KEYS.each do |key|
  74. value = instance_variable_get("@#{key}")
  75. if UPLOAD_KEYS.include?(key) && !value.nil?
  76. upload = SiteUpload.where(var: key).first_or_initialize(var: key)
  77. upload.update(file: value)
  78. else
  79. setting = Setting.where(var: key).first_or_initialize(var: key)
  80. setting.update(value: typecast_value(key, value))
  81. end
  82. end
  83. end
  84. private
  85. def initialize_attributes
  86. KEYS.each do |key|
  87. instance_variable_set("@#{key}", Setting.public_send(key)) if instance_variable_get("@#{key}").nil?
  88. end
  89. end
  90. def typecast_value(key, value)
  91. if BOOLEAN_KEYS.include?(key)
  92. value == '1'
  93. else
  94. value
  95. end
  96. end
  97. end