# firefox (in dev so slow):
http://bookerbee.com/bookables?page=7&&sleeps=2&when=31&where=1r
## shared/_paginate.html.haml
%ul
%li.prev
=paginate_nav(:prev)
-@count[:pages].times do |i|
%li
=paginate_nav(i+1)
%li.next
=paginate_nav(:next)
## ApplicationHelper
def pagination
render :partial => 'shared/paginate'
end
def paginate_params(page)
current_page = params[:page].to_i
case page
when :next
position = current_page + 1
when :prev
position = current_page - 1
else
position = page
end
Hash[*request.query_parameters.map {|it| it.include?("page") ? it = ["page", position] : it}.flatten]
end
def paginate_nav(kind)
link = link_to(kind.to_s.titlecase, bookables_path(paginate_params(kind)))
current_page = params[:page].to_i
page = kind.to_s.to_i
if page > 0 && page != current_page
link
elsif kind == :prev && current_page > 1
link
elsif kind == :next && current_page < @count[:pages]
link
else
"<span>#{kind.to_s.titlecase}</span>"
end
end
end
## CONTROLLER
count = Foo.yrfind (returns an integer)
per_page = 150
pages = count/per_page.to_f
pages %1 == 0 ? page_count = pages : page_count = pages+1
@count = {:all => count, :pages => page_count.to_i, :per_page => per_page}
@bookables = Foo.yrfind(foo,bar,params[:page],@count[:per_page])
## MODEL
basically add
query << " ORDER BY id LIMIT 150 OFFSET #{(page-1)*per_page}" unless count
(using the same query for count and select):
## index view
#refine
=render :partial => "form"
#results
.header
%p.counts==We found <em>#{@count[:all]}</em> properties from #{pluralize(@brands.size,'hospitality brand')}
.pagination
=pagination
%ul
=render :partial => @bookables
.clear
=javascript_include_tag 'search'