Module: Authlogic::Session::Callbacks

Included in:
Base
Defined in:
lib/authlogic/session/callbacks.rb

Overview

Callbacks

Just like in ActiveRecord you have before_save, before_validation, etc. You have similar callbacks with Authlogic, see all callbacks below.

Constant Summary collapse

CALLBACKS =
%w(before_create after_create before_destroy after_destroy before_find after_find before_save after_save before_update after_update before_validation after_validation)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:


9
10
11
12
13
14
15
16
# File 'lib/authlogic/session/callbacks.rb', line 9

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
end

Instance Method Details

#destroy_with_callbacksObject

Runs the following callbacks:

before_destroy
destroy
after_destroy # only if destroy is successful

23
24
25
26
27
28
# File 'lib/authlogic/session/callbacks.rb', line 23

def destroy_with_callbacks
  run_callbacks(:before_destroy)
  result = destroy_without_callbacks
  run_callbacks(:after_destroy) if result
  result
end

#find_record_with_callbacksObject

Runs the following callbacks:

before_find
find_record
after_find # if a record was found

35
36
37
38
39
40
# File 'lib/authlogic/session/callbacks.rb', line 35

def find_record_with_callbacks
  run_callbacks(:before_find)
  result = find_record_without_callbacks
  run_callbacks(:after_find) if result
  result
end

#save_with_callbacks(&block) ⇒ Object

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

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/authlogic/session/callbacks.rb', line 52

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)
    end
  end
  result
end

#validate_with_callbacksObject

Runs the following callbacks:

before_validation
validate
after_validation # only if errors.empty?

77
78
79
80
81
# File 'lib/authlogic/session/callbacks.rb', line 77

def validate_with_callbacks
  run_callbacks(:before_validation)
  validate_without_callbacks
  run_callbacks(:after_validation) if errors.empty?
end