lib/authlogic/session/callbacks.rb in authlogic-1.4.1 vs lib/authlogic/session/callbacks.rb in authlogic-1.4.2
- old
+ new
@@ -1,85 +1,68 @@
module Authlogic
module Session
# = Callbacks
#
- # Just like in ActiveRecord you have before_save, before_validation, etc. You have similar callbacks with Authlogic, see all callbacks below.
+ # Just like in ActiveRecord you have before_save, before_validation, etc. You have similar callbacks with Authlogic, see the METHODS constant below. The order of execution is as follows:
+ #
+ # Here is the order they execute
+ #
+ # before_find
+ # after_find
+ # save record if changed?
+ #
+ # before_validation
+ # before_validation_on_create
+ # before_validation_on_update
+ # validate
+ # after_validation_on_update
+ # after_validation_on_create
+ # after_validation
+ # save record if changed?
+ #
+ # before_save
+ # before_create
+ # before_update
+ # after_update
+ # after_create
+ # after_save
+ # save record if changed?
+ #
+ # before_destroy
+ # destroy
+ # after_destroy
+ #
+ # Notice the "save record if changed?" lines above. This helps with performance. If you need to make changes to the associated record, there is no need to save the record, Authlogic will do it for you.
+ # This allow multiple modules to modify the record and execute as few queries as possible.
+ #
+ # **WARNING**: unlike ActiveRecord, these callbacks must be set up on the class level:
+ #
+ # class UserSession < Authlogic::Session::Base
+ # before_validation :my_method
+ # validate :another_method
+ # # ..etc
+ # end
+ #
+ # You can NOT define a "before_validation" method, this is bad practice and does not allow Authlogic to extend properly with multiple extensions. Please ONLY use the method above.
module Callbacks
- CALLBACKS = %w(before_create after_create before_destroy after_destroy before_find after_find before_save after_save before_update after_update before_validation validate after_validation)
-
+ METHODS = [
+ "before_find", "after_find",
+ "before_validation", "before_validation_on_create", "before_validation_on_update", "validate", "after_validation_on_update", "after_validation_on_create", "after_validation",
+ "before_save", "before_create", "before_update", "after_update", "after_create", "after_save",
+ "before_destroy", "after_destroy"
+ ]
+
def self.included(base) #:nodoc:
- [:destroy, :find_record, :save, :validate].each do |method|
- base.send :alias_method_chain, method, :callbacks
- end
-
base.send :include, ActiveSupport::Callbacks
- base.define_callbacks *CALLBACKS
+ base.define_callbacks *METHODS
end
- # Runs the following callbacks:
- #
- # before_destroy
- # destroy
- # after_destroy # only if destroy is successful
- def destroy_with_callbacks
- run_callbacks(:before_destroy)
- result = destroy_without_callbacks
- run_callbacks(:after_destroy) if result
- result
- end
-
- # Runs the following callbacks:
- #
- # before_find
- # find_record
- # after_find # if a record was found
- def find_record_with_callbacks
- run_callbacks(:before_find)
- result = find_record_without_callbacks
- run_callbacks(:after_find) if result
- result
- end
-
- # Runs the following callbacks:
- #
- # before_save
- # before_create # only if new_session? == true
- # before_update # only if new_session? == false
- # save
- # # the following are only run is save is successful
- # after_save
- # before_update # only if new_session? == false
- # before_create # only if new_session? == true
- def save_with_callbacks(&block)
- run_callbacks(:before_save)
- if new_session?
- run_callbacks(:before_create)
- else
- run_callbacks(:before_update)
- end
- result = save_without_callbacks(&block)
- if result
- run_callbacks(:after_save)
-
- if new_session?
- run_callbacks(:after_create)
- else
- run_callbacks(:after_update)
+ METHODS.each do |method|
+ class_eval <<-"end_eval", __FILE__, __LINE__
+ def #{method}
+ run_callbacks(:#{method})
end
- end
- result
- end
-
- # Runs the following callbacks:
- #
- # before_validation
- # validate
- # after_validation # only if errors.empty?
- def validate_with_callbacks
- run_callbacks(:before_validation)
- validate_without_callbacks
- run_callbacks(:validate)
- run_callbacks(:after_validation) if errors.empty?
+ end_eval
end
end
end
end
\ No newline at end of file