Class: Saint::Pager

Inherits:
Object
  • Object
show all
Includes:
Rack::Utils, Utils
Defined in:
lib/saint/pager.rb

Constant Summary

VAR =
"saint-page"
SIDE_PAGES =
5
ITEMS_PER_PAGE =
10

Constants included from Utils

Utils::BOOLEAN_FILTERS, Utils::BOOLEAN_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

column_format, escape_html, #format_date__time, normalize_string, number_to_human_size, saint_view, unescape_html

Constructor Details

#initialize(page = 1, items = ITEMS_PER_PAGE, items_per_page = ITEMS_PER_PAGE) ⇒ Pager

Returns a new instance of Pager



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/saint/pager.rb', line 13

def initialize page = 1, items = ITEMS_PER_PAGE, items_per_page = ITEMS_PER_PAGE

  @items, @items_per_page = items, items_per_page
  @pages = (@items.to_f / @items_per_page.to_f).ceil
  @pages = 1 if @pages < 1

  @page_label = page.to_i
  @page_label = 1 if @page_label < 1
  @page_label = @pages if @page_label > @pages && @pages > 1
  @page_number = @page_label - 1
end

Instance Attribute Details

#htmlObject (readonly)

Returns the value of attribute html



11
12
13
# File 'lib/saint/pager.rb', line 11

def html
  @html
end

#itemsObject (readonly)

Returns the value of attribute items



11
12
13
# File 'lib/saint/pager.rb', line 11

def items
  @items
end

#items_per_pageObject (readonly)

Returns the value of attribute items_per_page



11
12
13
# File 'lib/saint/pager.rb', line 11

def items_per_page
  @items_per_page
end

#page_labelObject (readonly)

Returns the value of attribute page_label



11
12
13
# File 'lib/saint/pager.rb', line 11

def page_label
  @page_label
end

#page_numberObject (readonly)

Returns the value of attribute page_number



11
12
13
# File 'lib/saint/pager.rb', line 11

def page_number
  @page_number
end

#pagesObject (readonly)

Returns the value of attribute pages



11
12
13
# File 'lib/saint/pager.rb', line 11

def pages
  @pages
end

Instance Method Details

#filters_stringObject

returns query string without page var



66
67
68
# File 'lib/saint/pager.rb', line 66

def filters_string
  '?' << @filters_string
end


70
71
72
73
# File 'lib/saint/pager.rb', line 70

def link_page page, route = nil
  route ||= @route
  route.to_s.sub('[__%s__]' % VAR, page.to_s)
end

#paginate(opts = {}) ⇒ Object



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
# File 'lib/saint/pager.rb', line 25

def paginate opts = {}

  @opts = opts

  @filters_string = @opts[:query] || build_nested_query(@opts[:filters]||{})
  @query_string = [
      "?",
      @filters_string,
      (@filters_string.size > 0 ? "&" : ""),
      "%s=[__%s__]" % [VAR, VAR],
  ].join

  @route = @opts[:route].to_s + @query_string

  side_pages = opts[:side_pages] || SIDE_PAGES

  @page_min = @page_label - side_pages
  @page_min = @pages - side_pages * 2 if (@page_label + side_pages) > @pages
  @page_max = @page_label + side_pages
  @page_max = side_pages * 2 if @page_label < side_pages

  @page_max = @pages if @page_max > @pages
  @page_min = 1 if @page_min < 1

  @page_prev = @page_label > 1 ? @page_label - 1 : nil
  @page_next = @page_label < @pages ? @page_label + 1 : nil

  return if opts[:skip_render]

  @html = saint_view.render_view(::File.join('pager', (@opts[:template] || :default).to_s))
  self
end

#query_string(*page_or_params) ⇒ Object

returns query string including page var



59
60
61
62
63
# File 'lib/saint/pager.rb', line 59

def query_string *page_or_params
  page, params = @page_label, {}
  page_or_params.each { |a| a.is_a?(Hash) ? params.update(a) : page = a }
  link_page(page, '%s%s%s' % [@query_string, ('&' if params.size > 0), build_nested_query(params)])
end