class Commercelayer::CLI::Bootstrappers::DatoCMS

Public Class Methods

new(options={}) click to toggle source
# File lib/commercelayer/cli/bootstrappers/datocms.rb, line 8
def initialize(options={})
  puts "Clearing item types..."
  client.item_types.all.each do |item_type|
    client.item_types.destroy(item_type[:id])
  end
  puts "Clearing uploads..."
  client.uploads.all.each do |upload|
    client.uploads.destroy(upload[:id])
  end
end

Public Instance Methods

bootstrap!() click to toggle source
# File lib/commercelayer/cli/bootstrappers/datocms.rb, line 19
def bootstrap!
  create_product_model!
  create_variant_model!
  create_product_model_fields!
  create_variant_model_fields!
  create_records!
end

Private Instance Methods

client() click to toggle source
# File lib/commercelayer/cli/bootstrappers/datocms.rb, line 226
def client
  ::Dato::Site::Client.new(config_data[:dato][:api_key])
end
create_product_model!() click to toggle source
# File lib/commercelayer/cli/bootstrappers/datocms.rb, line 28
def create_product_model!
  puts "Creating product model..."
  @product_model = client.item_types.create({
    api_key: "product",
    name: "Product",
    singleton: false,
    all_locales_required: false,
    sortable: true,
    modular_block: false,
    draft_mode_active: false,
    tree: false,
    ordering_direction: nil,
    ordering_field: nil
  })
end
create_product_model_fields!() click to toggle source
# File lib/commercelayer/cli/bootstrappers/datocms.rb, line 60
def create_product_model_fields!
  puts "Creating product model fields..."
  product_model_fields.each do |api_key, options|
    client.fields.create(@product_model[:id], {
      api_key: api_key,
      appeareance: options[:appeareance],
      default_value: nil,
      field_type: options[:field_type],
      hint: options[:hint],
      label: options[:label],
      localized: false,
      position: options[:position],
      validators: options[:validators]
    })
  end
end
create_records!() click to toggle source
# File lib/commercelayer/cli/bootstrappers/datocms.rb, line 181
def create_records!
  last_product_reference = nil
  last_product_id = nil
  last_product_variants = []
  Commercelayer::Sku.order(:reference).all.each_total do |sku|
    puts "> #{sku.code}"
    if sku.reference != last_product_reference
      if last_product_id
        client.items.update(last_product_id, {
          reference: last_product_reference,
          variants: last_product_variants
        })
        last_product_variants = []
      end

      begin
        product = client.items.create({
          item_type: @product_model[:id],
          reference: sku.reference,
          variants: []
        })
        last_product_reference = sku.reference
        last_product_id = product[:id]
      rescue => e
        puts e.inspect
        break
      end
    end

    begin
      variant = client.items.create({
        item_type: @variant_model[:id],
        code: sku.code,
        name: sku.name,
        description: sku.description,
        image: client.upload_image(sku.image_url.split("?").first)
      })
      last_product_variants << variant[:id]
    rescue => e
      puts e.inspect
      break
    end
  end
end
create_variant_model!() click to toggle source
# File lib/commercelayer/cli/bootstrappers/datocms.rb, line 44
def create_variant_model!
  puts "Creating variant model..."
  @variant_model = client.item_types.create({
    api_key: "variant",
    name: "Variant",
    singleton: false,
    all_locales_required: false,
    sortable: true,
    modular_block: false,
    draft_mode_active: false,
    tree: false,
    ordering_direction: nil,
    ordering_field: nil
  })
end
create_variant_model_fields!() click to toggle source
# File lib/commercelayer/cli/bootstrappers/datocms.rb, line 77
def create_variant_model_fields!
  puts "Creating variant model fields..."
  variant_model_fields.each do |api_key, options|
    client.fields.create(@variant_model[:id], {
      api_key: api_key,
      appeareance: options[:appeareance],
      default_value: nil,
      field_type: options[:field_type],
      hint: options[:hint],
      label: options[:label],
      localized: false,
      position: options[:position],
      validators: options[:validators]
    })
  end
end
product_model_fields() click to toggle source
# File lib/commercelayer/cli/bootstrappers/datocms.rb, line 94
def product_model_fields
  {
    reference: {
      label: "Reference",
      field_type: "string",
      hint: "The product's reference",
      position: 1,
      validators: {
        required: {},
        unique: {}
      },
      appeareance: {
        editor: "single_line",
        parameters: {}
      }
    },
    variants: {
      label: "Variants",
      field_type: "links",
      hint: "The product's variants",
      position: 2,
      validators: { items_item_type: { item_types: [@variant_model[:id]] } },
      appeareance: {
        editor: "links_select",
        parameters: {}
      }
    }
  }
end
variant_model_fields() click to toggle source
# File lib/commercelayer/cli/bootstrappers/datocms.rb, line 124
def variant_model_fields
  {
    code: {
      label: "Code",
      field_type: "string",
      hint: "The variant's code",
      position: 1,
      validators: {
        required: {},
        unique: {}
      },
      appeareance: {
        editor: "single_line",
        parameters: {}
      }
    },
    name: {
      label: "Name",
      field_type: "string",
      hint: "The variant's name",
      position: 2,
      validators: {
        required: {},
        unique: {}
      },
      appeareance: {
        editor: "single_line",
        parameters: {}
      }
    },
    description: {
      label: "Description",
      field_type: "text",
      hint: "The variant's description",
      position: 3,
      validators: {},
      appeareance: {
        editor: "markdown",
        parameters: {
          "toolbar" => ["heading", "bold", "italic", "strikethrough", "unordered_list", "ordered_list", "quote", "link", "image", "fullscreen"]
        }
      }
    },
    image: {
      label: "Image",
      field_type: "file",
      hint: "The variant's image",
      position: 4,
      validators: {},
      appeareance: {
        editor: "file",
        parameters: {}
      }
    }
  }
end