2016-03-23 18:39:41 +01:00
|
|
|
class API::SettingsController < API::ApiController
|
|
|
|
before_action :authenticate_user!, only: :update
|
|
|
|
|
|
|
|
def index
|
|
|
|
@settings = Setting.where(name: names_as_string_to_array)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize Setting
|
2016-08-17 12:49:52 +02:00
|
|
|
@setting = Setting.find_or_initialize_by(name: params[:name])
|
2018-12-17 16:02:02 +01:00
|
|
|
if @setting.save && @setting.history_values.create(value: setting_params[:value], user: current_user)
|
2016-03-23 18:39:41 +01:00
|
|
|
render status: :ok
|
|
|
|
else
|
|
|
|
render json: @setting.errors.full_messages, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@setting = Setting.find_or_create_by(name: params[:name])
|
2019-01-14 15:00:33 +01:00
|
|
|
@show_history = params[:history] == 'true' && current_user.admin?
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-12-17 16:02:02 +01:00
|
|
|
def setting_params
|
|
|
|
params.require(:setting).permit(:value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def names_as_string_to_array
|
|
|
|
params[:names][1..-2].split(',').map(&:strip).map { |param| param[1..-2] }.map(&:strip)
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|