catstodon/app/controllers/admin/settings_controller.rb
Matt Jankowski 1ada494bb2 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
2017-04-20 17:18:09 +02:00

39 lines
864 B
Ruby

# frozen_string_literal: true
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])
@setting.update(value: value_for_update)
respond_to do |format|
format.html { redirect_to admin_settings_path }
format.json { respond_with_bip(@setting) }
end
end
private
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