Class: FlexiAdmin::Components::Resources::PaginationComponent
- Inherits:
-
BaseComponent
- Object
- ViewComponent::Base
- BaseComponent
- FlexiAdmin::Components::Resources::PaginationComponent
- Includes:
- Helpers::ResourceHelper, Helpers::UrlHelper
- Defined in:
- lib/flexi_admin/components/resources/pagination_component.rb
Constant Summary collapse
- LIMIT =
10
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#limited_pagination ⇒ Object
readonly
Returns the value of attribute limited_pagination.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
Instance Method Summary collapse
-
#initialize(context, per_page:, page:) ⇒ PaginationComponent
constructor
A new instance of PaginationComponent.
- #limited_pages ⇒ Object
- #page_path(page_number) ⇒ Object
- #paginated_resources ⇒ Object
- #render? ⇒ Boolean
- #total_pages ⇒ Object
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
#context ⇒ Object (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_pagination ⇒ Object (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 |
#page ⇒ Object (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_page ⇒ Object (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_pages ⇒ Object
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_resources ⇒ Object
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
20 21 22 |
# File 'lib/flexi_admin/components/resources/pagination_component.rb', line 20 def render? paginated_resources.total_pages > 1 end |
#total_pages ⇒ Object
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 |