metaduck
12Abr/100

Will_paginate and AJAX

I have been using will_paginate ever since I started on Rails.

It's a great plugin for classic page rendering, but what if you want to use it with AJAX, so it updates a document element (a floating window layer, for instance) with the results instead of reloading the page?

Well, inspired on Redline's blog post (which almost worked for the latest version of the will_paginate plugin), I have the solution.

Create a link renderer. I put in under lib/remote_link_renderer.rb so Rails always loads it on startup.

class RemoteLinkRenderer < WillPaginate::LinkRenderer

  def page_link_or_span(page, span_class = 'current', text = nil)
    text ||= page.to_s
    if page and page != current_page
      @template.link_to_remote(text, {:url => url_for(page), :method => :get}.merge(@remote))
    else
      @template.content_tag :span, text, :class => span_class
    end
  end
end

On the view, when you are rendering the pager, do it like this:

<%= will_paginate @collection, :renderer => 'RemoteLinkRenderer' , :remote => {:with => ’value’, :update => ‘some_div’} %>

That's it!

Filed under: Rails, Ruby No Comments
31Mar/100

How to paginate outside of the database with will_paginate

will_paginate is a great plugin. I have been using it since I started on Rails.

If you want to paginate records that are inside your database it works great.

But what if you want to paginate records that come from, say an external webservice?

Well, you can use the WillPaginate::Collection class.

This class will let you mimic the collection that will work on a pager, so you can use the view helper pager seamlessly throughout your app.

Just use it like this:

@entries = WillPaginate::Collection.create(1, 10) do |pager|
  result = Webservice.get(...) # get your results
  # inject the result array into the paginated collection:
  pager.replace(result)

  unless pager.total_entries
    # the pager didn't manage to guess the total count, do it manually
    pager.total_entries = result.count
  end
end
Filed under: Rails No Comments
4Nov/090

Tools for the job

toolsHere are some tools / gems / plugins I can't live without:

IDEs:

  • Aptana Rad Rails - very good at Rails and Ruby. It's based on Eclipse - Java - so it tends to bloat
  • gedit- Tiny footprint, simple. Great for web and PHP with the right plugins and conf tweaks

Ruby / Rails plugins:

12Out/094

Tutorial: building and styling a Rails app with styled_objects

How to keep your stylesheets clean and organized using Rails

This tutorial covers building a web application from scratch using Rails with the styled_objects plugin.

Filed under: CSS, Rails, Web Continue reading
10Out/090

styled_objects Rails plugin

Yesterday I released the styled_objects Rails plugin.

styled_objects is a Rails plugin for simplifying stylesheet management on your application.

Filed under: Rails, Web Continue reading
7Out/090

Rails cache in distributed environment

Page and fragment caching are life-savers for Rails application scalability. Specially for page cache, they can make your app fast, specially if you use a webserver like Nginx, serving static files directly without touching the Rails stack.

But maintaining cache consistency across a distributed Rails application can be challenging.

28Set/090

Capistrano deploying full text search on Rails using acts_as_xapian

acts_as_xapian is a rails plugin for Xapian, a full text search engine.

acts_as_xapian installs the search engine database inside the plugin directory, under xapiandbs. If you use Capistrano as a deployment tool and since the entire codebase is rewritten (including the plugin directory),  you will have to rebuild the entire index every time you do a deployment.