Skip to content

Instantly share code, notes, and snippets.

@sebbean
Created March 17, 2014 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebbean/9602552 to your computer and use it in GitHub Desktop.
Save sebbean/9602552 to your computer and use it in GitHub Desktop.
class HubPushWorker
include Sidekiq::Worker
sidekiq_options unique: true#, unique_unlock_order: :never
def perform(params)
klass = params.delete('class_name').constantize
objs = klass.where(params)
push_spree_obj(objs)
end
def push_spree_obj(objs)
first = objs.first
serializer = serializer_for_spree_obj(first)
case first
when Spree::Product
objs = objs.map(&:variants_including_master).flatten
end
json = ActiveModel::ArraySerializer.new(objs, each_serializer:serializer, root: first.class.to_s.demodulize.downcase).to_json
ap json
end
def serializer_for_spree_obj(obj)
case obj
when Spree::Product
Spree::Hub::ProductSerializer #raise 'must use variant instead of product'
when Spree::Variant
Spree::Hub::ProductSerializer
when Spree::Order
Spree::Hub::OrderSerializer
when Spree::StockItem
Spree::Hub::InventorySerializer
when Spree::Address
Spree::Hub::AddressSerializer
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment