variables_controller.rb 1.19 KB
Newer Older
shinya maeda's avatar
shinya maeda committed
1 2 3 4
module Groups
  class VariablesController < Groups::ApplicationController
    before_action :authorize_admin_build!

5 6
    skip_cross_project_access_check :show, :update

7 8 9
    def show
      respond_to do |format|
        format.json do
10
          render status: :ok, json: { variables: GroupVariableSerializer.new.represent(@group.variables) }
11 12 13 14 15
        end
      end
    end

    def update
16
      if @group.update(group_variables_params)
17
        respond_to do |format|
18
          format.json { return render_group_variables }
19 20 21
        end
      else
        respond_to do |format|
22
          format.json { render_error }
23 24 25 26
        end
      end
    end

shinya maeda's avatar
shinya maeda committed
27 28
    private

29 30 31 32 33 34 35 36 37
    def render_group_variables
      render status: :ok, json: { variables: GroupVariableSerializer.new.represent(@group.variables) }
    end

    def render_error
      render status: :bad_request, json: @group.errors.full_messages
    end

    def group_variables_params
38 39 40
      params.permit(variables_attributes: [*variable_params_attributes])
    end

41
    def variable_params_attributes
42
      %i[id key value protected _destroy]
shinya maeda's avatar
shinya maeda committed
43 44
    end

45 46 47
    def authorize_admin_build!
      return render_404 unless can?(current_user, :admin_build, group)
    end
shinya maeda's avatar
shinya maeda committed
48 49
  end
end