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 
      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

Instance Method Summary collapse

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_createObject



106
# File 'lib/active_object/callbacks.rb', line 106

def after_create() end

#after_destroyObject



154
# File 'lib/active_object/callbacks.rb', line 154

def after_destroy()  end

#after_initializeObject

在调用Base.new时,对象被初始化之后执行。



78
# File 'lib/active_object/callbacks.rb', line 78

def after_initialize() end

#after_saveObject

在执行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_updateObject



117
# File 'lib/active_object/callbacks.rb', line 117

def after_update() end

#after_validationObject



129
# File 'lib/active_object/callbacks.rb', line 129

def after_validation() end

#after_validation_on_createObject



133
# File 'lib/active_object/callbacks.rb', line 133

def after_validation_on_create()  end

#after_validation_on_updateObject



137
# File 'lib/active_object/callbacks.rb', line 137

def after_validation_on_update()  end

#before_createObject



104
# File 'lib/active_object/callbacks.rb', line 104

def before_create() end

#before_destroyObject



152
# File 'lib/active_object/callbacks.rb', line 152

def before_destroy() end

#before_saveObject

在执行Base.save之前被调用 (不管是创建还是更新).



87
# File 'lib/active_object/callbacks.rb', line 87

def before_save() end

#before_updateObject



115
# File 'lib/active_object/callbacks.rb', line 115

def before_update() end

#before_validationObject



127
# File 'lib/active_object/callbacks.rb', line 127

def before_validation() end

#before_validation_on_createObject



131
# File 'lib/active_object/callbacks.rb', line 131

def before_validation_on_create() end

#before_validation_on_updateObject



135
# File 'lib/active_object/callbacks.rb', line 135

def before_validation_on_update() end

#destroy_with_callbacksObject

: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:

Returns:

  • (Boolean)


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