在 Rails 中,你把 Sweepers 放在哪里?
Rails 中是否有约定将 Sweeper 类放在特定的目录位置?
更新:由于观察者被放入app/models
中,我假设sweeper没有什么不同,只要名称始终以“sweeper”结尾即可。
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我喜欢将它们放在 app/sweepers 目录中。
我还将
Presenters
放在 app/presenters 目录中...并将Observers
放在 app/observers 目录中。I like to put them in the app/sweepers directory.
I also put
Presenters
in the app/presenters directory...andObservers
in the app/observers directory.尝试将它们放入
app/models
目录中。Try putting them in the
app/models
directory.缓存
清理是一种机制,可以让您避免代码中出现大量的 expire_{page,action,fragment} 调用。它通过将使缓存内容过期所需的所有工作移至
一个 ActionController::Caching::Sweeper 类。此类是一个观察者,它通过回调查找对象的更改,当发生更改时,它会使周围或之后过滤器中与该对象关联的缓存过期。
继续我们的产品控制器示例,我们可以使用扫描器重写它,如下所示:
扫描器必须添加到将使用它的控制器中。因此,如果我们想在调用创建操作时使列表和编辑操作的缓存内容过期,我们可以执行以下操作
:
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:
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:
source rails guide