Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 103 lines (73 sloc) 2.894 kb
0544c78 @rafaelfranca s/active_record-observers/rails-observers/
rafaelfranca authored
1 # Rails::Observers
b4dcb34 @steveklabnik Initial commit.
authored
2
7e91713 @rafaelfranca Some documentation love
rafaelfranca authored
3 Rails Observers (removed from core in Rails 4.0)
b4dcb34 @steveklabnik Initial commit.
authored
4
5 ## Installation
6
7 Add this line to your application's Gemfile:
8
0544c78 @rafaelfranca s/active_record-observers/rails-observers/
rafaelfranca authored
9 gem 'rails-observers'
b4dcb34 @steveklabnik Initial commit.
authored
10
11 And then execute:
12
13 $ bundle
14
15 Or install it yourself as:
16
0544c78 @rafaelfranca s/active_record-observers/rails-observers/
rafaelfranca authored
17 $ gem install rails-observers
b4dcb34 @steveklabnik Initial commit.
authored
18
19 ## Usage
20
7e91713 @rafaelfranca Some documentation love
rafaelfranca authored
21 This gem contains two observers:
22
23 * Active Record Observer
24 * Action Controller Sweeper
25
26 ### Active Record Observer
27
28 Observer classes respond to life cycle callbacks to implement trigger-like
29 behavior outside the original class. This is a great way to reduce the
30 clutter that normally comes when the model class is burdened with
31 functionality that doesn't pertain to the core responsibility of the
32 class. Example:
33
34 ```ruby
35 class CommentObserver < ActiveRecord::Observer
36 def after_save(comment)
37 Notifications.comment("admin@do.com", "New comment was posted", comment).deliver
38 end
39 end
40 ```
41
42 This Observer sends an email when a Comment#save is finished.
43
44 ```ruby
45 class ContactObserver < ActiveRecord::Observer
46 def after_create(contact)
47 contact.logger.info('New contact added!')
48 end
49
50 def after_destroy(contact)
51 contact.logger.warn("Contact with an id of #{contact.id} was destroyed!")
52 end
53 end
54 ```
55
56 This Observer uses logger to log when specific callbacks are triggered.
57
58 ### Action Controller Sweeper
59
60 Sweepers are the terminators of the caching world and responsible for expiring caches when model objects change.
61 They do this by being half-observers, half-filters and implementing callbacks for both roles. A Sweeper example:
62
63 ```ruby
64 class ListSweeper < ActionController::Caching::Sweeper
65 observe List, Item
66
67 def after_save(record)
68 list = record.is_a?(List) ? record : record.list
69 expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id)
70 expire_action(:controller => "lists", :action => "all")
71 list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
72 end
73 end
74 ```
75
76 The sweeper is assigned in the controllers that wish to have its job performed using the `cache_sweeper` class method:
77
78 ```ruby
79 class ListsController < ApplicationController
80 caches_action :index, :show, :public, :feed
81 cache_sweeper :list_sweeper, :only => [ :edit, :destroy, :share ]
82 end
83 ```
84
85 In the example above, four actions are cached and three actions are responsible for expiring those caches.
86
87 You can also name an explicit class in the declaration of a sweeper, which is needed if the sweeper is in a module:
88
89 ```ruby
90 class ListsController < ApplicationController
91 caches_action :index, :show, :public, :feed
92 cache_sweeper OpenBar::Sweeper, :only => [ :edit, :destroy, :share ]
93 end
94 ```
b4dcb34 @steveklabnik Initial commit.
authored
95
96 ## Contributing
97
98 1. Fork it
99 2. Create your feature branch (`git checkout -b my-new-feature`)
100 3. Commit your changes (`git commit -am 'Added some feature'`)
101 4. Push to the branch (`git push origin my-new-feature`)
102 5. Create new Pull Request
Something went wrong with that request. Please try again.