Class: Bhf::Platform::Pagination

Inherits:
Object
  • Object
show all
Defined in:
lib/bhf/platform/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.



8
9
10
11
# File 'lib/bhf/platform/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/platform/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/platform/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/platform/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
# File 'lib/bhf/platform/pagination.rb', line 13

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

  links = if !(page_links = template.paginate(platform.objects,
    theme: 'bhf',
    param_name: "#{platform.name}[page]",
  )).blank?
    "#{load_more(platform)} #{page_links}"
  elsif platform.objects.total_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



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/bhf/platform/pagination.rb', line 30

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.limit_value
    })
  else
    count = collection.size
    platform_title = case count
    when 0
      platform.title
    when 1
      platform.title_singular
    else
      platform.title
    end

    I18n.t('bhf.pagination.info',
      count: count,
      name: platform_title
    )
  end.html_safe
end

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



86
87
88
# File 'lib/bhf/platform/pagination.rb', line 86

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

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



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

def load_more(platform, attributes = {}, plus = true)
  platform_params = (template.params[platform.name] || ActionController::Parameters.new).dup
  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
  platform_params.permit!

  direction = plus ? 'more' : 'less'

  parsed_paramas = {
    bhf_area: template.params[:bhf_area],
    page: template.params[:page],
    format: nil
  }.merge(platform.name.to_sym => platform_params)
  template.link_to(
    I18n.t("bhf.pagination.load_#{direction}"),
    template.page_path(platform.page_name, parsed_paramas),
    attributes.merge(class: "load_#{direction}")
  )
end