Mephisto Pagination Plugin updated: what's new?

posted: October 11th, 2007 · by: Sven

in: Programming · tagged as: , , , , , ·  21 comments »

Like I mentioned an updated version of the Mephisto Paged Article Lists plugin is available in the Subversion repository. You can grab this awesome stuff right here: http://svn.artweb-design.de/stuff/mephisto/mephisto_paged_article_list

Like with the Inverse Captcha Plugin some more information about the new version of this plugin are due, too. So, what’s new here?

Let me first mention that the plugin now pages tag pages, which I have been asked for by some people. This stuff will just work like expected.

Also, it’s worth noting that I am running my blog on Mephisto edge on Rails rev 7405 (i.e. that’s Rails edge as of some weeks ago, it’s not the Rails 2.0 release candidate!). I have neither tested this with Mephisto 0.7.3 on Rails 1.2.3 nor Rails 2.0 yet. The will_paginate plugin works on Rails 1.2.3 and most probably will work on Rails 2.0, so IMO there’s no obvious reason why the Mephisto Paged Article Lists plugin shouldn’t do so as well. But I can’t garantuee anything like that. Please post your results in the comments if you happen to check this out!

Ok, with this out of the way, let’s look at interesting stuff.

Now riding (a liquidized) will_paginate

You most probably know that Rails edge has kicked pagination as a built-in core feature and now requires us to choose from a variety of plugins to get this functionality. This really is a good thing. But of course it means that we have to re-integrate a plugin in our applications.

Rick chose will_paginate as the pagination solution that is shipped with Mephisto right out of the box now. Therefor the most obvious solution was to refactor my plugin to now rely on will_paginate, too.

To get will_paginate working from within Liquid templates I needed to do some wicked trickery though with the WillPaginate::Collection, which I guess probably deserves an article on its own. But in short the Mephisto PagedArticleList plugin now basically does the following:

  • It registers a before_filter which extracts the page parameter from the url path. (This is necessary because of Mephisto’s dynamic routes mechanism.)
  • It replaces the MephistoController actions dispatch_list and dispatch_tags with versions that assign paginated collections to the template.
  • It adds a to_liquid method to the WillPaginate::Collection and provides a new WillPaginate::CollectionDrop to be able to access the collection from within the template accordingly.
  • It registers a series of filters to Liquid in order to allow you to use the paginated collection from within your templates.

That’s it.

All you need to know though is that you can choose from the following Liquid filters:

Prev/next page links

Like before, there’s a convenient prev_next_page_links filter that provides prev/next links like those you can see on this blog. Here’s a screenshot:

The usage of this filter has slightly changed because of the WillPaginate collection that we are now using. You can now do:


{{ articles | prev_next_page_links: path_info }}    

You can also provide two alternative link texts and/or a separator to this filter. I.e. the following usages will work as expected:


{{ articles | prev_next_page_links: path_info, 'previous', 'next' }}
{{ articles | prev_next_page_links: path_info, 'previous', 'next', '·' }}
{{ articles | prev_next_page_links: path_info, '·' }}    

Single link filters

Probably you might want to control these links individually. You can then (like before) use the following filters:


{{ articles | link_to_prev_page: path_info }}   
{{ articles | link_to_next_page: path_info }}       

Again, you can inject a different link text. Like so:


{{ articles | link_to_prev_page: path_info, 'previous' }}   
{{ articles | link_to_next_page: path_info, 'next' }}   

Digg-style navigation

As we are riding on the back of will_paginate, only some minor additions were necessary to enable the usage of the will_paginate view helper from within our Liquid templates. That means that now you can do this:


{{  articles | will_paginate: path_info }}

… which will result in a nice list of pagination links. You can now easily add some CSS styles (e.g., like the stylesheet proposed here) and achieve something that looks very similar to the paginators on Digg, Flickr and others.

Probably something like this:

What do you think?

Leave a comment

21 Comments

  1. Hank said October 27th, 2007 at 10:44 AM  

    Bah - New Mephisto / Edge Rails broke your stuff. I just get Liquid type conversion errors now. I don’t really know where to look in your code to debug, and production.log isn’t being helpful. Reproduce by upgrading mephisto trunk and freezing edge rails.

  2. Ashley Moran said October 28th, 2007 at 01:40 PM  

    Was just about install this then I saw Hank’s comment. Think you will be able fix this one Sven? I’d like pagination on my blog, the index page is getting a bit unwieldy.

  3. Sven said October 30th, 2007 at 06:14 PM  

    Hank,

    I’m not sure what’s version you installed as Edge Rails. When I try to freeze to the current Edge Rails without any further version specified, I’ll not even get mongrel started. (Following the rails-core list I believe this is because they messed with routes and Mephisto’s own routing won’t work with this.)

    I have just updated my locale setup to Mephisto rev3009 (which is the current trunk) and froze Rails to TAG_rel_2-0-0_PR. I commented out Mephisto’s dependencies on ActionWebService (I don’t use the XMLRPC api anyways) and everything’s working like a charm.

    If you encounter any Liquid errors and don’t know how to debug them, in Mephisto it’s helpful to change lib/mephisto/liquid/liquid_template.rb like this:

    Index: lib/mephisto/liquid/liquid_template.rb
    ===================================================================
    --- lib/mephisto/liquid/liquid_template.rb  (revision 3009)
    +++ lib/mephisto/liquid/liquid_template.rb  (working copy)
    @@ -15,7 +15,7 @@
             # give the include tag access to files in the site's fragments directory
             ::Liquid::Template.file_system = ::Liquid::LocalFileSystem.new(File.join(@site.theme.path, 'templates'))
             tmpl = ::Liquid::Template.parse(template.read.to_s)
    -        returning tmpl.render(assigns, :registers => {:controller => controller}) do |result|
    +        returning tmpl.render!(assigns, :registers => {:controller => controller}) do |result|
               yield tmpl, result if block_given?
             end
           end
    

    This prevents Mephisto to swallow Liquid errors.

  4. Sven said October 30th, 2007 at 06:32 PM  

    Ashley,

    I don’t think there’s a serious problem with the plugin. I’m locally running the same setup you’ve described on your blog and everything’s fine.

  5. Ashley Moran said November 4th, 2007 at 12:05 AM  

    You’re right Sven, it works fine now

  6. Ryan said November 17th, 2007 at 11:55 PM  

    I have been trying to get this to work for a couple of hours. I don’t get any errors, where I add:

    {{ articles | prevnextpagelinks: pathinfo }}

    I get :

    #
  7. Sven said November 18th, 2007 at 09:12 PM  

    Hey Ryan,

    actually these are error messages like they are produced in Liquid templates (look at the HTML source code of your page, you’ll see something more than the # marks, nothing really informative, though).

    To find out more about the errors you can modify lib/mephisto/liquid/liquid_template.rb so that the errors don’t get caught by Liquid. See the patch that I’ve given some comments above, it’s actually just a single ! character added.

    Ryan, from your comment I’ve got really no clue about the problem. You might be running an old version of Mephisto or Rails or something? (Look here for some hints on working versions: http://www.artweb-design.de/projects/mephisto-plugin-paged-article-lists) If you can’t figure it out yourself just drop me an email with some kind of error backtrace and info about your system and I’ll see if I can help.

  8. Steph said November 20th, 2007 at 03:37 PM  

    Hi,

    I have installed this plugin with : script/plugin install http://svn.artweb-design.de/stuff/mephisto/mephistopagedarticle_list

    • ./mephistopagedarticle_list/README
    • ./mephistopagedarticle_list/init.rb
    • ./mephistopagedarticlelist/lib/pagedarticlelist/linkhelpers.rb
    • ./mephistopagedarticlelist/lib/pagedarticlelist/mephistocontroller.rb
    • ./mephistopagedarticlelist/lib/pagedarticlelist/willpaginate.rb

    But when I restart mongrel, I have this error :

    NameError (uninitialized constant WillPaginate::Collection)

    Do you have an idea ?

    Thanks.

  9. y0rtz said November 28th, 2007 at 07:27 PM  

    Hi Steph, i was running the same error message as specified by you:

    NameError (uninitialized constant WillPaginate::Collection)

    when i was trying to enter my admin section and browse a section.

    Actually i was able to fix this by installing will paginate plugin.

    http://errtheblog.com/post/4791

    Actually everything works fine on my mephisto install, except the fact that i wasted an entire afternoon trying to figure out how to use the plugin (i am talking about the pagination plugin from Sven).

    Thanks for your precious work man, but i find it really whimsical to understand how to use it properly.

    From what i got from your wiki it seems you just need to add those filters.

    When i try to add those liquid filters into my layout, nothing happens.

    Also, from what i have understand there must be a place where you set up how many articles you want to display, but i wasn’t able to find it by myself.

    Again, i think that Sven simply did an astonishing job with this plugin and with the other ones he released, i ‘d really like to know how to get it to work now.

    I AM COMPLETELY STUCK.

    Any support on this would be highly appreciated.

  10. Sven said November 28th, 2007 at 07:46 PM  

    Steph, y0rtz,

    my impression is that you guys probably have a too old version of Mephisto installed. Mephisto really should ship with the withpaginate plugin itself. I have no idea if you by any means could use the plugin somehow with a version of Mephisto that doesn’t come with withpaginate.

    if you have any urgent questions on this you can try to catch me in #mephisto (irc.freenode.net), too.

  11. y0rtz said November 29th, 2007 at 12:43 AM  

    Ehy Sven i tryed to see if i could find you in #mephisto with no luck:( Anyway, I can tell that i am running mephisto 0.7.2 stable flawlessly since i wanted to install the contact form plugin. Actually i tryed to download two fresh mephisto 0.7.2 and 0.7.3 release from the official site for the wonderful blogging system. No luck, I have found that both of them lack of support for the paginate plugin, what am i doing wrong? And also, where can i download the mephisto release with the “withpaginate” plugin? Thanks in advance and forgive my lameness…

  12. Sven said November 29th, 2007 at 09:32 AM  

    Hey y0rtz,

    well I’m sticking around there the whole day long :) Are you from Australia or something?

    So, no, have a look here: http://www.artweb-design.de/projects/mephisto-plugin-paged-article-lists … there are some Rails and Mephisto versions listed that are known to work. Actually I think that Rails 2.0 RC1 should work, too, with a fairly recent Mephisto version.

    But you have to live on “edge”. Look at the Mephisto wiki for more info: http://mephisto.stikipad.com/help/show/Installing+Mephisto … the releases you mention are actually quite old. Unfortunatly there hasn’t been a release for more than a year now.

    I hope that helps! If you can’t install Mephisto edge than I’m sorry that you probably won’t be able to use my pagination plugin, too … without some serious hacking, that is.

  13. Xavier Shay said December 29th, 2007 at 01:00 AM  

    http://svn.artweb-design.de/stuff/mephisto/mephistopagedarticlelist/lib/pagedarticle_list/

    When I look there and also when I check out (r305), I don’t see will_paginate.rb. This concerns me … there is no code otherwise to implement to_liquid.

    I looked at the svn log and it seems it should be there, so now I am really confused.

  14. Sven said December 29th, 2007 at 11:15 AM  

    Hi Xavier,

    yes, it should be there.

    You have to install it separately. Please refer to the plugin’s project page … I’ve separated the will_paginate_liquidized plugin for several reasons.

  15. Eric Davis said January 3rd, 2008 at 09:41 AM  

    I installed this plugin to my Mephisto install and was also getting some Liquid errors. After trying different Mephisto and Rails versions I found they all had the errors. Turns out the issue was with my liquid templates and a liquid partial.

    Working on Mephisto r3036 and Rails r8129, http://theadmin.org

  16. Hank said June 22nd, 2008 at 01:15 AM  

    I’m getting the dreaded WillPaginate::Collection NameError like Steph was having above. Any thoughts on how to fix this?

  17. kenderson said October 9th, 2008 at 04:24 AM  

    Sven, I ran the install you suggested above, and am getting the following errors. Any suggestions? I am running Mephisto 0.8 and rails 2.0.2.

    ** Starting Rails with development environment… Exiting /Library/Ruby/Gems/1.8/gems/builder-2.1.2/lib/blankslate.rb:106:in blankslate_original_append_features': cyclic include detected (ArgumentError) from /Library/Ruby/Gems/1.8/gems/builder-2.1.2/lib/blankslate.rb:106:inappend_features’ from /Rails/Ashleyandken/ak/trunk/vendor/plugins/willpaginateliquidized/trunk/lib/willpaginate/liquidized/viewhelpers.rb:3:in include' from /Rails/Ashleyandken/ak/trunk/vendor/plugins/will_paginate_liquidized/trunk/lib/will_paginate/liquidized/view_helpers.rb:3 from /Rails/Ashleyandken/ak/trunk/vendor/rails/activesupport/lib/active_support/dependencies.rb:203:inloadwithoutnewconstantmarking’ from /Rails/Ashleyandken/ak/trunk/vendor/rails/activesupport/lib/active_support/dependencies.rb:203:in load_file' from /Rails/Ashleyandken/ak/trunk/vendor/rails/activesupport/lib/active_support/dependencies.rb:342:innewconstantsin’ from /Rails/Ashleyandken/ak/trunk/vendor/rails/activesupport/lib/active_support/dependencies.rb:202:in load_file' from /Rails/Ashleyandken/ak/trunk/vendor/rails/activesupport/lib/active_support/dependencies.rb:94:inrequire_or_load’ … 40 levels… from /Rails/Ashleyandken/ak/trunk/vendor/rails/activesupport/lib/active_support/dependencies.rb:496:in require' from /Rails/Ashleyandken/ak/trunk/vendor/rails/railties/lib/commands/server.rb:39 from ./script/server:3:inrequire’ from ./script/server:3

  18. Steven Bakker said November 16th, 2008 at 05:52 PM  

    @kenderson: I was pulling my hairs out on that error as well, and tried various versions of Mephisto to make it work, until I figured out the following: the installation of “willpaginateliquidized” pulls in the whole of the SVN repository, i.e. not only the trunk:

    script/plugin install \
        http://svn.artweb-design.de/stuff/rails/will_paginate_liquidized
    

    Gives me:

    • willpaginateliquidized/
      • tags/
      • willpaginater413/
      • willpaginater436/
      • trunk/

    It seems the rails initialisation causes all of these instances to be included, resulting in cyclic includes.

    You will have to keep only one of r413, r436, trunk. I did this by moving the whole of “willpaginateliquidized” out of the plugins directory and copying back one of the sub-directories to the plugins/willpaginateliquidized directory. For me, “trunk” didn’t work, neither did “r436”, but “r413” works!

    Hope this helps.

    Steven

  19. Satish Chauhan said January 2nd, 2009 at 06:43 AM  

    I have installed this plugin and its working fine for me, but I am have some issue when passing extra parameters in params, not getting other params parameter with the links (getting only page )

    eg.

    what I am getting : http://www.localhost:3000/products?page=2

    expected: http://www.localhost:3000/products?page=2&perpage=10&sortby=name

    Any support on this would be highly appreciated.

    Thanks

  20. Satish Chauhan said January 2nd, 2009 at 06:43 AM  

    I have installed this plugin and its working fine for me, but I have some issue/problem when passing extra parameters in params, not getting other params parameter with the links (getting only page )

    eg.

    what I am getting : http://www.localhost:3000/products?page=2

    expected: http://www.localhost:3000/products?page=2&perpage=10&sortby=name

    Any support on this would be highly appreciated.

    Thanks

Leave a comment

Name required
E-Mail and Website optional

If you can read this, you don't use a typical webbrowser that plays nice with CSS.
Please do not fill in anything here!

Hint: Markdown will be applied to your comment. If you post any code, be sure to escape underscores (like so: \_) if you do not want them to be converted to an <em>phasis.

artweb design
Sven Fuchs
Grünberger Str. 65
10245 Berlin, Germany


http://www.artweb-design.de

Fon +49 (30) 47 98 69 96
Fax +49 (30) 47 98 69 97