Class: Scribo::PaginatorDrop

Inherits:
ApplicationDrop show all
Defined in:
app/drops/scribo/paginator_drop.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, content) ⇒ PaginatorDrop

Returns a new instance of PaginatorDrop.



8
9
10
11
12
# File 'app/drops/scribo/paginator_drop.rb', line 8

def initialize(site, content)
  @site = site
  @object = content
  @per_page = site&.properties&.[]('paginate') ? site.properties['paginate'].to_i : 5
end

Instance Attribute Details

#per_pageObject (readonly)

Returns the value of attribute per_page.



25
26
27
# File 'app/drops/scribo/paginator_drop.rb', line 25

def per_page
  @per_page
end

Instance Method Details

#next_pageObject



54
55
56
# File 'app/drops/scribo/paginator_drop.rb', line 54

def next_page
  page < total_pages ? page + 1 : nil
end

#next_page_pathObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/drops/scribo/paginator_drop.rb', line 58

def next_page_path
  current_path = @context.registers['controller'].request.original_fullpath
  next_path = nil
  if next_page
    if Scribo::Content.paginated?(current_path)
      next_path = current_path.gsub(%r[/(\d+)/$], "/#{next_page}/")
    else
      next_path = current_path
      next_path += '/' unless next_path.ends_with?('/')
      next_path += "#{next_page}/"
    end
  end
  next_path
end

#pageObject



18
19
20
21
22
23
# File 'app/drops/scribo/paginator_drop.rb', line 18

def page
  page = 1
  current_path = @context.registers['controller'].request.original_fullpath
  page = current_path.match(%r[/(\d+)/$])[1].to_i if Scribo::Content.paginated?(current_path)
  page
end

#postsObject



14
15
16
# File 'app/drops/scribo/paginator_drop.rb', line 14

def posts
  @site.contents.posts.order(created_at: :desc).page(page).per(@per_page).to_a
end

#previous_pageObject



35
36
37
# File 'app/drops/scribo/paginator_drop.rb', line 35

def previous_page
  page > 1 ? page - 1 : nil
end

#previous_page_pathObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/drops/scribo/paginator_drop.rb', line 39

def previous_page_path
  current_path = @context.registers['controller'].request.original_fullpath
  previous_path = nil
  if previous_page
    if Scribo::Content.paginated?(current_path)
      previous_path = current_path.gsub(%r[/(\d+)/$], "/#{previous_page}/")
    else
      previous_path = current_path
      previous_path += '/' unless previous_path.ends_with?('/')
      previous_path += "#{next_page}/"
    end
  end
  previous_path
end

#total_pagesObject



31
32
33
# File 'app/drops/scribo/paginator_drop.rb', line 31

def total_pages
  @site.contents.posts.page(page).per(@per_page).total_pages
end

#total_postsObject



27
28
29
# File 'app/drops/scribo/paginator_drop.rb', line 27

def total_posts
  @site.contents.posts.page(page).per(@per_page).total_count
end