Class: UserInteractedProject

Inherits:
ApplicationRecord show all
Defined in:
app/models/user_interacted_project.rb

Constant Summary collapse

CACHE_EXPIRY_TIME =
1.day

Class Method Summary collapse

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.track(event) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/user_interacted_project.rb', line 13

def track(event)
  # For events without a project, we simply don't care.
  # An example of this is the creation of a snippet (which
  # is not related to any project).
  return unless event.project_id

  attributes = {
    project_id: event.project_id,
    user_id: event.author_id
  }

  cached_exists?(attributes) do
    transaction(requires_new: true) do
      where(attributes).select(1).first || create!(attributes)
      true # not caching the whole record here for now
    rescue ActiveRecord::RecordNotUnique
      # Note, above queries are not atomic and prone
      # to race conditions (similar like #find_or_create!).
      # In the case where we hit this, the record we want
      # already exists - shortcut and return.
      true
    end
  end
end