On Mar 25, 4:33 pm, John Merlino <[email protected]> wrote:
> Here's the method:
>
> def paginate(resources)
>    @all_resources = resources
>   @limit = (params[:limit] && params[:limit].to_i) || 25
>   @page = (params[:page] && params[:page].to_i) || 1
>   @page_index = @page - 1
>   @offset = @page_index * @limit
>   @num_pages = if @limit == 0
>     1
>   elsif @all_resources.size == 0
>     1
>   else
>     ((@all_resources.size - 1) / @limit).to_i + 1
>   end
>   @last_page = @num_pages
>   @limit == 0 ? @all_resources : @all_resources.slice(@offset, @limit)
> end

BTW, if you found this in production code, I'd highly recommend
KILLING IT WITH FIRE and using something like will_paginate if at all
possible - this is the quite-ancient original pagination code included
with Rails (stripped out to the classic_pagination plugin in 1.2.4!)
and is immensely inefficient. The Rails version of your code really
doesn't matter; I just got will_paginate up and running on an old
1.2.3 app without any major issues.

--Matt Jones

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to