Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 103 lines (64 sloc) 2.921 kb
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
1 = ActionMailer Callbacks
2
5650248 Anthony Caliendo convert to a rails plugin structure
authored
3 ActionMailer callbacks provides basic callback support for ActionMailer. It providers similar functionality to before and after filters available in ActionController.
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
4
5 == Installation
6
5650248 Anthony Caliendo convert to a rails plugin structure
authored
7 Install as a plugin in your vendor/plugins directory.
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
8
5650248 Anthony Caliendo convert to a rails plugin structure
authored
9 If your rails version supports it, you can do:
10
11 script/plugin install git://github.com/AnthonyCaliendo/action_mailer_callbacks.git
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
12
fb47567 Anthony Caliendo refactor: encapsulate chain/callback logic into separate classes extendi...
authored
13 == Compatibility
14
15 This plugin is currently compatible with Rails 2.1 (untested with 2.2).
16
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
17
18
4832ee8 Anthony Caliendo fix editor cutoff which prevented class from loading
authored
19 == How Do I Use It?
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
20
fb47567 Anthony Caliendo refactor: encapsulate chain/callback logic into separate classes extendi...
authored
21 There are 3 ways to define a callback. In each case, the callback method/block is passed the mail object as the only argument.
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
22
23 You may define a callback using a block:
24 class FooMailer < ActionMailer::Base
25 after_deliver do |mail|
26 ...
27 end
28 end
29
fb47567 Anthony Caliendo refactor: encapsulate chain/callback logic into separate classes extendi...
authored
30
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
31 You may also define a callback using a symbol/string for a method name:
32 class FooMailer < ActionMailer::Base
33 before_deliver :append_advertisement
34
35 def append_advertisement(mail)
36 ...
37 end
38 end
39
fb47567 Anthony Caliendo refactor: encapsulate chain/callback logic into separate classes extendi...
authored
40
41 You can also pass a Method object directly in:
42 class FooMailer < ActionMailer::Base
43 class Handler
44 def self.bar
45 ...
46 end
47 end
48
49 after_deliver Handler.method(:bar)
50 end
51
52
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
53 Callbacks take options which can be used to define which mail types (i.e. methods) they will be applied to.
54 These options take the format of *only* and *except*.
55 - An *only* callback will only be run for methods which match the passed method names.
56 - An *except* callback will be called for all methods EXCEPT those that match the passed method names
57 The options can take either an array of strings/symbols, or a single string/symbol.
58
59 class FooMailer < ActionMailer::Base
60 before_deliver :append_disclaimer, :only => [:email_friend, :announce_something]
61 after_deliver :notify_user, :except => :invite_user
62
63 ...
64 end
65
5650248 Anthony Caliendo convert to a rails plugin structure
authored
66
67
9b11a98 Anthony Caliendo added ability to halt callback chain
authored
68 == What About Halting the Chain?
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
69
9b11a98 Anthony Caliendo added ability to halt callback chain
authored
70 You can halt the chain in either a before or after callback. In order to do this, just call +halt_callback_chain+ in the block (or +self.class.halt_callback_chain+ in an instance method).
71 If the chain is halted in a before callback, the email will *NOT* be delivered and no other callbacks will be invoked (either any after callbacks or any remaining before callbacks).
72 If the chain is halted in an after callback, the email will have already been sent and all before callbacks would have run, but any remaining after callbacks will not be invoked.
73
74 class FooMailer < ActionMailer::Base
75 before_deliver do |mail|
76 halt_callback_chain if invalid_mail?(mail)
77 end
78
79 after_deliver :abort
80
81 def abort(mail)
82 self.class.halt_callback_chain
83 end
84 end
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
85
5650248 Anthony Caliendo convert to a rails plugin structure
authored
86
87
e1747db Anthony Caliendo extract code into modules which are included/extended to ActionMailer
authored
88 == TODO
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
89
5650248 Anthony Caliendo convert to a rails plugin structure
authored
90 1) Add an around_deliver callback which would allow around advice
91 2) Add ability to skip filters
92 3) Add ability to prepend filters in the chain
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
93
e1747db Anthony Caliendo extract code into modules which are included/extended to ActionMailer
authored
94 Any bug reports or features requests are welcomed.
b079f17 Anthony Caliendo initial commit of version 1.0 action_mailer_callbacks
authored
95
96
5650248 Anthony Caliendo convert to a rails plugin structure
authored
97
fb47567 Anthony Caliendo refactor: encapsulate chain/callback logic into separate classes extendi...
authored
98 === Contact
99 Feel free to email me at:
100 action.mailer.callbacks@gmail.com
101
5650248 Anthony Caliendo convert to a rails plugin structure
authored
102
fb47567 Anthony Caliendo refactor: encapsulate chain/callback logic into separate classes extendi...
authored
103 Copyright (c) 2009 Anthony Caliendo, released under the MIT license
Something went wrong with that request. Please try again.