summaryrefslogtreecommitdiff
path: root/app/models/concerns/board_recent_visit.rb
blob: c1c8307500e8b4c7fd1a80bdbf0b3e041a9d1307 (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
# frozen_string_literal: true

module BoardRecentVisit
  extend ActiveSupport::Concern

  class_methods do
    def visited!(user, board)
      find_or_create_by(
        "user" => user,
        board_parent_relation => board.resource_parent,
        board_relation => board
      ).tap(&:touch)
    rescue ActiveRecord::RecordNotUnique
      retry
    end

    def latest(user, parent, count: nil)
      visits = by_user_parent(user, parent).order(updated_at: :desc)
      visits = visits.preload(board_relation)

      visits.first(count)
    end

    def board_relation
      :board
    end

    def board_parent_relation
      raise NotImplementedError
    end
  end
end