Skip to content

Instantly share code, notes, and snippets.

@hyphenPaul
Created February 23, 2016 14:44
Show Gist options
  • Save hyphenPaul/24784d50065f611652c3 to your computer and use it in GitHub Desktop.
Save hyphenPaul/24784d50065f611652c3 to your computer and use it in GitHub Desktop.
module Weblinc
module Reports
module ProductsMissingCategories
extend MongoCollection
def self.count
collection.find.count
end
def self.log
logger = Logger.new('log/product_classifications.log')
real_results = results.to_a.reject { |x| !!/c/.match(x) }
Weblinc::Catalog::Product.in(id: real_results).all.each do |p|
logger.info("Product ID: #{p.id} Classifcations: #{p.details}")
end
end
def self.results
collection.find.first['ids'].map
end
def self.build
@@found_products = []
return unless defined?(StoreFront)
collection.find.remove_all
Catalog::SmartCategory.where(clearance_product: true).all.each_by(50) { |c| check_category(c) }
add_products
@@found_products = []
end
def self.check_category(category)
search = category_products(category)
return unless search.total > 0
total_pages = (search.total.to_f / search.per_page.to_f).ceil
product_ids = search.results.map { |r| r[:model].id }
if total_pages > 1
product_ids += (2..total_pages).flat_map do |page|
category_products(category, page).results.map { |r| r[:model].id }
end
end
@@found_products.concat(product_ids)
end
def self.add_products
missing_ids = Weblinc::Catalog::Product.where(clearance: true).not_in(id: @@found_products.uniq.compact).only(:id).all.map(&:id)
collection.insert(ids: missing_ids)
end
def self.category_products(category, page = 1)
Search::Queries::CategoryBrowse.new(
category_ids: [],
rules: category.rules,
sort: category.default_sort,
page: page
)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment