summaryrefslogtreecommitdiff
path: root/app/controllers/concerns/boards_actions.rb
blob: 2cf0ba7c0862710da7c025f815b9c43c97f0303c (plain)
1
2
3
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

module BoardsActions
  include Gitlab::Utils::StrongMemoize
  extend ActiveSupport::Concern

  included do
    include BoardsResponses

    before_action :redirect_to_recent_board, only: :index
    before_action :boards, only: :index
    before_action :board, only: :show
  end

  def index
    respond_with_boards
  end

  def show
    # Add / update the board in the recent visits table
    Boards::Visits::CreateService.new(parent, current_user).execute(board) if request.format.html?

    respond_with_board
  end

  private

  def redirect_to_recent_board
    return if request.format.json?

    if recently_visited = Boards::Visits::LatestService.new(board_parent, current_user).execute
      board_path = case board_parent
                   when Project
                     namespace_project_board_path(id: recently_visited.board_id)
                   when Group
                     group_board_path(id: recently_visited.board_id)
                   end

      redirect_to board_path
    end
  end

  def boards
    strong_memoize(:boards) do
      Boards::ListService.new(parent, current_user).execute
    end
  end

  def board
    strong_memoize(:board) do
      boards.find(params[:id])
    end
  end
end