2017-05-03 14:51:55 -04:00
|
|
|
module Groups
|
|
|
|
class VariablesController < Groups::ApplicationController
|
|
|
|
before_action :authorize_admin_build!
|
|
|
|
|
2018-01-29 12:54:16 -05:00
|
|
|
def show
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
variables = @group.variables
|
|
|
|
.map { |variable| variable.present(current_user: current_user) }
|
|
|
|
|
|
|
|
render status: :ok, json: { variables: variables }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2018-01-23 13:24:55 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2018-01-29 18:44:53 -05:00
|
|
|
if @group.update(variables_params)
|
|
|
|
variables = @group.variables
|
|
|
|
.map { |variable| variable.present(current_user: current_user) }
|
|
|
|
|
|
|
|
return render status: :ok, json: { variables: variables }
|
|
|
|
end
|
2018-01-23 13:24:55 -05:00
|
|
|
|
2018-01-25 06:59:52 -05:00
|
|
|
render status: :bad_request, json: @group.errors.full_messages
|
2018-01-23 13:24:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-03 14:51:55 -04:00
|
|
|
private
|
|
|
|
|
2018-01-23 13:24:55 -05:00
|
|
|
def variables_params
|
|
|
|
params.permit(variables_attributes: [*variable_params_attributes])
|
|
|
|
end
|
|
|
|
|
2017-07-07 04:14:29 -04:00
|
|
|
def variable_params_attributes
|
2018-01-23 13:24:55 -05:00
|
|
|
%i[id key value protected _destroy]
|
2017-05-03 14:51:55 -04:00
|
|
|
end
|
|
|
|
|
2017-07-07 04:14:29 -04:00
|
|
|
def authorize_admin_build!
|
|
|
|
return render_404 unless can?(current_user, :admin_build, group)
|
|
|
|
end
|
2017-05-03 14:51:55 -04:00
|
|
|
end
|
|
|
|
end
|