Skip to content
This repository
Newer
Older
100644 135 lines (126 sloc) 4.556 kb
a98db7c6 »
2010-03-27 Use Array.wrap uniformly
1 require 'active_support/core_ext/array/wrap'
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
2 require 'active_support/callbacks'
3
4 module ActiveModel
de65c82b »
2010-06-15 Changes call backs to callbacks.
5 # == Active Model Callbacks
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
6 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
7 # Provides an interface for any class to have Active Record like callbacks.
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
8 #
de65c82b »
2010-06-15 Changes call backs to callbacks.
9 # Like the Active Record methods, the callback chain is aborted as soon as
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
10 # one of the methods in the chain returns false.
11 #
12 # First, extend ActiveModel::Callbacks from the class you are creating:
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
13 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
14 # class MyModel
15 # extend ActiveModel::Callbacks
16 # end
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
17 #
de65c82b »
2010-06-15 Changes call backs to callbacks.
18 # Then define a list of methods that you want callbacks attached to:
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
19 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
20 # define_model_callbacks :create, :update
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
21 #
aa2235be »
2010-07-18 replacing around with for in the comments for callbacks
22 # This will provide all three standard callbacks (before, around and after) for
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
23 # both the :create and :update methods. To implement, you need to wrap the methods
de65c82b »
2010-06-15 Changes call backs to callbacks.
24 # you want callbacks on in a block so that the callbacks get a chance to fire:
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
25 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
26 # def create
57bc25c5 »
2011-01-09 Use run_callbacks; the generated _run_<name>_callbacks method is not …
27 # run_callbacks :create do
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
28 # # Your create action methods here
29 # end
30 # end
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
31 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
32 # Then in your class, you can use the +before_create+, +after_create+ and +around_create+
33 # methods, just as you would in an Active Record module.
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
34 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
35 # before_create :action_before_create
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
36 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
37 # def action_before_create
38 # # Your code here
39 # end
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
40 #
41 # You can choose not to have all three callbacks by passing a hash to the
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
42 # define_model_callbacks method.
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
43 #
efbb7356 »
2011-11-19 Small docs fix in Active Model callbacks module
44 # define_model_callbacks :create, :only => [:after, :before]
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
45 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
46 # Would only create the after_create and before_create callback methods in your
47 # class.
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
48 module Callbacks
49 def self.extended(base)
50 base.class_eval do
51 include ActiveSupport::Callbacks
52 end
53 end
54
45230813 »
2010-06-14 Minor changes to callbacks and conversion API docs.
55 # define_model_callbacks accepts the same options define_callbacks does, in case
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
56 # you want to overwrite a default. Besides that, it also accepts an :only option,
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
57 # where you can choose if you want all types (before, around or after) or just some.
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
58 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
59 # define_model_callbacks :initializer, :only => :after
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
60 #
de65c82b »
2010-06-15 Changes call backs to callbacks.
61 # Note, the <tt>:only => <type></tt> hash will apply to all callbacks defined on
2a4b780a »
2011-05-23 Remove extra white spaces on ActiveModel docs.
62 # that method call. To get around this you can call the define_model_callbacks
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
63 # method as many times as you need.
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
64 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
65 # define_model_callbacks :create, :only => :after
66 # define_model_callbacks :update, :only => :before
67 # define_model_callbacks :destroy, :only => :around
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
68 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
69 # Would create +after_create+, +before_update+ and +around_destroy+ methods only.
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
70 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
71 # You can pass in a class to before_<type>, after_<type> and around_<type>, in which
de65c82b »
2010-06-15 Changes call backs to callbacks.
72 # case the callback will call that class's <action>_<type> method passing the object
73 # that the callback is being called on.
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
74 #
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
75 # class MyModel
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
76 # extend ActiveModel::Callbacks
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
77 # define_model_callbacks :create
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
78 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
79 # before_create AnotherClass
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
80 # end
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
81 #
582228e4 »
2010-01-15 ActiveModel::Callbacks documentation
82 # class AnotherClass
83 # def self.before_create( obj )
de65c82b »
2010-06-15 Changes call backs to callbacks.
84 # # obj is the MyModel instance that the callback is being called on
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
85 # end
86 # end
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
87 #
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
88 def define_model_callbacks(*callbacks)
89 options = callbacks.extract_options!
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
90 options = {
91 :terminator => "result == false",
92 :scope => [:kind, :name],
93 :only => [:before, :around, :after]
ef0da581 »
2010-08-03 no callbacks should be created for empty array [#5289 state:resolved]
94 }.merge(options)
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
95
38ab982c »
2011-11-30 Log 'Filter chain halted as CALLBACKNAME rendered or redirected' ever…
96 types = Array.wrap(options.delete(:only))
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
97
98 callbacks.each do |callback|
99 define_callbacks(callback, options)
100
101 types.each do |type|
6b099975 »
2010-12-27 No need to symbolize these.
102 send("_define_#{type}_model_callback", self, callback)
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
103 end
104 end
105 end
106
107 def _define_before_model_callback(klass, callback) #:nodoc:
13e00ce6 »
2010-04-09 fix stack trace lines on class_eval
108 klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
109 def self.before_#{callback}(*args, &block)
110 set_callback(:#{callback}, :before, *args, &block)
111 end
112 CALLBACK
113 end
114
115 def _define_around_model_callback(klass, callback) #:nodoc:
13e00ce6 »
2010-04-09 fix stack trace lines on class_eval
116 klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
117 def self.around_#{callback}(*args, &block)
118 set_callback(:#{callback}, :around, *args, &block)
119 end
120 CALLBACK
121 end
122
123 def _define_after_model_callback(klass, callback) #:nodoc:
13e00ce6 »
2010-04-09 fix stack trace lines on class_eval
124 klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
125 def self.after_#{callback}(*args, &block)
126 options = args.extract_options!
127 options[:prepend] = true
a98db7c6 »
2010-03-27 Use Array.wrap uniformly
128 options[:if] = Array.wrap(options[:if]) << "!halted && value != false"
6d390671 »
2009-12-29 Move ActiveRecord callbacks implementation to ActiveModel and make us…
129 set_callback(:#{callback}, :after, *(args << options), &block)
130 end
131 CALLBACK
132 end
133 end
a98db7c6 »
2010-03-27 Use Array.wrap uniformly
134 end
Something went wrong with that request. Please try again.