in Rails where do you put your Sweepers?
Asked Answered
C

3

7

Is there a convention in Rails to put Sweeper classes in a particular directory location?

UPDATE: Since observers are put into app/models, I'm assuming sweepers are no different, as long as the name always ends with "sweeper".

Chaqueta answered 26/1, 2012 at 19:21 Comment(2)
Just so you know, and related to my answer, observers don't need to go on app/models.Armchair
Right, I guess this one is purely about standards. There's no functionality that depends on it.Chaqueta
A
5

I like to put them in the app/sweepers directory.

I also put Presenters in the app/presenters directory...and Observers in the app/observers directory.

Armchair answered 26/1, 2012 at 19:26 Comment(4)
<<If you’re using Active Record within Rails, observer classes are usually stored in app/models with the naming convention of app/models/audit_observer.rb.>> api.rubyonrails.org/classes/ActiveRecord/Observer.htmlChaqueta
Hmm, that is where they are put if you generate them, I have always found it a less obvious place to put them. Thanks for the link though.Armchair
Is there a cmd flag to let the generator put files in a different directory?Chaqueta
Good question, doesn't look like it. github.com/rails/rails/blob/master/activerecord/lib/rails/…Armchair
C
0

Try putting them in the app/models directory.

Citronella answered 26/1, 2012 at 19:27 Comment(0)
S
-2

Sweepers

Cache sweeping is a mechanism which allows you to get around having a ton of expire_{page,action,fragment} calls in your code. It does this by moving all the work required to expire cached content into na ActionController::Caching::Sweeper class. This class is an Observer that looks for changes to an object via callbacks, and when a change occurs it expires the caches associated with that object in an around or after filter.

Continuing with our Product controller example, we could rewrite it with a sweeper like this:

class StoreSweeper < ActionController::Caching::Sweeper
  # This sweeper is going to keep an eye on the Product model
  observe Product

  # If our sweeper detects that a Product was created call this
  def after_create(product)
          expire_cache_for(product)
  end

  # If our sweeper detects that a Product was updated call this
  def after_update(product)
          expire_cache_for(product)
  end

  # If our sweeper detects that a Product was deleted call this
  def after_destroy(product)
          expire_cache_for(product)
  end

  private
  def expire_cache_for(record)
    # Expire the list page now that we added a new product
    expire_page(:controller => '#{record}', :action => 'list')

    # Expire a fragment
    expire_fragment(:controller => '#{record}', 
      :action => 'recent', :action_suffix => 'all_products')
  end
end

The sweeper has to be added to the controller that will use it. So, if we wanted to expire the cached content for the list and edit actions when the create action was called, we could do the following:

class ProductsController < ActionController

  before_filter :authenticate, :only => [ :edit, :create ]
  caches_page :list
  caches_action :edit
  cache_sweeper :store_sweeper, :only => [ :create ]

  def list; end

  def create
    expire_page :action => :list
    expire_action :action => :edit
  end

  def edit; end

end

source rails guide

Stealthy answered 28/7, 2019 at 3:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.