Sha256: 3527d070e5434bde54d58538141e2c13e6d86c613536611494e9fa6966af9088

Contents?: true

Size: 1.4 KB

Versions: 62

Compression:

Stored size: 1.4 KB

Contents

module Workarea
  module Recommendation
    class UserActivity
      include ApplicationDocument

      # id will be either current_user.id if present or session id
      field :_id, type: String, default: -> { BSON::ObjectId.new.to_s }
      field :product_ids, type: Array, default: []
      field :category_ids, type: Array, default: []
      field :searches, type: Array, default: []

      index({ updated_at: 1 })

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

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

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

      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
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.5.26 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.4.45 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.5.25 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.5.23 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.4.44 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.5.22 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.4.43 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.5.21 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.4.42 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.5.20 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.4.41 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.5.19 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.4.40 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.5.18 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.4.39 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.5.17 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.4.38 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.5.16 app/models/workarea/recommendation/user_activity.rb
workarea-core-3.4.37 app/models/workarea/recommendation/user_activity.rb