Commit 313d4159 authored by Sean McGivern's avatar Sean McGivern

Merge branch '7714-view-recent-boards' into 'master'

Add recent boards endpoint

See merge request gitlab-org/gitlab-ee!9705
parents 0d9de4e9 5132825b
......@@ -10,7 +10,7 @@ class BoardGroupRecentVisit < ActiveRecord::Base
validates :group, presence: true
validates :board, presence: true
scope :by_user_group, -> (user, group) { where(user: user, group: group).order(:updated_at) }
scope :by_user_group, -> (user, group) { where(user: user, group: group) }
def self.visited!(user, board)
visit = find_or_create_by(user: user, group: board.group, board: board)
......@@ -19,7 +19,10 @@ class BoardGroupRecentVisit < ActiveRecord::Base
retry
end
def self.latest(user, group)
by_user_group(user, group).last
def self.latest(user, group, count: nil)
visits = by_user_group(user, group).order(updated_at: :desc)
visits = visits.preload(:board) if count && count > 1
visits.first(count)
end
end
......@@ -10,7 +10,7 @@ class BoardProjectRecentVisit < ActiveRecord::Base
validates :project, presence: true
validates :board, presence: true
scope :by_user_project, -> (user, project) { where(user: user, project: project).order(:updated_at) }
scope :by_user_project, -> (user, project) { where(user: user, project: project) }
def self.visited!(user, board)
visit = find_or_create_by(user: user, project: board.project, board: board)
......@@ -19,7 +19,10 @@ class BoardProjectRecentVisit < ActiveRecord::Base
retry
end
def self.latest(user, project)
by_user_project(user, project).last
def self.latest(user, project, count: nil)
visits = by_user_project(user, project).order(updated_at: :desc)
visits = visits.preload(:board) if count && count > 1
visits.first(count)
end
end
......@@ -6,11 +6,13 @@ module Boards
def execute
return nil unless current_user
if parent.is_a?(Group)
BoardGroupRecentVisit.latest(current_user, parent)
else
BoardProjectRecentVisit.latest(current_user, parent)
recent_visit_model.latest(current_user, parent, count: params[:count])
end
private
def recent_visit_model
parent.is_a?(Group) ? BoardGroupRecentVisit : BoardProjectRecentVisit
end
end
end
......
......@@ -67,7 +67,7 @@ constraints(::Constraints::GroupUrlConstrainer.new) do
end
end
resources :boards, only: [:index, :show]
resources :boards, only: [:index, :show], constraints: { id: /\d+/ }
resources :runners, only: [:index, :edit, :update, :destroy, :show] do
member do
......
......@@ -475,8 +475,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
get 'noteable/:target_type/:target_id/notes' => 'notes#index', as: 'noteable_notes'
# On CE only index and show are needed
resources :boards, only: [:index, :show, :create, :update, :destroy]
resources :boards, only: [:index, :show], constraints: { id: /\d+/ }
resources :todos, only: [:create]
......
......@@ -12,6 +12,13 @@ module EE
before_action :authorize_admin_board!, only: [:create, :update, :destroy]
end
def recent
recent_visits = ::Boards::Visits::LatestService.new(parent, current_user, count: 5).execute
recent_boards = recent_visits.map(&:board)
render json: serialize_as_json(recent_boards)
end
def create
board = ::Boards::CreateService.new(parent, current_user, board_params).execute
......
......@@ -67,7 +67,11 @@ constraints(::Constraints::GroupUrlConstrainer.new) do
end
resources :todos, only: [:create]
resources :boards, only: [:create, :update, :destroy]
resources :boards, only: [:create, :update, :destroy] do
collection do
get :recent
end
end
namespace :security do
resource :dashboard, only: [:show], controller: :dashboard
......
......@@ -17,6 +17,12 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
end
end
resources :boards, only: [:create, :update, :destroy] do
collection do
get :recent
end
end
resources :web_ide_terminals, path: :ide_terminals, only: [:create, :show], constraints: { id: /\d+/, format: :json } do
member do
post :cancel
......
......@@ -52,6 +52,12 @@ describe Groups::BoardsController do
end
end
describe 'GET recent' do
let(:parent) { group }
it_behaves_like 'returns recently visited boards'
end
describe 'GET show' do
context 'for multiple issue boards' do
let(:parent) { group }
......
......@@ -38,6 +38,12 @@ describe Projects::BoardsController do
end
end
describe 'GET recent' do
let(:parent) { project }
it_behaves_like 'returns recently visited boards'
end
describe 'GET show' do
let(:parent) { project }
......
# frozen_string_literal: true
require 'spec_helper'
shared_examples 'returns recently visited boards' do
let(:boards) { create_list(:board, 8, parent: parent) }
it 'returns last 5 visited boards' do
[0, 2, 5, 3, 7, 1].each_with_index do |board_index, i|
visit_board(boards[board_index], Time.now + i.minutes)
end
get_recent_boards
expect(json_response.length).to eq(5)
expect(json_response.map { |b| b['id'] }).to eq([1, 7, 3, 5, 2].map { |i| boards[i].id })
end
def visit_board(board, time)
if parent.is_a?(Group)
create(:board_group_recent_visit, group: parent, board: board, user: user, updated_at: time)
else
create(:board_project_recent_visit, project: parent, board: board, user: user, updated_at: time)
end
end
def get_recent_boards
params = if parent.is_a?(Group)
{ group_id: parent }
else
{ namespace_id: parent.namespace, project_id: parent }
end
get :recent, params: params
end
end
......@@ -50,15 +50,25 @@ describe BoardGroupRecentVisit do
end
describe '#latest' do
it 'returns the most recent visited' do
board2 = create(:board, group: group)
board3 = create(:board, group: group)
def create_visit(time)
create :board_group_recent_visit, group: group, user: user, updated_at: time
end
create :board_group_recent_visit, group: board.group, board: board, user: user, updated_at: 7.days.ago
create :board_group_recent_visit, group: board2.group, board: board2, user: user, updated_at: 5.days.ago
recent = create :board_group_recent_visit, group: board3.group, board: board3, user: user, updated_at: 1.day.ago
it 'returns the most recent visited' do
create_visit(7.days.ago)
create_visit(5.days.ago)
recent = create_visit(1.day.ago)
expect(described_class.latest(user, group)).to eq recent
end
it 'returns last 3 visited boards' do
create_visit(7.days.ago)
visit1 = create_visit(3.days.ago)
visit2 = create_visit(2.days.ago)
visit3 = create_visit(5.days.ago)
expect(described_class.latest(user, group, count: 3)).to eq([visit2, visit1, visit3])
end
end
end
......@@ -50,15 +50,25 @@ describe BoardProjectRecentVisit do
end
describe '#latest' do
it 'returns the most recent visited' do
board2 = create(:board, project: project)
board3 = create(:board, project: project)
def create_visit(time)
create :board_project_recent_visit, project: project, user: user, updated_at: time
end
create :board_project_recent_visit, project: board.project, board: board, user: user, updated_at: 7.days.ago
create :board_project_recent_visit, project: board2.project, board: board2, user: user, updated_at: 5.days.ago
recent = create :board_project_recent_visit, project: board3.project, board: board3, user: user, updated_at: 1.day.ago
it 'returns the most recent visited' do
create_visit(7.days.ago)
create_visit(5.days.ago)
recent = create_visit(1.day.ago)
expect(described_class.latest(user, project)).to eq recent
end
it 'returns last 3 visited boards' do
create_visit(7.days.ago)
visit1 = create_visit(3.days.ago)
visit2 = create_visit(2.days.ago)
visit3 = create_visit(5.days.ago)
expect(described_class.latest(user, project, count: 3)).to eq([visit2, visit1, visit3])
end
end
end
......@@ -23,6 +23,12 @@ describe Boards::Visits::LatestService do
service.execute
end
it 'queries for last N visits' do
expect(BoardProjectRecentVisit).to receive(:latest).with(user, project, count: 5).once
described_class.new(project_board.parent, user, count: 5).execute
end
end
context 'when a group board' do
......@@ -42,6 +48,12 @@ describe Boards::Visits::LatestService do
service.execute
end
it 'queries for last N visits' do
expect(BoardGroupRecentVisit).to receive(:latest).with(user, group, count: 5).once
described_class.new(group_board.parent, user, count: 5).execute
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment