Sha256: 78ed29bd3a90bc5f48209a8b96018c3db08566c9fddfbe2c822efdff5ab0c5e7

Contents?: true

Size: 998 Bytes

Versions: 3

Compression:

Stored size: 998 Bytes

Contents

module Gris
  module PaginatedPresenter
    def self.included(base)
      base.class_eval do
        include Gris::Presenter

        property :total_count
        property :total_pages
        property :current_page
        property :next_page
        property :prev_page

        link :self do |opts|
          "#{request_url(opts)}?#{query_string_for_page(represented.current_page, opts)}"
        end

        link :next do |opts|
          "#{request_url(opts)}?#{query_string_for_page(represented.next_page, opts)}" if represented.next_page
        end

        link :prev do |opts|
          "#{request_url(opts)}?#{query_string_for_page(represented.prev_page, opts)}" if represented.prev_page
        end

        private

        # replace the page parameter in the query string
        def query_string_for_page(page, opts)
          qs = Rack::Utils.parse_nested_query(opts[:env]['QUERY_STRING'])
          qs['page'] = page
          qs.to_query
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gris-0.4.6 lib/gris/output_formatters/paginated_presenter.rb
gris-0.4.5 lib/gris/output_formatters/paginated_presenter.rb
gris-0.4.4 lib/gris/output_formatters/paginated_presenter.rb