From b7bf957dd257c80781fad9d70ec500f820e3c688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Fri, 17 Feb 2023 21:36:30 +0800 Subject: [PATCH] Update registration settings --- .../settings/registrations_controller.rb | 31 ++++++++++++++++++- app/models/setting.rb | 8 +---- .../settings/registrations/index.html.erb | 20 +++++++----- config/routes.rb | 4 +-- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/app/controllers/admin/settings/registrations_controller.rb b/app/controllers/admin/settings/registrations_controller.rb index 1abca45..3f2019e 100644 --- a/app/controllers/admin/settings/registrations_controller.rb +++ b/app/controllers/admin/settings/registrations_controller.rb @@ -3,7 +3,36 @@ class Admin::Settings::RegistrationsController < Admin::SettingsController def index end - def update + def create + @errors = ActiveModel::Errors.new(Setting.new) + + setting_params.keys.each do |key| + next if setting_params[key].nil? + + setting = Setting.new(var: key) + setting.value = setting_params[key].strip + unless setting.valid? + @errors.merge!(setting.errors) + end + end + + if @errors.any? + render :index + end + + setting_params.keys.each do |key| + Setting.send("#{key}=", setting_params[key].strip) unless setting_params[key].nil? + end + + redirect_to admin_settings_registrations_path, flash: { + success: "Settings saved" + } + end + + private + + def setting_params + params.require(:setting).permit(:reserved_usernames) end end diff --git a/app/models/setting.rb b/app/models/setting.rb index 16a8590..fb4dae5 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -3,13 +3,7 @@ class Setting < RailsSettings::Base cache_prefix { "v1" } field :reserved_usernames, type: :array, default: %w[ - account - accounts - admin - donations - mail - webmaster - support + account accounts admin donations mail webmaster support ] field :lndhub_enabled, default: (ENV["LNDHUB_API_URL"].present?.to_s || "false"), type: :boolean diff --git a/app/views/admin/settings/registrations/index.html.erb b/app/views/admin/settings/registrations/index.html.erb index 2a327e1..2131e41 100644 --- a/app/views/admin/settings/registrations/index.html.erb +++ b/app/views/admin/settings/registrations/index.html.erb @@ -1,9 +1,9 @@ <%= render HeaderComponent.new(title: "Settings") %> <%= render MainWithSidenavComponent.new(sidenav_partial: 'shared/admin_sidenav_settings') do %> -
-

Registrations

- <%= form_for(Setting.new, url: admin_settings_services_path) do |f| %> + <%= form_for(Setting.new, url: admin_settings_registrations_path) do |f| %> +
+

Registrations

<% if @errors && @errors.any? %>
    @@ -14,7 +14,7 @@
<% end %> -
+
+ +
+

+ <%= f.submit 'Save', class: "btn-md btn-blue w-full md:w-auto" %> +

+
+ <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index eb118cf..7559393 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -45,8 +45,8 @@ Rails.application.routes.draw do get 'lightning', to: 'lightning#index' namespace :settings do - resources 'registrations', only: ['index', 'update'] - resources 'services', only: ['index', 'update'] + resources 'registrations', only: ['index', 'create'] + resources 'services', only: ['index', 'create'] end end