2018-10-26 08:49:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Tracks which boards in a specific project a user has visited
|
2019-03-28 09:17:42 -04:00
|
|
|
class BoardProjectRecentVisit < ApplicationRecord
|
2018-10-26 08:49:16 -04:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :project
|
|
|
|
belongs_to :board
|
|
|
|
|
|
|
|
validates :user, presence: true
|
|
|
|
validates :project, presence: true
|
|
|
|
validates :board, presence: true
|
|
|
|
|
2019-02-27 08:08:48 -05:00
|
|
|
scope :by_user_project, -> (user, project) { where(user: user, project: project) }
|
2018-10-26 08:49:16 -04:00
|
|
|
|
|
|
|
def self.visited!(user, board)
|
|
|
|
visit = find_or_create_by(user: user, project: board.project, board: board)
|
2020-05-22 05:08:09 -04:00
|
|
|
visit.touch if visit.updated_at < Time.current
|
2018-10-26 08:49:16 -04:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
2019-02-27 08:08:48 -05:00
|
|
|
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)
|
2018-10-26 08:49:16 -04:00
|
|
|
end
|
|
|
|
end
|