Class: Workarea::Recommendation::UserActivity

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument
Defined in:
app/models/workarea/recommendation/user_activity.rb

Class Method Summary collapse

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.prepend_field(id, field, values) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/workarea/recommendation/user_activity.rb', line 26

def self.prepend_field(id, field, values)
  timestamp = Time.current

  result = collection.update_one(
    { _id: id.to_s },
    {
      '$set' => { updated_at: timestamp },
      '$push' => {
        field => {
          '$each' => Array(values),
          '$position' => 0,
          '$slice' => Workarea.config.max_user_activities
        }
      }
    },
    upsert: true
  )

  if result.documents[0]['nModified'].zero?
    collection.update_one(
      { _id: id.to_s },
      { '$set' => { created_at: timestamp } }
    )
  end
end

.save_category(id, category_id) ⇒ Object



18
19
20
# File 'app/models/workarea/recommendation/user_activity.rb', line 18

def self.save_category(id, category_id)
  prepend_field(id, :category_ids, category_id)
end

.save_product(id, product_id) ⇒ Object



14
15
16
# File 'app/models/workarea/recommendation/user_activity.rb', line 14

def self.save_product(id, product_id)
  prepend_field(id, :product_ids, product_id)
end

.save_search(id, search) ⇒ Object



22
23
24
# File 'app/models/workarea/recommendation/user_activity.rb', line 22

def self.save_search(id, search)
  prepend_field(id, :searches, search)
end