Class: Liquor::Pagination::PageExternal

Inherits:
Object
  • Object
show all
Includes:
External
Defined in:
lib/liquor/extensions/pagination.rb

Instance Method Summary collapse

Methods included from External

included, #liquor_send

Constructor Details

#initialize(collection, options = {}) ⇒ PageExternal


56
57
58
59
60
# File 'lib/liquor/extensions/pagination.rb', line 56

def initialize(collection, options={})
  @collection = collection
  @options    = options
  @index      = @collection.current_page
end

Instance Method Details

#each_relevant_pageObject


104
105
106
107
108
# File 'lib/liquor/extensions/pagination.rb', line 104

def each_relevant_page
  relevant_pages.each do |page|
    with(page); yield
  end
end

#in_inner_windowObject


84
85
86
# File 'lib/liquor/extensions/pagination.rb', line 84

def in_inner_window
  (@collection.current_page - @index).abs <= @options[:inner_window]
end

#in_outer_windowObject


88
89
90
91
# File 'lib/liquor/extensions/pagination.rb', line 88

def in_outer_window
  @index <= @options[:outer_window] ||
      @collection.total_pages - @index < @options[:outer_window]
end

#is_currentObject


62
63
64
# File 'lib/liquor/extensions/pagination.rb', line 62

def is_current
  @index == @collection.current_page
end

#is_firstObject


66
67
68
# File 'lib/liquor/extensions/pagination.rb', line 66

def is_first
  @index == 1
end

#is_gapObject


93
94
95
# File 'lib/liquor/extensions/pagination.rb', line 93

def is_gap
  !in_outer_window && !in_inner_window
end

#is_lastObject


70
71
72
# File 'lib/liquor/extensions/pagination.rb', line 70

def is_last
  @index == @collection.total_pages - 1
end

#is_nextObject


78
79
80
# File 'lib/liquor/extensions/pagination.rb', line 78

def is_next
  @index == @collection.current_page + 1
end

#is_prevObject


74
75
76
# File 'lib/liquor/extensions/pagination.rb', line 74

def is_prev
  @index == @collection.current_page - 1
end

#numberObject


130
131
132
# File 'lib/liquor/extensions/pagination.rb', line 130

def number
  @index
end

#pathObject


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/liquor/extensions/pagination.rb', line 118

def path
  @url_generator ||= UrlGenerator.bootstrap.new

  if @collection.respond_to? :to_page_path
    @collection.to_page_path(@url_generator, @index)
  elsif @collection.is_a? ActiveRecord::Relation
    @url_generator.polymorphic_path(@collection.klass, page: @index)
  else
    raise "Don't know how to generate page path for #{@collection.class}"
  end
end

#relevant_pagesObject


110
111
112
113
114
115
116
# File 'lib/liquor/extensions/pagination.rb', line 110

def relevant_pages
  left_window_plus_one = 1.upto(@options[:outer_window] + 1).to_a
  right_window_plus_one = (@collection.total_pages - @options[:outer_window]).upto(@collection.total_pages).to_a
  inside_window_plus_each_sides = (@collection.current_page - @options[:inner_window] - 1).upto(@collection.current_page + @options[:inner_window] + 1).to_a

  (left_window_plus_one + inside_window_plus_each_sides + right_window_plus_one).uniq.sort.reject {|x| (x < 1) || (x > @collection.total_pages)}
end

#with(index) ⇒ Object


99
100
101
102
# File 'lib/liquor/extensions/pagination.rb', line 99

def with(index)
  @index = index
  self
end