ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehasemodule PaginationHelper
7ea3e78d7552d1ec2d21efac4e14ace1a20f3975Eugen Kuksa def pagination(collection = nil, **options, &block)
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase # call the collection-method if no collection is given
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase collection ||= send :collection
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase
d30259e99ac8e7afd42077b99d3cd971c50c791cEugen Kuksa pages = paginate(collection, **options)
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase html = ''
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase html << pages
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase html << capture(&block)
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase html << pages
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase html.html_safe
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase end
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa # Kaminari generates a URL from the params hash. The URL is, however, not
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa # compatible to loc/ids.
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa # This method builds the loc/id compatible link to the page.
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa def build_link_from_request(kaminari_url)
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa page, per_page = params_from_kaminari_url(kaminari_url)
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa query_string_parts = request.env['QUERY_STRING'].
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa split(/;|&/).
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa map { |p| p.split('=') }
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa query_string_parts = replace_page_params(query_string_parts, page, per_page)
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa query_string = build_query_string(query_string_parts)
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa [request.env['REQUEST_PATH'], query_string].compact.join('?')
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa end
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa # Kaminari generates a URL from the params hash. The URL is, however, not
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa # compatible to loc/ids.
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa # This method extracts the page number and page size from the generated url.
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa def params_from_kaminari_url(kaminari_url)
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa page =
f464dee3d47c75a7a6ded792defb600bdb0d611dEugen Kuksa if match = kaminari_url.match(/[\?&]page=(\d+)/)
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa match[1]
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa end
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa per_page =
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa if match = kaminari_url.match(/[\?&]per_page=(\d+)/)
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa match[1]
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa end
2c941942e8229fe0cc6102f261303845a899f823Eugen Kuksa [page || 1, per_page]
86e6513d2355dd69a5993812e4651d7d7535aacbEugen Kuksa end
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa private
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa
f464dee3d47c75a7a6ded792defb600bdb0d611dEugen Kuksa def replace_page_params(query_string_parts, page, per_page)
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa query_string_parts.reject! { |p| p.first == 'page' } if page
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa query_string_parts.reject! { |p| p.first == 'per_page' } if per_page
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa query_string_parts << ['page', page] if page
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa if per_page && per_page != :default
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa query_string_parts << ['per_page', per_page]
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa end
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa query_string_parts
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa end
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa def build_query_string(query_string_parts)
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa query_string = query_string_parts.map { |p| p.join('=') }.join('&')
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa query_string if query_string.present?
5b5a719ebc902d7e2bacd575a4f80816c02594beEugen Kuksa end
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehaseend