Class: Dimples::Pager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dimples/pager.rb

Overview

A class for paginating a collection of posts.

Constant Summary collapse

DEFAULT_OPTIONS =
{ per_page: 5, page_prefix: 'page_' }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, posts:, options: {}) ⇒ Pager

Returns a new instance of Pager.



16
17
18
19
20
21
22
23
24
25
# File 'lib/dimples/pager.rb', line 16

def initialize(url:, posts:, options: {})
  @url = url
  @url << '/' unless @url[-1] == '/'

  @posts = posts
  @options = DEFAULT_OPTIONS.merge(options)
  @page_count = (posts.length.to_f / @options[:per_page].to_i).ceil

  step_to(1)
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



10
11
12
# File 'lib/dimples/pager.rb', line 10

def current_page
  @current_page
end

#next_pageObject (readonly)

Returns the value of attribute next_page.



10
11
12
# File 'lib/dimples/pager.rb', line 10

def next_page
  @next_page
end

#page_countObject (readonly)

Returns the value of attribute page_count.



10
11
12
# File 'lib/dimples/pager.rb', line 10

def page_count
  @page_count
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



10
11
12
# File 'lib/dimples/pager.rb', line 10

def previous_page
  @previous_page
end

Class Method Details

.paginate(url:, posts:, options: {}, context: {}, &block) ⇒ Object



12
13
14
# File 'lib/dimples/pager.rb', line 12

def self.paginate(url:, posts:, options: {}, context: {}, &block)
  new(url: url, posts: posts, options: options).paginate(context: context, &block)
end

Instance Method Details

#current_page_pathObject



55
56
57
# File 'lib/dimples/pager.rb', line 55

def current_page_path
  @current_page == 1 ? @url : File.join(@url, "page_#{@current_page}")
end

#current_page_urlObject



59
60
61
# File 'lib/dimples/pager.rb', line 59

def current_page_url
  @current_page == 1 ? @url : "#{@url}#{@options[:page_prefix]}#{@current_page}"
end

#first_page_urlObject



63
64
65
# File 'lib/dimples/pager.rb', line 63

def first_page_url
  @url
end

#last_page_urlObject



67
68
69
# File 'lib/dimples/pager.rb', line 67

def last_page_url
  @page_count == 1 ? @url : "#{@url}#{@options[:page_prefix]}#{@page_count}"
end

#metadataObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dimples/pager.rb', line 91

def 
  {
    posts: posts_at(current_page),
    pagination: {
      current_page: @current_page,
      page_count: @page_count,
      post_count: @posts.count,
      previous_page: @previous_page,
      next_page: @next_page,
      urls: urls
    }
  }
end

#next_page?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dimples/pager.rb', line 51

def next_page?
  @current_page + 1 <= @page_count
end

#next_page_urlObject



77
78
79
# File 'lib/dimples/pager.rb', line 77

def next_page_url
  "#{@url}#{@options[:page_prefix]}#{@next_page}" if @next_page
end

#paginate(context: {}) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/dimples/pager.rb', line 27

def paginate(context: {})
  (1..@page_count).each do |index|
    step_to(index)

    yield(current_page_path, context.merge()) if block_given?
  end
end

#posts_at(page) ⇒ Object



43
44
45
# File 'lib/dimples/pager.rb', line 43

def posts_at(page)
  @posts.slice((page - 1) * @options[:per_page], @options[:per_page])
end

#previous_page?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/dimples/pager.rb', line 47

def previous_page?
  (@current_page - 1).positive?
end

#previous_page_urlObject



71
72
73
74
75
# File 'lib/dimples/pager.rb', line 71

def previous_page_url
  return unless @previous_page

  @previous_page == 1 ? @url : "#{@url}#{@options[:page_prefix]}#{@previous_page}"
end

#step_to(page) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/dimples/pager.rb', line 35

def step_to(page)
  @current_page = page
  @previous_page = previous_page? ? @current_page - 1 : nil
  @next_page = next_page? ? @current_page + 1 : nil

  @current_page
end

#urlsObject



81
82
83
84
85
86
87
88
89
# File 'lib/dimples/pager.rb', line 81

def urls
  {
    current_page: current_page_url,
    first_page: first_page_url,
    last_page: last_page_url,
    previous_page: previous_page_url,
    next_page: next_page_url
  }
end