active_record回调过程
activesupport-2.3.4/lib/active_support/callbacks.rb
activerecord-2.3.4/lib/active_record/callbacks.rb
activerecord-2.3.4/lib/active_record/transactions.rb
models/topic.rb
activerecord-2.3.4/lib/active_record/callbacks.rb
- [:create_or_update, :valid?, :create, :update, :destroy].each do |method|
- base.send :alias_method_chain, method, :callbacks
- end
- def after_create() end
- def create_with_callbacks #:nodoc:
- return false if callback(:before_create) == false
- result = create_without_callbacks
- callback(:after_create)
- result
- end
- private :create_with_callbacks
activesupport-2.3.4/lib/active_support/callbacks.rb
- def define_callbacks(*callbacks)
- callbacks.each do |callback|
- class_eval <<-"end_eval"
- def self.#{callback}(*methods, &block)
- callbacks = CallbackChain.build(:#{callback}, *methods, &block)
- @#{callback}_callbacks ||= CallbackChain.new
- @#{callback}_callbacks.concat callbacks
- end
- def self.#{callback}_callback_chain
- @#{callback}_callbacks ||= CallbackChain.new
- if superclass.respond_to?(:#{callback}_callback_chain)
- CallbackChain.new(
- superclass.#{callback}_callback_chain +
- @#{callback}_callbacks
- )
- else
- @#{callback}_callbacks
- end
- end
- end_eval
- end
- end
- def run_callbacks(kind, options = {}, &block)
- self.class.send("#{kind}_callback_chain").run(self, options, &block)
- end
activerecord-2.3.4/lib/active_record/transactions.rb
- base.class_eval do
- [:destroy, :save, :save!].each do |method|
- alias_method_chain method, :transactions
- end
- end
models/topic.rb
- after_create :clear_cache
base.rb
- def save
- create_or_update
- end
- def create_or_update
- raise ReadOnlyRecord if readonly?
- result = new_record? ? create : update
- result != false
- end
- def create
- if self.id.nil? && connection.prefetch_primary_key?(self.class.table_name)
- self.id = connection.next_sequence_value(self.class.sequence_name)
- end
- quoted_attributes = attributes_with_quotes
- statement = if quoted_attributes.empty?
- connection.empty_insert_statement(self.class.table_name)
- else
- "INSERT INTO #{self.class.quoted_table_name} " +
- "(#{quoted_column_names.join(', ')}) " +
- "VALUES(#{quoted_attributes.values.join(', ')})"
- end
- self.id = connection.insert(statement, "#{self.class.name} Create",
- self.class.primary_key, self.id, self.class.sequence_name)
- @new_record = false
- id
- end
莫愁前路无知己,天下无人不识君。