Module: ActiveObject::Callbacks
- Defined in:
- lib/active_object/callbacks.rb
Overview
当一个新对象的 Base#save
方法被调用时:
-
(-)
save
-
(-)
valid
-
(1)
before_validation
-
(2)
before_validation_on_create
-
(-)
validate
-
(-)
validate_on_create
-
(3)
after_validation
-
(4)
after_validation_on_create
-
(5)
before_save
-
(6)
before_create
-
(-)
create
-
(7)
after_create
-
(8)
after_save
这里总共有8个回调.已经存在的记录调用Base#save
时,除了_on_create
回调被_on_update
取代外,其它都一样.
示例:
class CreditCard < ActiveObject::Base
# Strip everything but digits, so the user can specify "555 234 34" or
# "5552-3434" or both will mean "55523434"
def before_validation_on_create
self.number = number.gsub(/[^0-9]/, "") if attribute_present?("number")
end
end
class Subscription < ActiveObject::Base
before_create :record_signup
private
def record_signup
self.signed_up_on = Date.today
end
end
继承回调序列
除了重写回调方法外,它也可以通过使用回调宏注册回调.宏能添加行为到回调序列,这样在多级继承时,能保留回调序列的完整。 示例:
class Topic < ActiveObject::Base
before_destroy :destroy_author
end
class Reply < Topic
before_destroy :destroy_readers
end
现在, 当 Topic#destroy
运行时只调用 destroy_author
.当Reply#destroy
运行时,将调用destroy_author
和 # destroy_readers
。
Constant Summary collapse
- CALLBACKS =
%w( after_find after_initialize before_save after_save before_create after_create before_update after_update before_validation after_validation before_validation_on_create after_validation_on_create before_validation_on_update after_validation_on_update before_destroy after_destroy )
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #after_create ⇒ Object
- #after_destroy ⇒ Object
-
#after_initialize ⇒ Object
在调用
Base.new
时,对象被初始化之后执行。. -
#after_save ⇒ Object
在执行
Base.save
被调用(不管是创建还是更新). - #after_update ⇒ Object
- #after_validation ⇒ Object
- #after_validation_on_create ⇒ Object
- #after_validation_on_update ⇒ Object
- #before_create ⇒ Object
- #before_destroy ⇒ Object
-
#before_save ⇒ Object
在执行
Base.save
之前被调用 (不管是创建还是更新). - #before_update ⇒ Object
- #before_validation ⇒ Object
- #before_validation_on_create ⇒ Object
- #before_validation_on_update ⇒ Object
-
#destroy_with_callbacks ⇒ Object
:nodoc:.
- #initialize_with_callbacks(*args) ⇒ Object
-
#valid_with_callbacks? ⇒ Boolean
:nodoc:.
Class Method Details
.included(base) ⇒ Object
:nodoc:
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/active_object/callbacks.rb', line 66 def self.included(base) #:nodoc: base.extend Observable [:initialize,:create_or_update, :valid?, :create, :update, :destroy].each do |method| base.send :alias_method_chain, method, :callbacks end base.send :include, ActiveSupport::Callbacks base.define_callbacks *CALLBACKS end |
Instance Method Details
#after_create ⇒ Object
106 |
# File 'lib/active_object/callbacks.rb', line 106 def after_create() end |
#after_destroy ⇒ Object
154 |
# File 'lib/active_object/callbacks.rb', line 154 def after_destroy() end |
#after_initialize ⇒ Object
在调用Base.new
时,对象被初始化之后执行。
78 |
# File 'lib/active_object/callbacks.rb', line 78 def after_initialize() end |
#after_save ⇒ Object
在执行Base.save
被调用(不管是创建还是更新).
class Contact < ActiveObject::Base
after_save { logger.info( 'New contact saved!' ) }
end
94 |
# File 'lib/active_object/callbacks.rb', line 94 def after_save() end |
#after_update ⇒ Object
117 |
# File 'lib/active_object/callbacks.rb', line 117 def after_update() end |
#after_validation ⇒ Object
129 |
# File 'lib/active_object/callbacks.rb', line 129 def after_validation() end |
#after_validation_on_create ⇒ Object
133 |
# File 'lib/active_object/callbacks.rb', line 133 def after_validation_on_create() end |
#after_validation_on_update ⇒ Object
137 |
# File 'lib/active_object/callbacks.rb', line 137 def after_validation_on_update() end |
#before_create ⇒ Object
104 |
# File 'lib/active_object/callbacks.rb', line 104 def before_create() end |
#before_destroy ⇒ Object
152 |
# File 'lib/active_object/callbacks.rb', line 152 def before_destroy() end |
#before_save ⇒ Object
在执行Base.save
之前被调用 (不管是创建还是更新).
87 |
# File 'lib/active_object/callbacks.rb', line 87 def before_save() end |
#before_update ⇒ Object
115 |
# File 'lib/active_object/callbacks.rb', line 115 def before_update() end |
#before_validation ⇒ Object
127 |
# File 'lib/active_object/callbacks.rb', line 127 def before_validation() end |
#before_validation_on_create ⇒ Object
131 |
# File 'lib/active_object/callbacks.rb', line 131 def before_validation_on_create() end |
#before_validation_on_update ⇒ Object
135 |
# File 'lib/active_object/callbacks.rb', line 135 def before_validation_on_update() end |
#destroy_with_callbacks ⇒ Object
:nodoc:
155 156 157 158 159 160 |
# File 'lib/active_object/callbacks.rb', line 155 def destroy_with_callbacks #:nodoc: return false if callback(:before_destroy) == false result = destroy_without_callbacks callback(:after_destroy) result end |
#initialize_with_callbacks(*args) ⇒ Object
80 81 82 83 84 |
# File 'lib/active_object/callbacks.rb', line 80 def initialize_with_callbacks(*args) result = initialize_without_callbacks(*args) callback(:after_initialize) result end |
#valid_with_callbacks? ⇒ Boolean
:nodoc:
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/active_object/callbacks.rb', line 139 def valid_with_callbacks? #:nodoc: return false if callback(:before_validation) == false if new_record? then result = callback(:before_validation_on_create) else result = callback(:before_validation_on_update) end return false if false == result result = valid_without_callbacks? callback(:after_validation) if new_record? then callback(:after_validation_on_create) else callback(:after_validation_on_update) end return result end |