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 || 10
  @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

#create(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 create(platform)
  platform_params = template.params[platform.name] || {}

  if page_links = template.will_paginate(platform.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.objects.total_pages == 1 && platform.objects.size + @offset_to_add > @offset_per_page
    links = load_less(platform)
  end

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

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

def info(platform, options = {})
  collection = platform.objects
  
  unless collection.respond_to?(:total_pages)
    collection = collection.paginate({:page => 1, :per_page => collection.count+1})
  end

  if collection.total_pages > 1
    I18n.t('bhf.pagination.info.default', {
      :name => platform.title,
      :count => collection.total_entries,
      :offset_start => collection.offset + 1,
      :offset_end => collection.offset + collection.length
    })
  else
    I18n.t('bhf.pagination.info', {
      :name_zero => platform.title_zero,
      :name_singular => platform.title_singular,
      :name_plural => platform.title,
      :count => collection.size
    })
  end.html_safe
end

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



79
80
81
# File 'lib/bhf/pagination.rb', line 79

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

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



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

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