debian-mirror-gitlab/app/models/board_group_recent_visit.rb

29 lines
797 B
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
# Tracks which boards in a specific group a user has visited
2019-05-18 00:54:41 +05:30
class BoardGroupRecentVisit < ApplicationRecord
2018-12-13 13:39:08 +05:30
belongs_to :user
belongs_to :group
belongs_to :board
validates :user, presence: true
validates :group, presence: true
validates :board, presence: true
2019-05-18 00:54:41 +05:30
scope :by_user_group, -> (user, group) { where(user: user, group: group) }
2018-12-13 13:39:08 +05:30
def self.visited!(user, board)
visit = find_or_create_by(user: user, group: board.group, board: board)
visit.touch if visit.updated_at < Time.now
rescue ActiveRecord::RecordNotUnique
retry
end
2019-05-18 00:54:41 +05:30
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)
2018-12-13 13:39:08 +05:30
end
end