Class: Releaf::Builders::PaginationBuilder

Inherits:
Object
  • Object
show all
Includes:
Base, Template
Defined in:
app/builders/releaf/builders/pagination_builder.rb

Instance Attribute Summary collapse

Attributes included from Template

#template

Instance Method Summary collapse

Methods included from Base

#default_translation_scope, #html_escape, #icon, #layout_settings, #locale_options, #resource_title, #safe_join, #t, #tag, #template_variable, #translate_locale, #wrapper

Constructor Details

#initialize(template, options = {}) ⇒ PaginationBuilder

Returns a new instance of PaginationBuilder.



10
11
12
13
14
# File 'app/builders/releaf/builders/pagination_builder.rb', line 10

def initialize( template, options = {} )
  self.collection = options[:collection]
  self.params     = options[:params]
  super template
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



5
6
7
# File 'app/builders/releaf/builders/pagination_builder.rb', line 5

def collection
  @collection
end

#paramsObject

Returns the value of attribute params.



6
7
8
# File 'app/builders/releaf/builders/pagination_builder.rb', line 6

def params
  @params
end

Instance Method Details

#next_page_buttonObject



39
40
41
# File 'app/builders/releaf/builders/pagination_builder.rb', line 39

def next_page_button
  page_button( 1, 'next', 'chevron-right' )
end

#outputObject



16
17
18
19
# File 'app/builders/releaf/builders/pagination_builder.rb', line 16

def output
  return nil unless collection.total_pages > 1
  pagination_block
end

#page_button(offset, class_name, icon_name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/builders/releaf/builders/pagination_builder.rb', line 43

def page_button( offset, class_name, icon_name )
  attributes =
  {
    class: ['secondary', class_name ],
    title: t( "#{class_name.capitalize} page", scope: 'pagination')
  }

  page_number = relative_page_number( offset )

  if page_number.present?
    attributes[:rel]  = relative_page_relationship(offset)
    attributes[:href] = page_url(page_number)
  else
    attributes[:disabled] = true
  end

  button( nil, icon_name, attributes )
end

#page_label(page_number) ⇒ Object



101
102
103
104
105
# File 'app/builders/releaf/builders/pagination_builder.rb', line 101

def page_label page_number
  first_item_in_page = (page_number - 1) * per_page + 1
  last_item_in_page  = [page_number * per_page, total_entries].min
  "#{first_item_in_page}-#{last_item_in_page}"
end

#page_numbersObject



62
63
64
# File 'app/builders/releaf/builders/pagination_builder.rb', line 62

def page_numbers
  (1..total_pages).to_a
end

#page_url(page_number) ⇒ Object



81
82
83
# File 'app/builders/releaf/builders/pagination_builder.rb', line 81

def page_url(page_number)
  template.url_for( params.merge( page: page_number ))
end

#pagination_blockObject



21
22
23
24
25
# File 'app/builders/releaf/builders/pagination_builder.rb', line 21

def pagination_block
  ( :div, class: :pagination ) do
    safe_join { pagination_parts }
  end
end

#pagination_optionsObject



93
94
95
96
97
98
99
# File 'app/builders/releaf/builders/pagination_builder.rb', line 93

def pagination_options
  page_numbers.map do |page_number|
    attributes = { value: page_number }
    attributes[:selected] = true if page_number == current_page
    (:option, attributes) { page_label(page_number) }
  end
end

#pagination_partsObject



27
28
29
30
31
32
33
# File 'app/builders/releaf/builders/pagination_builder.rb', line 27

def pagination_parts
  [
    previous_page_button,
    pagination_select,
    next_page_button
  ]
end

#pagination_selectObject



85
86
87
88
89
90
91
# File 'app/builders/releaf/builders/pagination_builder.rb', line 85

def pagination_select
  ('select', name: :page) do
    safe_join do
      pagination_options
    end
  end
end

#previous_page_buttonObject



35
36
37
# File 'app/builders/releaf/builders/pagination_builder.rb', line 35

def previous_page_button
  page_button( -1, 'previous', 'chevron-left' )
end

#relative_page_number(offset) ⇒ Object



66
67
68
69
# File 'app/builders/releaf/builders/pagination_builder.rb', line 66

def relative_page_number( offset )
  page_number = current_page + offset
  page_numbers.include?(page_number) ? page_number : nil
end

#relative_page_relationship(offset) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'app/builders/releaf/builders/pagination_builder.rb', line 71

def relative_page_relationship(offset)
  if offset == -1
    :prev
  elsif offset == 1
    :next
  else
    nil
  end
end