Class: SpreeCmCommissioner::Feed::VendorProduct

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/feed/vendor_product.rb

Constant Summary collapse

LIMIT =
4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vendor:, products:) ⇒ VendorProduct

Returns a new instance of VendorProduct.



8
9
10
11
# File 'app/services/spree_cm_commissioner/feed/vendor_product.rb', line 8

def initialize(vendor:, products:)
  @vendor = vendor
  @products = products
end

Instance Attribute Details

#vendorObject

Returns the value of attribute vendor.



4
5
6
# File 'app/services/spree_cm_commissioner/feed/vendor_product.rb', line 4

def vendor
  @vendor
end

Class Method Details

.call(vendor_ids, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/spree_cm_commissioner/feed/vendor_product.rb', line 17

def self.call(vendor_ids, options = {})
  limit = options[:limit] || LIMIT
  from_mobile = options[:from_mobile] || false
  return [] if vendor_ids.blank?

  vendors = Spree::Vendor.where(id: vendor_ids).order(priority: :desc)
  vendors = vendors.includes(app_promotion_banner: :attachment_blob) if from_mobile

  sub_query = query_product_in_vendor(vendor_ids).to_sql

  vendor_products = Spree::Product.select('*').from(" ( #{sub_query} ) AS spree_products")
                                  .where(['spree_products.product_top_rank <= ?', limit])
                                  .includes(master: :prices)

  vendor_products = vendor_products.to_a

  vendors.map do |vendor|
    serilize_class = serilize_class(options[:serialize_data])

    products = vendor_products.select { |vendor_product| vendor_product.vendor_id == vendor.id }
    serilize_class.new(vendor: vendor, products: products)
  end
end

.query_product_in_vendor(vendor_ids) ⇒ Object



45
46
47
48
49
# File 'app/services/spree_cm_commissioner/feed/vendor_product.rb', line 45

def self.query_product_in_vendor(vendor_ids)
  select_stm = 'spree_products.*, DENSE_RANK() OVER( PARTITION BY vendor_id ORDER BY created_at DESC ) AS product_top_rank'
  SpreeCmCommissioner::Feed.query_active_products.select(select_stm)
                           .where(vendor_id: vendor_ids)
end

.serilize_class(serialize_data) ⇒ Object



41
42
43
# File 'app/services/spree_cm_commissioner/feed/vendor_product.rb', line 41

def self.serilize_class(serialize_data)
  serialize_data ? SpreeCmCommissioner::Feed::VendorIncludeProduct : SpreeCmCommissioner::Feed::VendorProduct
end

Instance Method Details

#productsObject



13
14
15
# File 'app/services/spree_cm_commissioner/feed/vendor_product.rb', line 13

def products
  (@products.presence || [])
end