Report abuse

1
2
# firefox (in dev so slow):
http://bookerbee.com/bookables?page=7&&sleeps=2&when=31&where=1r

shared/_paginate.html.haml

1
2
3
4
5
6
7
8
9
%ul
  %li.prev
    =paginate_nav(:prev)
  -@count[:pages].times do |i|
    %li
      =paginate_nav(i+1)
  %li.next
    =paginate_nav(:next)

ApplicationHelper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  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

1
2
3
4
5
6
7
    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

1
2
3
4
5
6
  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

1
2
3
4
5
6
7
8
9
10
11
12
#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'