Sha256: d53183da5e258f9b10299be52a3c68008da96a01e4c2ed3d042b56b2f8852825

Contents?: true

Size: 1.12 KB

Versions: 33

Compression:

Stored size: 1.12 KB

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

        def request_url(opts)
          request = Grape::Request.new(opts[:env])
          "#{request.base_url}#{opts[:env]['PATH_INFO']}"
        end

        # 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

33 entries across 33 versions & 1 rubygems

Version Path
gris-0.4.3 lib/gris/output_formatters/paginated_presenter.rb
gris-0.4.2 lib/gris/output_formatters/paginated_presenter.rb
gris-0.4.0 lib/gris/output_formatters/paginated_presenter.rb
gris-0.3.9 lib/gris/output_formatters/paginated_presenter.rb
gris-0.3.8 lib/gris/output_formatters/paginated_presenter.rb
gris-0.3.7 lib/gris/output_formatters/paginated_presenter.rb
gris-0.3.6 lib/gris/output_formatters/paginated_presenter.rb
gris-0.3.4 lib/gris/output_formatters/paginated_presenter.rb
gris-0.3.3 lib/gris/output_formatters/paginated_presenter.rb
gris-0.3.2 lib/gris/output_formatters/paginated_presenter.rb
gris-0.3.1 lib/gris/output_formatters/paginated_presenter.rb
gris-0.3.0 lib/gris/output_formatters/paginated_presenter.rb
gris-0.2.4 lib/gris/output_formatters/paginated_presenter.rb
gris-0.2.3 lib/gris/output_formatters/paginated_presenter.rb
gris-0.2.2 lib/gris/output_formatters/paginated_presenter.rb
gris-0.2.1 lib/gris/output_formatters/paginated_presenter.rb
gris-0.2.0 lib/gris/output_formatters/paginated_presenter.rb
gris-0.1.7 lib/gris/output_formatters/paginated_presenter.rb
gris-0.1.6 lib/gris/output_formatters/paginated_presenter.rb
gris-0.1.5 lib/gris/output_formatters/paginated_presenter.rb