Module: LatoBlog::Interface::Categories

Included in:
LatoBlog::Interface
Defined in:
lib/lato_blog/interfaces/categories.rb

Overview

This module contains a list of functions used to manage categories for the blog.

Instance Method Summary collapse

Instance Method Details

#blog__clean_category_parentsObject

This function cleans all old category parents without any child.



28
29
30
31
# File 'lib/lato_blog/interfaces/categories.rb', line 28

def blog__clean_category_parents
  category_parents = LatoBlog::CategoryParent.all
  category_parents.map { |cp| cp.destroy if cp.categories.empty? }
end

#blog__create_default_categoryObject

This function create the default category if it not exists.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lato_blog/interfaces/categories.rb', line 7

def blog__create_default_category
  category_parent = LatoBlog::CategoryParent.find_by(meta_default: true)
  return if category_parent

  category_parent = LatoBlog::CategoryParent.new(meta_default: true)
  throw 'Impossible to create default category parent' unless category_parent.save

  languages = blog__get_languages_identifier
  languages.each do |language|
    category = LatoBlog::Category.new(
      title: 'Default',
      meta_permalink: "default_#{language}",
      meta_language: language,
      lato_core_superuser_creator_id: 1,
      lato_blog_category_parent_id: category_parent.id
    )
    throw 'Impossible to create default category' unless category.save
  end
end

#blog__get_categories(order: nil, language: nil, search: nil, page: nil, per_page: nil) ⇒ Object

This function returns an object with the list of categories with some filters.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lato_blog/interfaces/categories.rb', line 34

def blog__get_categories(
  order: nil,
  language: nil,
  search: nil,
  page: nil,
  per_page: nil
)
  categories = LatoBlog::Category.all

  # apply filters
  order = order && order == 'ASC' ? 'ASC' : 'DESC'
  categories = _categories_filter_by_order(categories, order)
  categories = _categories_filter_by_language(categories, language)
  categories = _categories_filter_search(categories, search)

  # take categories uniqueness
  categories = categories.uniq(&:id)

  # save total categories
  total = categories.length

  # manage pagination
  page = page&.to_i || 1
  per_page = per_page&.to_i || 20
  categories = core__paginate_array(categories, per_page, page)

  # return result
  {
    categories: categories && !categories.empty? ? categories.map(&:serialize) : [],
    page: page,
    per_page: per_page,
    order: order,
    total: total
  }
end

#blog__get_category(id: nil, permalink: nil) ⇒ Object

This function returns a single category searched by id or permalink.



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lato_blog/interfaces/categories.rb', line 71

def blog__get_category(id: nil, permalink: nil)
  return {} unless id || permalink

  if id
    category = LatoBlog::Category.find_by(id: id.to_i)
  else
    category = LatoBlog::Category.find_by(meta_permalink: permalink)
  end

  category.serialize
end