Sha256: 96a173afe82c619ce7210ce7e50e3b32716d767ae1774d39ca132b0a8a1c4d97

Contents?: true

Size: 1.23 KB

Versions: 8

Compression:

Stored size: 1.23 KB

Contents

module Spina
  module Api
    module Paginable
      extend ActiveSupport::Concern

      private

      def pagination(records)
        paginated_records = records.page(params[:page]).per(params[:per_page])
        [paginated_records, {
          meta: pagination_meta(paginated_records),
          links: pagination_links(paginated_records)
        }]
      end

      def pagination_meta(paginated_records)
        {
          current_page: paginated_records.current_page,
          total: paginated_records.total_count,
          per_page: paginated_records.limit_value,
          path: view_context.url_for(only_path: true)
        }
      end

      def pagination_links(paginated_records)
        {
          first: path_to_first_page,
          prev: view_context.path_to_prev_page(paginated_records),
          next: view_context.path_to_next_page(paginated_records),
          last: path_to_last_page(paginated_records)
        }
      end

      def path_to_first_page
        view_context.url_for(page: 1, per_page: params[:per_page], only_path: true)
      end

      def path_to_last_page(paginated_records)
        view_context.url_for(page: paginated_records.total_pages, per_page: params[:per_page], only_path: true)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
spina-2.18.0 app/controllers/concerns/spina/api/paginable.rb
spina-2.17.0 app/controllers/concerns/spina/api/paginable.rb
spina-2.16.0 app/controllers/concerns/spina/api/paginable.rb
spina-2.15.1 app/controllers/concerns/spina/api/paginable.rb
spina-2.15.0 app/controllers/concerns/spina/api/paginable.rb
spina-2.14.0 app/controllers/concerns/spina/api/paginable.rb
spina-2.13.1 app/controllers/concerns/spina/api/paginable.rb
spina-2.13.0 app/controllers/concerns/spina/api/paginable.rb