Class: SpreeCmCommissioner::Feed::TaxonProduct
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::Feed::TaxonProduct
- Defined in:
- app/services/spree_cm_commissioner/feed/taxon_product.rb
Constant Summary collapse
- LIMIT =
4
Instance Attribute Summary collapse
-
#taxon ⇒ Object
Returns the value of attribute taxon.
Class Method Summary collapse
- .call(taxon_ids, options = {}) ⇒ Object
- .query_product_in_taxon(taxon_ids) ⇒ Object
- .serilize_class(serialize_data) ⇒ Object
Instance Method Summary collapse
-
#initialize(taxon:, products:) ⇒ TaxonProduct
constructor
A new instance of TaxonProduct.
- #products ⇒ Object
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
#taxon ⇒ Object
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, = {}) limit = [: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([: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
#products ⇒ Object
13 14 15 |
# File 'app/services/spree_cm_commissioner/feed/taxon_product.rb', line 13 def products (@products.presence || []) end |