class Prestashop::Mapper::Category

Attributes

active[RW]
description[W]
id[RW]
id_lang[RW]
id_parent[RW]
id_shop_default[RW]
is_root_category[RW]
level_depth[RW]
name[W]
position[RW]

Public Class Methods

cache() click to toggle source

Requesting all on Prestashop API, displaying id, id_parent, name

# File lib/prestashop/mapper/models/category.rb, line 82
def cache
  all display: '[id, id_parent, name]'
end
create_from_name(category_name, id_lang) click to toggle source

Create new category based on given param, delimited by delimiter in settings

Example:

Category.create_from_name('Apple||iPhone', 2) # => [1, 2]
# File lib/prestashop/mapper/models/category.rb, line 91
def create_from_name category_name, id_lang
  if category_name and !category_name.empty?
    names = [category_name.split('||')].flatten!
    categories = []
    id_parent = 2
    names.each do |name|
      id_parent = new(name: name, id_parent: id_parent, id_lang: id_lang).find_or_create
      categories << id_parent
    end
    categories
  end
end
create_from_names(category_names, id_lang) click to toggle source
# File lib/prestashop/mapper/models/category.rb, line 104
def create_from_names category_names, id_lang
  categories = []
  category_names.each do |category_name|
    categories << create_from_name(category_name, id_lang)
  end
  categories.flatten.uniq
end
find_in_cache(id_parent, name, id_lang) click to toggle source

Search for category based on args on cached categories, see cache and Client::Settings.categories_cache Returns founded category or nil

# File lib/prestashop/mapper/models/category.rb, line 77
def find_in_cache id_parent, name, id_lang
  Client.categories_cache.find{ |c| c[:id_parent] == id_parent and c[:name].find_lang(name, id_lang) }
end
new(args = {}) click to toggle source
# File lib/prestashop/mapper/models/category.rb, line 12
def initialize args = {}
  @id               = args[:id]
  @id_parent        = args.fetch(:id_parent, 2)
  @level_depth      = args[:level_depth]
  # nb_products_recursive
  @active           = args.fetch(:active, 1)
  @id_shop_default  = args.fetch(:id_shop_default, 1)
  @is_root_category = 0
  @position         = args[:position]
  # date_add
  # date_upd
  @name             = args.fetch(:name)
  @link_rewrite     = args[:link_rewrite]
  @description      = args[:description]
  @meta_title       = args[:meta_title]
  @meta_description = args[:meta_description]
  @meta_keywords    = args[:meta_keywords]

  @id_lang          = args.fetch(:id_lang)
end

Public Instance Methods

description() click to toggle source

Description can have additional symbols and can’t be longer than 255

# File lib/prestashop/mapper/models/category.rb, line 39
def description
  @description.restricted.truncate(252) if @description
end
find_or_create() click to toggle source

Find category by name and id parent, create new one from hash, when doesn’t exist

# File lib/prestashop/mapper/models/category.rb, line 63
def find_or_create
  category = self.class.find_in_cache id_parent, name, id_lang
  unless category
    category = create
    Client.clear_categories_cache
  end
  category[:id]
end
hash() click to toggle source

Category hash structure, which will be converted to XML

# File lib/prestashop/mapper/models/category.rb, line 49
def hash
  { id_parent:        id_parent,
    active:           active ,
    id_shop_default:  id_shop_default,
    is_root_category: is_root_category,
    name:             hash_lang(name, id_lang),
    link_rewrite:     hash_lang(link_rewrite, id_lang),
    description:      hash_lang(description, id_lang),
    meta_title:       hash_lang(name, id_lang),
    meta_description: hash_lang(description, id_lang),
    meta_keywords:    hash_lang(meta_keywords, id_lang) }
end
name() click to toggle source

Category name can’t have some symbols and can’t be longer than 63

# File lib/prestashop/mapper/models/category.rb, line 34
def name
  @name.plain.truncate(61)
end