Class: FlexiAdmin::Components::Resources::PaginationComponent

Inherits:
BaseComponent
  • Object
show all
Includes:
Helpers::ResourceHelper, Helpers::UrlHelper
Defined in:
lib/flexi_admin/components/resources/pagination_component.rb

Constant Summary collapse

LIMIT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ResourceHelper

#autocomplete_path, #bulk_action_path, #datalist_path, #edit_resource_path, #paginate, #resource__path, #resource_input_name, #resource_path, #resources_path, #scope, #scope_plural, #scope_singular

Constructor Details

#initialize(context, per_page:, page:) ⇒ PaginationComponent

Returns a new instance of PaginationComponent.



12
13
14
15
16
17
18
# File 'lib/flexi_admin/components/resources/pagination_component.rb', line 12

def initialize(context, per_page:, page:)
  @context = context
  @resources = context.resources
  @parent = context.parent
  @per_page = per_page
  @page = page&.to_i
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



10
11
12
# File 'lib/flexi_admin/components/resources/pagination_component.rb', line 10

def context
  @context
end

#limited_paginationObject (readonly)

Returns the value of attribute limited_pagination.



10
11
12
# File 'lib/flexi_admin/components/resources/pagination_component.rb', line 10

def limited_pagination
  @limited_pagination
end

#pageObject (readonly)

Returns the value of attribute page.



10
11
12
# File 'lib/flexi_admin/components/resources/pagination_component.rb', line 10

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



10
11
12
# File 'lib/flexi_admin/components/resources/pagination_component.rb', line 10

def per_page
  @per_page
end

Instance Method Details

#limited_pagesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flexi_admin/components/resources/pagination_component.rb', line 24

def limited_pages
  return (1..paginated_resources.total_pages).to_a if paginated_resources.total_pages < LIMIT

  max = paginated_resources.total_pages

  mid_range_start = if page > (LIMIT / 2) && page < (max - (LIMIT / 2))
                      page - (LIMIT / 2)
                    elsif page < (LIMIT / 2)
                      1
                    else
                      max - LIMIT
                    end

  mid_range_end = if page > (LIMIT / 2) && page < (max - (LIMIT / 2))
                    page + (LIMIT / 2)
                  elsif page < (LIMIT / 2)
                    LIMIT
                  else
                    max
                  end

  [1, (mid_range_start..mid_range_end).to_a, max].flatten.compact.uniq
end

#page_path(page_number) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/flexi_admin/components/resources/pagination_component.rb', line 48

def page_path(page_number)
  params = context.params
                  .merge(page: page_number, per_page:, frame: context.scope)
                  .to_params

  resources_path(**params.merge)
end

#paginated_resourcesObject



56
57
58
# File 'lib/flexi_admin/components/resources/pagination_component.rb', line 56

def paginated_resources
  @resources.paginate(page:, per_page:)
end

#render?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/flexi_admin/components/resources/pagination_component.rb', line 20

def render?
  paginated_resources.total_pages > 1
end

#total_pagesObject



60
61
62
# File 'lib/flexi_admin/components/resources/pagination_component.rb', line 60

def total_pages
  (resources.count / per_page.to_f).ceil
end