1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/controllers/api/settings_controller.rb
Sylvain 0cd841da33 rubocop api controllers
TODO:
 - events controller
 - availabilies controller
 - members controller
 - plans controller
2019-01-16 16:28:25 +01:00

36 lines
964 B
Ruby

# frozen_string_literal: true
# API Controller for resources of type Setting
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
@setting = Setting.find_or_initialize_by(name: params[:name])
if @setting.save && @setting.history_values.create(value: setting_params[:value], user: current_user)
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])
@show_history = params[:history] == 'true' && current_user.admin?
end
private
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
end