Danimal wrote:
> Matt,
>
> No, you are making sense. I think you and I got mixed up backwards.
>
> If you are KEEPING sort order but CHANGING pages, then don't change
> the sort.
> If you are KEEPING pages but CHANGING sort order, then I would argue,
> force a page change to page 1.
>
> So, in your example, if you sort by hostname ASC then go to page 2, it
> should maintain the hostname sort. But if you are on page 2 and then
> change your sort to IP address, it should jump you to page 1 (although
> as I stated in my previous post, you could stay on page 2 in this
> instance).
>
> It's not a big deal programmatically because you take your collection
> of objects first (i.e. via find() ), then you sort, then you paginate.
>
> The only thing that could mess this up is if your dataset changes
> between pages. If that is a concern, then you'd want to think through
> the best way to handle that, which could be one of multiple options.
>
> So, to reiterate, here's how I handle it. Here is my index method for
> widgets:
>
> [code]
> def index
>   @widgets = paginate_and_sort Widgets.find(:all) # or whatever means
> to get an array of widget objects
> end
> [/code]
>
> Then, I have this defined on application.rb (cause I use it all over):
>
> [code]
>       def paginate_and_sort(collection)
>               per_page = (session[:per_page] || 5).to_i
>               page = (params[:page] ||= 1).to_i
>               page = 1 if (page - 1) * per_page > collection.size or page < 1
>               offset = (page - 1) * per_page
>               @sorted_on = params[:sort] unless params[:sort_direction] == 
> "reset"
>               @sorted_dir = params[:sort_direction]
>               if (["ASC", "DESC"].include?(@sorted_dir))
>                       collection = if @sorted_dir == "DESC"
>                               collection.sort {|a,b| b.send(params[:sort]) <=>
> a.send(params[:sort])}
>                       else
>                               collection.sort {|a,b| a.send(params[:sort]) <=>
> b.send(params[:sort])}
>                       end
>               else
>                 collection = collection.sort {|a,b| b.updated_at <=>
> a.updated_at }
>               end
>
>               WillPaginate::Collection.new(page, per_page,
> collection.length).concat(collection[offset..(offset + per_page - 1)])
>       end
> [/code]
>
> I then set the session[:per_page] elsewhere, i.e. when the user
> changes that in a different action, I set it as a session variable.
>
> So note that though not the cleanest code, it works and works well. I
> build the values needed for will_paginate then I sort the collection
> either by the sort & direction or by updated_at (that's my default,
> yours may differ). Then pass that into will_paginate which will return
> a pseudo-array of the paginated objects.
>
> HTH!
>
> -Danimal
Well I'm glad we're on the same page (excuse the pun). I've managed to 
fix the "changing page resets sort" problem by using the session as the 
store instead of params. That means I don't have to fiddle with 
paginating_find and get the params passed.

I'm onto the next problem now:

/machines/index => Shows a list of machine which are paginated and can 
be sorted.

/machines/view/1 => Views a machine by ID and lists records associated 
with the machine.

I want to paginate and sort the list of records but I'm not sure if this 
can be done with associated records. Ideally it would be in the records 
controller but for some reason I chose to put it in the machines/view 
action.

Any suggestions on paginating and sorting an associated collection? 
Incidentally, the records also have uploads associated with them so I 
will have to do it again with the uploads list.

Thanks for the help so far and I'd appreciate any further advice :)

Thanks

Matt

--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-talk@googlegroups.com
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