Class: Dimples::Pager
- Inherits:
-
Object
- Object
- Dimples::Pager
- Includes:
- Enumerable
- Defined in:
- lib/dimples/pager.rb
Constant Summary collapse
- PER_PAGE =
5
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#next_page ⇒ Object
readonly
Returns the value of attribute next_page.
-
#page_count ⇒ Object
readonly
Returns the value of attribute page_count.
-
#previous_page ⇒ Object
readonly
Returns the value of attribute previous_page.
Instance Method Summary collapse
- #current_page_url ⇒ Object
- #each(&block) ⇒ Object
- #first_page_url ⇒ Object
-
#initialize(url, posts, options = {}) ⇒ Pager
constructor
A new instance of Pager.
- #last_page_url ⇒ Object
- #next_page? ⇒ Boolean
- #next_page_url ⇒ Object
- #posts_at(page) ⇒ Object
- #previous_page? ⇒ Boolean
- #previous_page_url ⇒ Object
- #step_to(page) ⇒ Object
- #to_context ⇒ Object
Constructor Details
#initialize(url, posts, options = {}) ⇒ Pager
Returns a new instance of Pager.
14 15 16 17 18 19 20 21 22 |
# File 'lib/dimples/pager.rb', line 14 def initialize(url, posts, = {}) @url = url @posts = posts @per_page = .fetch(:per_page, PER_PAGE) @page_prefix = .fetch(:page_prefix, 'page_') @page_count = (posts.length.to_f / @per_page.to_i).ceil step_to(1) end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
9 10 11 |
# File 'lib/dimples/pager.rb', line 9 def current_page @current_page end |
#next_page ⇒ Object (readonly)
Returns the value of attribute next_page.
11 12 13 |
# File 'lib/dimples/pager.rb', line 11 def next_page @next_page end |
#page_count ⇒ Object (readonly)
Returns the value of attribute page_count.
12 13 14 |
# File 'lib/dimples/pager.rb', line 12 def page_count @page_count end |
#previous_page ⇒ Object (readonly)
Returns the value of attribute previous_page.
10 11 12 |
# File 'lib/dimples/pager.rb', line 10 def previous_page @previous_page end |
Instance Method Details
#current_page_url ⇒ Object
48 49 50 |
# File 'lib/dimples/pager.rb', line 48 def current_page_url @current_page != 1 ? "#{@url}#{@page_prefix}#{@current_page}" : @url end |
#each(&block) ⇒ Object
24 25 26 |
# File 'lib/dimples/pager.rb', line 24 def each(&block) (1..@page_count).each { |index| block.yield step_to(index) } end |
#first_page_url ⇒ Object
52 53 54 |
# File 'lib/dimples/pager.rb', line 52 def first_page_url @url end |
#last_page_url ⇒ Object
56 57 58 |
# File 'lib/dimples/pager.rb', line 56 def last_page_url @page_count != 1 ? "#{@url}#{@page_prefix}#{@page_count}" : @url end |
#next_page? ⇒ Boolean
44 45 46 |
# File 'lib/dimples/pager.rb', line 44 def next_page? @current_page + 1 <= @page_count end |
#next_page_url ⇒ Object
66 67 68 |
# File 'lib/dimples/pager.rb', line 66 def next_page_url "#{@url}#{@page_prefix}#{@next_page}" if @next_page end |
#posts_at(page) ⇒ Object
36 37 38 |
# File 'lib/dimples/pager.rb', line 36 def posts_at(page) @posts.slice((page - 1) * @per_page, @per_page) end |
#previous_page? ⇒ Boolean
40 41 42 |
# File 'lib/dimples/pager.rb', line 40 def previous_page? (@current_page - 1).positive? end |
#previous_page_url ⇒ Object
60 61 62 63 64 |
# File 'lib/dimples/pager.rb', line 60 def previous_page_url return unless @previous_page @previous_page != 1 ? "#{@url}#{@page_prefix}#{@previous_page}" : @url end |
#step_to(page) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/dimples/pager.rb', line 28 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 |
#to_context ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/dimples/pager.rb', line 70 def to_context { posts: posts_at(current_page), current_page: @current_page, page_count: @page_count, post_count: @posts.count, previous_page: @previous_page, next_page: @next_page, 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 |