Sha256: e0a2fbb03329a0c070f8dd4b98b7a085a84cc31349bea7c43a096749b4280979

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module JsonApiToolbox
  module Paginable
    def pagination_meta(collection_resource)
      {
        'current-page': collection_resource.current_page,
        'next-page': collection_resource.next_page,
        'prev-page': collection_resource.previous_page,
        'total-pages': collection_resource.total_pages,
        'total-count': collection_resource.total_entries
      }
    end

    def pagination(collection_resource)
      {
        first: 1,
        last: collection_resource.total_pages,
        prev: collection_resource.previous_page,
        next: collection_resource.next_page
      }.reduce({}, &method(:page_links))
    end

    def page_links(paginations, entry)
      link, page = entry
      if page
        paginations.merge(create_page_link(link, page))
      else
        paginations
      end
    end

    def create_page_link(link, page)
      link_path = collection_resource_path(page: page,
                                           per_page: params[:per_page])
      { link => link_path }
    end

    def collection_resource_path(options)
      path_method = "#{controller_name.pluralize.underscore}_path"
      public_send(path_method, options)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
json_api_toolbox-1.3.0 lib/json_api_toolbox/paginable.rb
json_api_toolbox-1.2.0 lib/json_api_toolbox/paginable.rb
json_api_toolbox-1.1.0 lib/json_api_toolbox/paginable.rb
json_api_toolbox-1.0.0 lib/json_api_toolbox/paginable.rb
json_api_toolbox-0.17.0 lib/json_api_toolbox/paginable.rb