boards_responses.rb 1.7 KB
Newer Older
1 2
# frozen_string_literal: true

F
Felipe Artur 已提交
3
module BoardsResponses
F
Felipe Artur 已提交
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
  include Gitlab::Utils::StrongMemoize

  def board_params
    params.require(:board).permit(:name, :weight, :milestone_id, :assignee_id, label_ids: [])
  end

  def parent
    strong_memoize(:parent) do
      group? ? group : project
    end
  end

  def boards_path
    if group?
      group_boards_path(parent)
    else
      project_boards_path(parent)
    end
  end

  def board_path(board)
    if group?
      group_board_path(parent, board)
    else
      project_board_path(parent, board)
    end
  end

  def group?
    instance_variable_defined?(:@group)
  end

F
Felipe Artur 已提交
36
  def authorize_read_list
37
    authorize_action_for!(board, :read_list)
F
Felipe Artur 已提交
38 39 40
  end

  def authorize_read_issue
41
    authorize_action_for!(board, :read_issue)
F
Felipe Artur 已提交
42 43 44 45 46 47 48
  end

  def authorize_update_issue
    authorize_action_for!(issue, :admin_issue)
  end

  def authorize_create_issue
49 50
    list = List.find(issue_params[:list_id])
    action = list.backlog? ? :create_issue : :admin_issue
51

52
    authorize_action_for!(project, action)
F
Felipe Artur 已提交
53 54 55
  end

  def authorize_admin_list
56
    authorize_action_for!(board, :admin_list)
F
Felipe Artur 已提交
57 58 59 60 61 62 63
  end

  def authorize_action_for!(resource, ability)
    return render_403 unless can?(current_user, ability, resource)
  end

  def respond_with_boards
64
    respond_with(@boards) # rubocop:disable Gitlab/ModuleWithInstanceVariables
F
Felipe Artur 已提交
65 66 67
  end

  def respond_with_board
68
    respond_with(@board) # rubocop:disable Gitlab/ModuleWithInstanceVariables
F
Felipe Artur 已提交
69 70
  end

F
Felipe Artur 已提交
71 72 73 74
  def serialize_as_json(resource)
    resource.as_json(only: [:id])
  end

F
Felipe Artur 已提交
75 76 77 78 79 80 81 82 83
  def respond_with(resource)
    respond_to do |format|
      format.html
      format.json do
        render json: serialize_as_json(resource)
      end
    end
  end
end