Class: Bhf::Pagination

Inherits:
Object
  • Object
show all
Defined in:
lib/bhf/pagination.rb

Defined Under Namespace

Classes: LinkRenderer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(offset_per_page = 10, offset_to_add = 5) ⇒ Pagination

Returns a new instance of Pagination.



8
9
10
11
# File 'lib/bhf/pagination.rb', line 8

def initialize(offset_per_page = 10, offset_to_add = 5)
  @offset_per_page = offset_per_page
  @offset_to_add = offset_to_add
end

Instance Attribute Details

#offset_per_pageObject (readonly)

Returns the value of attribute offset_per_page.



6
7
8
# File 'lib/bhf/pagination.rb', line 6

def offset_per_page
  @offset_per_page
end

#offset_to_addObject (readonly)

Returns the value of attribute offset_to_add.



6
7
8
# File 'lib/bhf/pagination.rb', line 6

def offset_to_add
  @offset_to_add
end

#templateObject

Returns the value of attribute template.



5
6
7
# File 'lib/bhf/pagination.rb', line 5

def template
  @template
end

Instance Method Details

#info(platform, options = {}) ⇒ Object



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
57
# File 'lib/bhf/pagination.rb', line 32

def info(platform, options = {})
  collection = platform.paginated_objects
  
  entry_name = options[:entry_name] ||
    (collection.empty?? 'entry' : collection.first.class.model_name.human)
  
  info = if collection.total_pages < 2
    case collection.size
      when 0
        I18n.t 'bhf.pagination.info.nothing_found', :name => entry_name.pluralize
      when 1
        I18n.t 'bhf.pagination.info.one_found', :name => entry_name
      else
        I18n.t 'bhf.pagination.info.all_displayed', :total_count => collection.size, :name => entry_name.pluralize
    end
  else
    I18n.t('bhf.pagination.info.default', {
      :name => entry_name.pluralize,
      :total_count => collection.total_entries,
      :offset_start => collection.offset + 1,
      :offset_end => collection.offset + collection.length
    })
  end

  info.html_safe
end

#load_less(platform, attributes = {}) ⇒ Object



82
83
84
# File 'lib/bhf/pagination.rb', line 82

def load_less(platform, attributes = {})
  load_more(platform, attributes, false)
end

#load_more(platform, attributes = {}, plus = true) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bhf/pagination.rb', line 59

def load_more(platform, attributes = {}, plus = true)
  platform_params = template.params[platform.name] || {}
  load_offset = @offset_per_page
  load_offset = platform_params[:per_page].to_i if platform_params[:per_page]
  if plus
    load_offset += @offset_to_add
  else
    load_offset -= @offset_to_add
  end

  platform_params.delete(:page)
  platform_params[:per_page] = load_offset
  
  direction = (plus ? 'more' : 'less')
  template.link_to(
    I18n.t("bhf.pagination.load_#{direction}"),
    template.bhf_page_path(
      platform.page_name,
      template.params.merge(platform.name => platform_params)
    ), attributes.merge(:class => "load_#{direction}")
  )
end

#paginate(platform) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bhf/pagination.rb', line 13

def paginate(platform)
  platform_params = template.params[platform.name] || {}

  if page_links = template.will_paginate(platform.paginated_objects, {
    :previous_label => I18n.t('bhf.pagination.previous_label'),
    :next_label => I18n.t('bhf.pagination.next_label'),
    :renderer => LinkRenderer.new(self, platform),
    :container => false
  })
    links = "#{load_more(platform)} #{page_links}"
  elsif platform.paginated_objects.total_pages == 1 && platform.paginated_objects.size > @offset_to_add
    links = load_less(platform)
  end

  if links
    template.(:div, links.html_safe, {:class => 'pagination'})
  end
end