Class: SpreeCmCommissioner::Feed::TaxonProduct

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

Constant Summary collapse

LIMIT =
4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(taxon:, products:) ⇒ TaxonProduct

Returns a new instance of TaxonProduct.



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

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

Instance Attribute Details

#taxonObject

Returns the value of attribute taxon.



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

def taxon
  @taxon
end

Class Method Details

.call(taxon_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
40
# File 'app/services/spree_cm_commissioner/feed/taxon_product.rb', line 17

def self.call(taxon_ids, options = {})
  limit = options[:limit] || LIMIT

  return [] if taxon_ids.blank?

  taxons = Spree::Taxon.where(id: taxon_ids).order(:lft)

  sub_query = query_product_in_taxon(taxon_ids).to_sql

  taxon_products = Spree::Product.select('spree_products.*, rank_taxon_products.taxon_id as taxon_id')
                                 .joins("INNER JOIN (#{sub_query}) AS rank_taxon_products ON spree_products.id= rank_taxon_products.product_id")
                                 .where(['rank_taxon_products.product_top_rank <= ?', limit])
                                 .includes(master: :prices)

  taxon_products = taxon_products.to_a

  taxons.map do |taxon|
    serilize_class = serilize_class(options[:serialize_data])

    # taxon_product contain attribute taxon_id get from the query above
    products = taxon_products.select { |taxon_product| taxon_product.read_attribute_before_type_cast('taxon_id') == taxon.id }
    serilize_class.new(taxon: taxon, products: products)
  end
end

.query_product_in_taxon(taxon_ids) ⇒ Object



46
47
48
49
50
51
# File 'app/services/spree_cm_commissioner/feed/taxon_product.rb', line 46

def self.query_product_in_taxon(taxon_ids)
  select_stm = 'spree_products_taxons.*, DENSE_RANK() OVER( PARTITION BY taxon_id ORDER BY position ASC ) AS product_top_rank'
  Spree::Classification.select(select_stm)
                       .where(taxon_id: taxon_ids) # classifications table is spree_products_taxons
                       .where("product_id in (#{SpreeCmCommissioner::Feed.query_active_products.select('id').to_sql})")
end

.serilize_class(serialize_data) ⇒ Object



42
43
44
# File 'app/services/spree_cm_commissioner/feed/taxon_product.rb', line 42

def self.serilize_class(serialize_data)
  serialize_data ? SpreeCmCommissioner::Feed::TaxonIncludeProduct : SpreeCmCommissioner::Feed::TaxonProduct
end

Instance Method Details

#productsObject



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

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