I have a few rather convoluted queries implemented within existing models. These are each used by a number of controllers. Now, in a new controller I would like to display a paged result set of the collection returned by one of the existing methods. I don''t see a way to do pagination on the results of class methods and I''d rather not pull the queries out into the controllers. I''d like to pass an existing collection object to "paginate" instead. Something like this in the controller... projects = Project.do_complex_query @project_pages, @projects = paginate(projects, :per_page => 10) Seems like I could simulate a Paginator object by slicing up the array (projects), but thought I''d check here first to make sure I''m not misunderstanding something or missing a better way to handle this. -- Jack Baty - Director of Unspecified Services Fusionary Media - http://www.fusionary.com Weblog - http://jackbaty.com
I have the same problem - anyone come up with a way to pass an existing collection object to "paginate?" It seems like there has to be - without it, it looks like a big contradiction of the the "DRY" principle. - Derek On 5/30/05, Jack Baty <jackbaty-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a few rather convoluted queries implemented within existing > models. These are each used by a number of controllers. Now, in a new > controller I would like to display a paged result set of the > collection returned by one of the existing methods. > > I don''t see a way to do pagination on the results of class methods and > I''d rather not pull the queries out into the controllers. I''d like to > pass an existing collection object to "paginate" instead. Something > like this in the controller... > > projects = Project.do_complex_query > @project_pages, @projects = paginate(projects, :per_page => 10) > > Seems like I could simulate a Paginator object by slicing up the array > (projects), but thought I''d check here first to make sure I''m not > misunderstanding something or missing a better way to handle this. > > -- > Jack Baty - Director of Unspecified Services > Fusionary Media - http://www.fusionary.com > Weblog - http://jackbaty.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Derek Haynes blog - http://itsderek23.blogspot.com Cell - 404.593.4879
On Jun 15, 2005, at 3:26 PM, Derek Haynes wrote:> I have the same problem - anyone come up with a way to pass an > existing collection object to "paginate?" >I haven''t found a nice solution to this problem either. I wrote the following as an implemenation of just what you seem to be talking about: def paginate_collection(collection, options = {}) default_options = {:per_page => 10, :page => 1} options = default_options.merge options pages = Paginator.new self, collection.size, options[:per_page], options[:page] first = pages.current.offset last = [first + options[:per_page], collection.size].min slice = collection[first...last] return [pages, slice] end I put that in my application controller. Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Oops! I went and violated my own suggestion. That should have been a [REUSE] tag. Sorry all :) Duane Johnson (canadaduane)
On Jun 15, 2005, at 7:32 PM, Duane Johnson wrote:> On Jun 15, 2005, at 3:26 PM, Derek Haynes wrote: > >> I have the same problem - anyone come up with a way to pass an >> existing collection object to "paginate?" >> > > I haven''t found a nice solution to this problem either. I wrote > the following as an implemenation of just what you seem to be > talking about: > > def paginate_collection(collection, options = {}) > default_options = {:per_page => 10, :page => 1} > options = default_options.merge options > > pages = Paginator.new self, collection.size, options > [:per_page], options[:page] > first = pages.current.offset > last = [first + options[:per_page], collection.size].min > slice = collection[first...last] > return [pages, slice] > end > > I put that in my application controller.Nice. I could be missing something, but shouldn''t the page key off the params? pages = Paginator.new self, collection.size, options[:per_page], params[:page] The code above would seem to stick on page 1. Mike http://clarkware.com
On Jun 15, 2005, at 9:16 PM, Mike Clark wrote:> > Nice. > > I could be missing something, but shouldn''t the page key off the > params? > > pages = Paginator.new self, collection.size, options[:per_page], > params[:page] > > The code above would seem to stick on page 1. >I''ve found that in many cases I actually want to store the page in the session. For example, when I have a set of "sort by" links, or when there are some search terms involved in determining what the result set looks like. The options hash takes the :page as a parameter, so you''d be free (in your calling action) to do something like this: my_complex_array_of_movies = Movie.find_all_custom_query pages, @movies = paginate_collection my_complex_array_of_movies, :page => @session[:page] or, in the more traditional way, pages, @movies = paginate_collection my_complex_array_of_movies, :page => @params[:page] Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Jun 15, 2005, at 9:28 PM, Duane Johnson wrote:> > On Jun 15, 2005, at 9:16 PM, Mike Clark wrote: > >> >> Nice. >> >> I could be missing something, but shouldn''t the page key off the >> params? >> >> pages = Paginator.new self, collection.size, options[:per_page], >> params[:page] >> >> The code above would seem to stick on page 1. >> > > I''ve found that in many cases I actually want to store the page in > the session. For example, when I have a set of "sort by" links, or > when there are some search terms involved in determining what the > result set looks like. The options hash takes the :page as a > parameter, so you''d be free (in your calling action) to do > something like this: > > my_complex_array_of_movies = Movie.find_all_custom_query > pages, @movies = paginate_collection > my_complex_array_of_movies, :page => @session[:page] > > or, in the more traditional way, > > pages, @movies = paginate_collection > my_complex_array_of_movies, :page => @params[:page]Ah, but of course. Nice! And the default options can use params[:page] otherwise. Mike
Rob
2005-Oct-02 02:25 UTC
Re: [TIP] Paginator and queries in the model (ATTN: Duane Johnson)
Sorry to bump an old topic. I was hoping to ask Duane if he might elaborate on how one might use session in this way. I''m having trouble figuring out how this might look in the controller. I too have a large number of ''sort by'' links (and want to add a search box) to a list view. Thanks if you have a little time, Rob On 15-Jun-05, at 11:28 PM, Duane Johnson wrote:> I''ve found that in many cases I actually want to store the page in the > session. For example, when I have a set of "sort by" links, or when > there are some search terms involved in determining what the result > set looks like. The options hash takes the :page as a parameter, so > you''d be free (in your calling action) to do something like this: > > my_complex_array_of_movies = Movie.find_all_custom_query > pages, @movies = paginate_collection my_complex_array_of_movies, :page > => @session[:page] > > or, in the more traditional way, > > pages, @movies = paginate_collection my_complex_array_of_movies, :page > => @params[:page] > > Duane Johnson > (canadaduane)
Duane Johnson
2005-Oct-02 03:48 UTC
Re: [TIP] Paginator and queries in the model (ATTN: Duane Johnson)
I use a before_filter to store any incoming "page" params in the session, something like this: before_filter :store_page_in_session def store_page_in_session session[controller_name] ||= Hash.new session[controller_name][:page] = params[:page] end Note: the above code is untested. Then I retrieve the page from the session instead of from the params hash. Duane On Oct 1, 2005, at 7:25 PM, Rob wrote:> Sorry to bump an old topic. I was hoping to ask Duane if he might > elaborate on how one might use session in this way. I''m having > trouble figuring out how this might look in the controller. I too > have a large number of ''sort by'' links (and want to add a search > box) to a list view. > > Thanks if you have a little time, > Rob > > On 15-Jun-05, at 11:28 PM, Duane Johnson wrote: > > >> I''ve found that in many cases I actually want to store the page in >> the session. For example, when I have a set of "sort by" links, >> or when there are some search terms involved in determining what >> the result set looks like. The options hash takes the :page as a >> parameter, so you''d be free (in your calling action) to do >> something like this: >> >> my_complex_array_of_movies = Movie.find_all_custom_query >> pages, @movies = paginate_collection >> my_complex_array_of_movies, :page => @session[:page] >> >> or, in the more traditional way, >> >> pages, @movies = paginate_collection >> my_complex_array_of_movies, :page => @params[:page] >> >> Duane Johnson >> (canadaduane) >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Robert Wheaton
2005-Oct-09 13:45 UTC
Re: [TIP] Paginator and queries in the model (ATTN: Duane Johnson)
Thanks, Duane: that was a tremendous help. I guess I''m having difficulty deciding how/when to destroy the session object, though. If a user returns to the list page some time later (but during the same session), I don''t necessarily want them to see the same page (and ''sort by'' criteria) that they were using last time. On the other hand, if they just hit the back button to return to the list page, it shouldn''t reset to the first page. Those issues seem to be fine when everything is handled in the querystring -- I just haven''t figured out how to manage them via the session. Have you found a good solution to this? Many thanks, Rob On 1-Oct-05, at 11:48 PM, Duane Johnson wrote:> I use a before_filter to store any incoming "page" params in the > session, something like this: > > before_filter :store_page_in_session > > def store_page_in_session > session[controller_name] ||= Hash.new > session[controller_name][:page] = params[:page] > end > > Note: the above code is untested. > > Then I retrieve the page from the session instead of from the params > hash. > > Duane > > On Oct 1, 2005, at 7:25 PM, Rob wrote: > >> Sorry to bump an old topic. I was hoping to ask Duane if he might >> elaborate on how one might use session in this way. I''m having >> trouble figuring out how this might look in the controller. I too >> have a large number of ''sort by'' links (and want to add a search box) >> to a list view. >> >> Thanks if you have a little time, >> Rob >> >> On 15-Jun-05, at 11:28 PM, Duane Johnson wrote: >> >> >>> I''ve found that in many cases I actually want to store the page in >>> the session. For example, when I have a set of "sort by" links, or >>> when there are some search terms involved in determining what the >>> result set looks like. The options hash takes the :page as a >>> parameter, so you''d be free (in your calling action) to do something >>> like this: >>> >>> my_complex_array_of_movies = Movie.find_all_custom_query >>> pages, @movies = paginate_collection my_complex_array_of_movies, >>> :page => @session[:page] >>> >>> or, in the more traditional way, >>> >>> pages, @movies = paginate_collection my_complex_array_of_movies, >>> :page => @params[:page] >>> >>> Duane Johnson >>> (canadaduane) >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >