27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Page and action caching<br />

Alternatively, pass a constant<br />

Rather than passing the symbolized version of the name along to the cache_sweeper<br />

method, you can also alternatively pass along a class:<br />

cache_sweeper TicketsSweeeper<br />

This doesn’t perform any differently than passing in a symbol, but is really helpful if<br />

your sweeper was modularized:<br />

cache_sweeper Ticketee::TicketsSweeper<br />

You can’t pass a modularized sweeper name as a symbol, and so the<br />

cache_sweeper method supports passing both a symbol and a constant reference<br />

as well.<br />

sweeper and its brethren to live. 10 In this directory you’ll create a new file called app/<br />

sweepers/tickets_sweeper.rb and fill it with this content:<br />

class TicketsSweeper < ActionController::Caching::Sweeper<br />

observe Ticket<br />

def after_create(ticket)<br />

# expire fragment code goes here<br />

end<br />

end<br />

You’ll get around to adding the expire fragment code in just a bit, but first a bit of<br />

explanation is needed. A sweeper looks and acts much the same as an observer. By<br />

calling the observe method at the top of the TicketsSweeper, you tell this sweeper to<br />

watch the Ticket class for changes. The after_create method here will be called<br />

after creation of a new Ticket object, but because you’re in a sweeper, you’ll have<br />

access to the controller’s parameters also. With them, you can use what’s usually available<br />

in the controller to expire the cached fragments.<br />

To do this, you can call the expire_fragment method, passing it a regular expression.<br />

This regular expression will match all cached fragments for the ticket’s project<br />

for all users, effectively wiping clean the slate for this project in terms of cached pages.<br />

Inside your after_create method you’ll put this:<br />

expire_fragment(/projects\/#{ticket.project.id}\/.*?/)<br />

Now when you create a new ticket for a project, this expire_fragment method will be<br />

called. Let’s try this out now, creating a new ticket by clicking the New Ticket link on a<br />

project’s page and filling out the form. Once you’ve clicked the Create Ticket button<br />

on the form, you’ll see this in the console:<br />

Expire fragment (?-mix:projects\/1\/.*?) (327.3ms)<br />

Rails has gone through and expired all the fragments associated with this ticket’s project.<br />

If you now go into tmp/cache and into any one of the directories there looking<br />

10 Because it doesn’t really belong in the controllers, helpers, models, observers, or views directory, but is still a<br />

vital part of your application.<br />

455

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!