Module: LatoBlog::Interface::Tags

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#blog__clean_tag_parentsObject

This function cleans all old tag parents without any child.



7
8
9
10
# File 'lib/lato_blog/interfaces/tags.rb', line 7

def blog__clean_tag_parents
  tag_parents = LatoBlog::TagParent.all
  tag_parents.map { |tp| tp.destroy if tp.tags.empty? }
end

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

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



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/lato_blog/interfaces/tags.rb', line 50

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

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

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lato_blog/interfaces/tags.rb', line 13

def blog__get_tags(
  order: nil,
  language: nil,
  search: nil,
  page: nil,
  per_page: nil
)
  tags = LatoBlog::Tag.all

  # apply filters
  order = order && order == 'ASC' ? 'ASC' : 'DESC'
  tags = _tags_filter_by_order(tags, order)
  tags = _tags_filter_by_language(tags, language)
  tags = _tags_filter_search(tags, search)

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

  # save total tags
  total = tags.length

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

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