I noticed a lot of repetition across different Sweeper models in my
app expiring the same fragments and so have been trying to move all
the expire_fragment calls into another model which the sweepers can
then use but it's not working as I'd hoped..
I've created a CacheDestroyer model and want to be able to call
something to the effect of: CacheDestroyer.clear_all_page_relations
but I'm struggling to find how to make the "expire_fragment" method
available to this model.
I've tried putting "include ActionController::Caching::Fragments" in
the CacheDestroyer model but it doesn't seem to be working (undefined
method `expire_fragment' for CacheDestroyer:Class (NoMethodError))
Any ideas how I can get the "expire_fragment" method to be available
to this model?
Thanks in advance for any help,
The problem with trying to access what the controller did/does
[caching and sweeping the cache] from the model is that it breaks one
of Rails' philosophies: MVC [the separation of model - view -
controller code]. The proper solution is to use Sweeper classes.
There's a good bit of information out there on these as well. The guys
over at RailsEnvy have a good tutorial you might want to check out:
Hope that helps,
Thanks Russell. I am already using sweepers however. What I can't get
to work is to refactor repetitive code from multiple sweepers into one
common model which can then be referenced from each sweeper. It
doesn't have to be a model really, I'm just trying to clean my code
For example, in my app, I have some active record models; Location,
Page, Talent. Each has a sweeper to expire caches but there's a lot of
repetition between those three sweepers. I can't combine them into one
sweeper because they're slightly different but I don't want to keep
copying & pasting the same expire_fragment snippets from one to the
other.. seems so un-rails like.
What I'd do is create a module for that shared functionality and mix
it in to the sweepers.
Ahhh, that sorted it.. Cheers for the help Russell.