Module: LatoBlog::Interface::Posts

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#blog__clean_post_parentsObject

This function cleans all old post parents without any child.



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

def blog__clean_post_parents
  post_parents = LatoBlog::PostParent.all
  post_parents.map { |pp| pp.destroy if pp.posts.empty? }
end

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

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



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lato_blog/interfaces/posts.rb', line 68

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

  if id
    post = LatoBlog::Post.find_by(id: id.to_i, meta_status: 'published')
  else
    post = LatoBlog::Post.find_by(meta_permalink: permalink, meta_status: 'published')
  end

  post.serialize
end

#blog__get_posts(order: nil, language: nil, category_permalink: nil, category_permalink_AND: false, category_id: nil, category_id_AND: false, tag_permalink: nil, tag_permalink_AND: false, tag_id: nil, tag_id_AND: false, search: nil, page: nil, per_page: nil) ⇒ Object

This function returns an object with the list of posts 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lato_blog/interfaces/posts.rb', line 13

def blog__get_posts(
  order: nil,
  language: nil,
  category_permalink: nil,
  category_permalink_AND: false,
  category_id: nil,
  category_id_AND: false,
  tag_permalink: nil,
  tag_permalink_AND: false,
  tag_id: nil,
  tag_id_AND: false,
  search: nil,
  page: nil,
  per_page: nil
)
  posts = LatoBlog::Post.published.joins(:post_parent).where('lato_blog_post_parents.publication_datetime <= ?', DateTime.now)

  # apply filters
  order = order && order == 'ASC' ? 'ASC' : 'DESC'
  posts = _posts_filter_by_order(posts, order)
  posts = _posts_filter_by_language(posts, language)
  if category_permalink || category_id
    posts = posts.joins(:categories)
    posts = _posts_filter_by_category_permalink(posts, category_permalink, category_permalink_AND)
    posts = _posts_filter_category_id(posts, category_id, category_id_AND)
  end
  if tag_permalink || tag_id
    posts = posts.joins(:tags)
    posts = _posts_filter_by_tag_permalink(posts, tag_permalink, tag_permalink_AND)
    posts = _posts_filter_tag_id(posts, tag_id, tag_id_AND)
  end
  posts = _posts_filter_search(posts, search)

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

  # save total posts
  total = posts.length

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

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