Class: Prestashop::Mapper::Category

Inherits:
Model
  • Object
show all
Defined in:
lib/prestashop/mapper/models/category.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#hash_lang, #meta_description, #meta_keywords, #meta_title, model, resource

Methods included from Extension

included

Constructor Details

#initialize(args = {}) ⇒ Category


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#activeObject

Returns the value of attribute active


9
10
11
# File 'lib/prestashop/mapper/models/category.rb', line 9

def active
  @active
end

#descriptionObject

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


39
40
41
# File 'lib/prestashop/mapper/models/category.rb', line 39

def description
  @description.restricted.truncate(252) if @description
end

#idObject

Returns the value of attribute id


9
10
11
# File 'lib/prestashop/mapper/models/category.rb', line 9

def id
  @id
end

#id_langObject

Returns the value of attribute id_lang


8
9
10
# File 'lib/prestashop/mapper/models/category.rb', line 8

def id_lang
  @id_lang
end

#id_parentObject

Returns the value of attribute id_parent


9
10
11
# File 'lib/prestashop/mapper/models/category.rb', line 9

def id_parent
  @id_parent
end

#id_shop_defaultObject

Returns the value of attribute id_shop_default


9
10
11
# File 'lib/prestashop/mapper/models/category.rb', line 9

def id_shop_default
  @id_shop_default
end

#is_root_categoryObject

Returns the value of attribute is_root_category


9
10
11
# File 'lib/prestashop/mapper/models/category.rb', line 9

def is_root_category
  @is_root_category
end

#level_depthObject

Returns the value of attribute level_depth


9
10
11
# File 'lib/prestashop/mapper/models/category.rb', line 9

def level_depth
  @level_depth
end

Link rewrite must be usable in uri


44
45
46
# File 'lib/prestashop/mapper/models/category.rb', line 44

def link_rewrite
  @link_rewrite ? @link_rewrite.parameterize : name.parameterize
end

#nameObject

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


34
35
36
# File 'lib/prestashop/mapper/models/category.rb', line 34

def name
  @name.plain.truncate(61)
end

#positionObject

Returns the value of attribute position


9
10
11
# File 'lib/prestashop/mapper/models/category.rb', line 9

def position
  @position
end

Class Method Details

.cacheObject

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


82
83
84
# 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) ⇒ Object

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

Example:

Category.create_from_name('Apple||iPhone', 2) # => [1, 2]

91
92
93
94
95
96
97
98
99
100
101
102
# 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) ⇒ Object


104
105
106
107
108
109
110
# 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) ⇒ Object

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


77
78
79
# 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

Instance Method Details

#find_or_createObject

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


63
64
65
66
67
68
69
70
# 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

#hashObject

Category hash structure, which will be converted to XML


49
50
51
52
53
54
55
56
57
58
59
60
# 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