module PaginationHelper

  def pagination(collection=nil, **options, &block)
    # call the collection-method if no collection is given
    collection ||= send :collection

    pages = paginate(collection, **options)

    html = ''
    html << pages
    html << capture(&block)
    html << pages
    html.html_safe
  end

end

module PaginationHelper

  def pagination(collection=nil, **options, &block)
    # call the collection-method if no collection is given
    collection ||= send :collection

    pages = paginate(collection, **options)

    html = ''
    html << pages
    html << capture(&block)
    html << pages
    html.html_safe
  end

  # Kaminari generates a URL from the params hash. The URL is, however, not
  # compatible to loc/ids.
  # This method builds the loc/id compatible link to the page.
  def build_link_from_request(kaminari_url)
    page, per_page = params_from_kaminari_url(kaminari_url)

    query_string_parts = request.env['QUERY_STRING'].
      split(/;|&/).
      map { |p| p.split('=') }

    query_string_parts.reject! { |p| p.first == 'page' } if page
    query_string_parts.reject! { |p| p.first == 'per_page' } if per_page

    query_string_parts << ['page', page] if page
    query_string_parts << ['per_page', per_page] if per_page && per_page != :default

    query_string_parts.map! { |p| p.join('=') }
    query_string = query_string_parts.join('&')

    [request.env['REQUEST_PATH'], query_string].compact.join('?')
  end

  # Kaminari generates a URL from the params hash. The URL is, however, not
  # compatible to loc/ids.
  # This method extracts the page number and page size from the generated url.
  def params_from_kaminari_url(kaminari_url)
    page =
      if match = kaminari_url.match(/[\?&]page=(\d+)/)
        match[1]
      else
        1
      end
    per_page =
      if match = kaminari_url.match(/[\?&]per_page=(\d+)/)
        match[1]
      else
        nil
      end
    [page, per_page]
  end
end

Add helper method to build the page-URL from the request-URL.
Add helper method to build the page-URL from the request-URL.
