about summary refs log tree commit diff
path: root/app/controllers/admin/settings_controller.rb
blob: fc9064068c361238f91279d2eaa6178c75cd145b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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