1
+ [ ![ Build Status] ( https://travis-ci.org/chamnap/mongoid-observers.svg?branch=master )] ( https://travis-ci.org/chamnap/mongoid-observers ) [ ![ Code Climate] ( https://codeclimate.com/github/chamnap/mongoid-observers.png )] ( https://codeclimate.com/github/chamnap/mongoid-observers )
1
2
# Mongoid::Observers
2
3
3
- TODO: Write a gem description
4
+ Mongoid Observers (removed from core in Mongoid 4.0). Because this gem doesn't exist and I need to use it very often. Therefore, I extract the code from mongoid on my own.
4
5
5
6
## Installation
6
7
@@ -18,12 +19,128 @@ Or install it yourself as:
18
19
19
20
## Usage
20
21
21
- TODO: Write usage instructions here
22
+ Observer classes respond to life cycle callbacks to implement trigger-like
23
+ behavior outside the original class. This is a great way to reduce the
24
+ clutter that normally comes when the model class is burdened with
25
+ functionality that doesn't pertain to the core responsibility of the
26
+ class. Mongoid's observers work similar to ActiveRecord's. Example:
27
+
28
+ ``` ruby
29
+ class CommentObserver < Mongoid ::Observer
30
+ def after_save (comment )
31
+ Notifications .comment(
32
+ " [email protected] " ,
" New comment was posted" , comment
33
+ ).deliver
34
+ end
35
+ end
36
+ ```
37
+
38
+ This Observer sends an email when a Comment#save is finished.
39
+
40
+ ``` ruby
41
+ class ContactObserver < Mongoid ::Observer
42
+ def after_create (contact )
43
+ contact.logger.info(' New contact added!' )
44
+ end
45
+
46
+ def after_destroy (contact )
47
+ contact.logger.warn (" Contact with an id of #{ contact.id } was destroyed!" )
48
+ end
49
+ end
50
+ ```
51
+
52
+ This Observer uses logger to log when specific callbacks are triggered.
53
+
54
+ #### Observing a class that can't be inferred
55
+
56
+ Observers will by default be mapped to the class with which they share a
57
+ name. So CommentObserver will be tied to observing Comment,
58
+ ProductManagerObserver to ProductManager, and so on. If you want to
59
+ name your observer differently than the class you're interested in
60
+ observing, you can use the Observer.observe class method which takes
61
+ either the concrete class (Product) or a symbol for that class (: product ):
62
+
63
+ ``` ruby
64
+ class AuditObserver < Mongoid ::Observer
65
+ observe :account
66
+
67
+ def after_update (account )
68
+ AuditTrail .new (account, " UPDATED" )
69
+ end
70
+ end
71
+ ```
72
+
73
+ If the audit observer needs to watch more than one kind of object,
74
+ this can be specified with multiple arguments:
75
+
76
+ ``` ruby
77
+ class AuditObserver < Mongoid ::Observer
78
+ observe :account , :balance
79
+
80
+ def after_update (record )
81
+ AuditTrail .new (record, " UPDATED" )
82
+ end
83
+ end
84
+ ```
85
+
86
+ The AuditObserver will now act on both updates to Account and Balance
87
+ by treating them both as records.
88
+
89
+ #### Available callback methods
90
+
91
+ * after_initialize
92
+ * before_validation
93
+ * after_validation
94
+ * before_create
95
+ * around_create
96
+ * after_create
97
+ * before_update
98
+ * around_update
99
+ * after_update
100
+ * before_upsert
101
+ * around_upsert
102
+ * after_upsert
103
+ * before_save
104
+ * around_save
105
+ * after_save
106
+ * before_destroy
107
+ * around_destroy
108
+ * after_destroy
109
+
110
+ #### Storing Observers in Rails
111
+
112
+ If you're using Mongoid within Rails, observer classes are usually stored
113
+ in ` app/models ` with the naming convention of ` app/models/audit_observer.rb ` .
114
+
115
+ #### Configuration
116
+
117
+ In order to activate an observer, list it in the ` config.mongoid.observers `
118
+ configuration setting in your ` config/application.rb ` file.
119
+
120
+ ``` ruby
121
+ config.mongoid.observers = :comment_observer , :signup_observer
122
+ ```
123
+
124
+ Observers will not be invoked unless you define them in your
125
+ application configuration.
126
+
127
+ #### Loading
128
+
129
+ Observers register themselves with the model class that they observe,
130
+ since it is the class that notifies them of events when they occur.
131
+ As a side-effect, when an observer is loaded, its corresponding model
132
+ class is loaded.
133
+
134
+ Observers are loaded after the application initializers, so that
135
+ observed models can make use of extensions. If by any chance you are
136
+ using observed models in the initialization, you can
137
+ still load their observers by calling ` ModelObserver.instance ` before.
138
+ Observers are singletons and that call instantiates and registers them.
22
139
23
140
## Contributing
24
141
25
142
1 . Fork it ( http://github.com/ <my-github-username >/mongoid-observers/fork )
26
143
2 . Create your feature branch (` git checkout -b my-new-feature ` )
27
144
3 . Commit your changes (` git commit -am 'Add some feature' ` )
28
145
4 . Push to the branch (` git push origin my-new-feature ` )
29
- 5 . Create new Pull Request
146
+ 5 . Create new Pull Request
0 commit comments