Class: Gemgento::Adapter::Shopify::Category

Inherits:
Object
  • Object
show all
Defined in:
app/models/gemgento/adapter/shopify/category.rb

Class Method Summary collapse

Class Method Details

.importvoid

This method returns an undefined value.

Import all Shopify collections.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/gemgento/adapter/shopify/category.rb', line 9

def self.import
  page = 1
  ShopifyAPI::Base.site = Gemgento::Adapter::ShopifyAdapter.api_url
  shopify_collections = ShopifyAPI::CustomCollection.where(limit: 250, page: page)

  while shopify_collections.any?
    shopify_collections.each do |collection|
      sync_shopify_category(collection)
    end

    page = page + 1
    shopify_collections = ShopifyAPI::CustomCollection.where(limit: 250, page: page)
  end
end

.sync_shopify_category(collection) ⇒ Gemgento::Category

Create Gemgento::Category from Shopify collection.

Parameters:

  • collection (ShopifyAPI::CustomCollection)

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/gemgento/adapter/shopify/category.rb', line 28

def self.sync_shopify_category(collection)
  if shopify_adapter = Gemgento::Adapter::ShopifyAdapter.find_by_shopify_model(collection)
    category = shopify_adapter.gemgento_model
  else
    category = Gemgento::Category.new
  end

  category.url_key = collection.handle
  category.name = collection.title
  category.parent = Gemgento::Category.root
  category.image = URI.parse(collection.image) if collection.has_attribute? :image
  category.is_active = collection.published_at ? true : false
  category.include_in_menu = false
  category.stores = Gemgento::Store.where.not(code: 'admin')
  category.sync_needed = true
  category.save

  Gemgento::Adapter::ShopifyAdapter.create_association(category, collection)

  return category
end