about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorMatt Jankowski <mjankowski@thoughtbot.com>2017-04-20 11:18:09 -0400
committerEugen <eugen@zeonfederated.com>2017-04-20 17:18:09 +0200
commit1ada494bb21658c92a58f8bd9e5cc4d7ebf59b6e (patch)
tree9032e9e73ff1279ad031a77400530bbd40e6ebbd /app
parent4df26b262184ebc75344369f236e2a37e3722513 (diff)
Admin settings controller refactor, add specs, cleanup (#2225)
* Add render_views for admin/settings spec

* Add coverage for admin/settings#update

* Add coverage for admin/settings typecasting open_registrations setting

* Simplify how admin/settings finds the value for updating

* Rely on activerecord to not update a value that hasnt changed

* Add coverage for non-existent setting

* Use a constant for boolean settings
Diffstat (limited to 'app')
-rw-r--r--app/controllers/admin/settings_controller.rb24
1 files changed, 15 insertions, 9 deletions
diff --git a/app/controllers/admin/settings_controller.rb b/app/controllers/admin/settings_controller.rb
index 6cca5c3e3..fc9064068 100644
--- a/app/controllers/admin/settings_controller.rb
+++ b/app/controllers/admin/settings_controller.rb
@@ -2,21 +2,15 @@
 
 module Admin
   class SettingsController < BaseController
+    BOOLEAN_SETTINGS = %w(open_registrations).freeze
+
     def index
       @settings = Setting.all_as_records
     end
 
     def update
       @setting = Setting.where(var: params[:id]).first_or_initialize(var: params[:id])
-      value    = settings_params[:value]
-
-      # Special cases
-      value = value == 'true' if @setting.var == 'open_registrations'
-
-      if @setting.value != value
-        @setting.value = value
-        @setting.save
-      end
+      @setting.update(value: value_for_update)
 
       respond_to do |format|
         format.html { redirect_to admin_settings_path }
@@ -29,5 +23,17 @@ module Admin
     def settings_params
       params.require(:setting).permit(:value)
     end
+
+    def value_for_update
+      if updating_boolean_setting?
+        settings_params[:value] == 'true'
+      else
+        settings_params[:value]
+      end
+    end
+
+    def updating_boolean_setting?
+      BOOLEAN_SETTINGS.include?(params[:id])
+    end
   end
 end