Commit 5819115a authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Allow querying of last N visits to issue boards

Adds additional count param to Boards::Visits::LatestService
parent 99a00859
...@@ -10,7 +10,7 @@ class BoardGroupRecentVisit < ActiveRecord::Base ...@@ -10,7 +10,7 @@ class BoardGroupRecentVisit < ActiveRecord::Base
validates :group, presence: true validates :group, presence: true
validates :board, 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) def self.visited!(user, board)
visit = find_or_create_by(user: user, group: board.group, board: board) visit = find_or_create_by(user: user, group: board.group, board: board)
...@@ -19,7 +19,7 @@ class BoardGroupRecentVisit < ActiveRecord::Base ...@@ -19,7 +19,7 @@ class BoardGroupRecentVisit < ActiveRecord::Base
retry retry
end end
def self.latest(user, group) def self.latest(user, group, count = nil)
by_user_group(user, group).last by_user_group(user, group).order(updated_at: :desc).first(count)
end end
end end
...@@ -10,7 +10,7 @@ class BoardProjectRecentVisit < ActiveRecord::Base ...@@ -10,7 +10,7 @@ class BoardProjectRecentVisit < ActiveRecord::Base
validates :project, presence: true validates :project, presence: true
validates :board, 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) def self.visited!(user, board)
visit = find_or_create_by(user: user, project: board.project, board: board) visit = find_or_create_by(user: user, project: board.project, board: board)
...@@ -19,7 +19,7 @@ class BoardProjectRecentVisit < ActiveRecord::Base ...@@ -19,7 +19,7 @@ class BoardProjectRecentVisit < ActiveRecord::Base
retry retry
end end
def self.latest(user, project) def self.latest(user, project, count = nil)
by_user_project(user, project).last by_user_project(user, project).order(updated_at: :desc).first(count)
end end
end end
...@@ -6,11 +6,19 @@ module Boards ...@@ -6,11 +6,19 @@ module Boards
def execute def execute
return nil unless current_user return nil unless current_user
if parent.is_a?(Group) relation = recent_visit_model
BoardGroupRecentVisit.latest(current_user, parent)
else if params[:count] && params[:count] > 1
BoardProjectRecentVisit.latest(current_user, parent) relation = relation.preload(:board)
end end
relation.latest(current_user, parent, params[:count])
end
private
def recent_visit_model
parent.is_a?(Group) ? BoardGroupRecentVisit : BoardProjectRecentVisit
end end
end end
end end
......
...@@ -51,14 +51,20 @@ describe BoardGroupRecentVisit do ...@@ -51,14 +51,20 @@ describe BoardGroupRecentVisit do
describe '#latest' do describe '#latest' do
it 'returns the most recent visited' do it 'returns the most recent visited' do
board2 = create(:board, group: group) create :board_group_recent_visit, group: group, user: user, updated_at: 7.days.ago
board3 = create(:board, group: group) create :board_group_recent_visit, group: group, user: user, updated_at: 5.days.ago
recent = create :board_group_recent_visit, group: group, user: user, updated_at: 1.day.ago
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
expect(described_class.latest(user, group)).to eq recent expect(described_class.latest(user, group)).to eq recent
end end
it 'returns last 3 visited boards' do
create :board_group_recent_visit, group: group, user: user, updated_at: 7.days.ago
visit1 = create :board_group_recent_visit, group: group, user: user, updated_at: 3.days.ago
visit2 = create :board_group_recent_visit, group: group, user: user, updated_at: 2.days.ago
visit3 = create :board_group_recent_visit, group: group, user: user, updated_at: 5.days.ago
expect(described_class.latest(user, group, 3)).to eq([visit2, visit1, visit3])
end
end end
end end
...@@ -51,14 +51,20 @@ describe BoardProjectRecentVisit do ...@@ -51,14 +51,20 @@ describe BoardProjectRecentVisit do
describe '#latest' do describe '#latest' do
it 'returns the most recent visited' do it 'returns the most recent visited' do
board2 = create(:board, project: project) create :board_project_recent_visit, project: project, user: user, updated_at: 7.days.ago
board3 = create(:board, project: project) create :board_project_recent_visit, project: project, user: user, updated_at: 5.days.ago
recent = create :board_project_recent_visit, project: project, user: user, updated_at: 1.day.ago
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
expect(described_class.latest(user, project)).to eq recent expect(described_class.latest(user, project)).to eq recent
end end
it 'returns last 3 visited boards' do
create :board_project_recent_visit, project: project, user: user, updated_at: 7.days.ago
visit1 = create :board_project_recent_visit, project: project, user: user, updated_at: 3.days.ago
visit2 = create :board_project_recent_visit, project: project, user: user, updated_at: 2.days.ago
visit3 = create :board_project_recent_visit, project: project, user: user, updated_at: 5.days.ago
expect(described_class.latest(user, project, 3)).to eq([visit2, visit1, visit3])
end
end end
end end
...@@ -23,6 +23,12 @@ describe Boards::Visits::LatestService do ...@@ -23,6 +23,12 @@ describe Boards::Visits::LatestService do
service.execute service.execute
end end
it 'queries for last N visits' do
expect(BoardProjectRecentVisit).to receive(:latest).with(user, project, 5).once
described_class.new(project_board.parent, user, count: 5).execute
end
end end
context 'when a group board' do context 'when a group board' do
...@@ -42,6 +48,12 @@ describe Boards::Visits::LatestService do ...@@ -42,6 +48,12 @@ describe Boards::Visits::LatestService do
service.execute service.execute
end end
it 'queries for last N visits' do
expect(BoardGroupRecentVisit).to receive(:latest).with(user, group, 5).once
described_class.new(group_board.parent, user, count: 5).execute
end
end end
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