Class: Bhf::Pagination

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

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.



37
38
39
40
# File 'lib/bhf/pagination.rb', line 37

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.



35
36
37
# File 'lib/bhf/pagination.rb', line 35

def offset_per_page
  @offset_per_page
end

#offset_to_addObject (readonly)

Returns the value of attribute offset_to_add.



35
36
37
# File 'lib/bhf/pagination.rb', line 35

def offset_to_add
  @offset_to_add
end

#templateObject

Returns the value of attribute template.



34
35
36
# File 'lib/bhf/pagination.rb', line 34

def template
  @template
end

Instance Method Details

#create(platform) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bhf/pagination.rb', line 42

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

  links = if !(page_links = template.paginate(platform.objects, {
    theme: 'bhf',
    param_name: [platform.name, 'page'],
    params: template.params
  })).blank?
    "#{load_more(platform)} #{page_links}"
  elsif platform.objects.num_pages == 1 && platform.objects.size > @offset_to_add
    load_less(platform)
  end

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

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



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

def info(platform, options = {})
  collection = platform.objects

  if collection.respond_to?(:num_pages) and collection.num_pages > 1
    I18n.t('bhf.pagination.info.default', {
      name: platform.title,
      count: collection.total_count,
      offset_start: collection.offset_value + 1,
      offset_end: collection.offset_value + collection.length
    })
  else
    I18n.t('bhf.pagination.info', {
      name_zero: platform.title,
      name_singular: platform.title_singular,
      name_plural: platform.title,
      count: collection.size
    })
  end.html_safe
end

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



103
104
105
# File 'lib/bhf/pagination.rb', line 103

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

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bhf/pagination.rb', line 80

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