Module: AuthlogicConnect::Common::User::InstanceMethods

Defined in:
lib/authlogic_connect/common/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/authlogic_connect/common/user.rb', line 15

def self.included(base)
  base.class_eval do
    has_many :access_tokens, :class_name => "AccessToken", :dependent => :destroy
    belongs_to :active_token, :class_name => "AccessToken", :dependent => :destroy
    accepts_nested_attributes_for :access_tokens, :active_token
  end
end

Instance Method Details

#authenticated_withObject



23
24
25
# File 'lib/authlogic_connect/common/user.rb', line 23

def authenticated_with
  @authenticated_with ||= self.access_tokens.collect{|t| t.service_name.to_s}
end

#authenticated_with?(service) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/authlogic_connect/common/user.rb', line 27

def authenticated_with?(service)
  self.access_tokens.detect{|t| t.service_name.to_s == service.to_s}
end

#get_token(service_name) ⇒ Object



40
41
42
# File 'lib/authlogic_connect/common/user.rb', line 40

def get_token(service_name)
  self.access_tokens.detect {|i| i.service_name.to_s == service_name.to_s}
end

#has_token?(service_name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/authlogic_connect/common/user.rb', line 36

def has_token?(service_name)
  !get_token(service_name).nil?
end

#save(options = {}, &block) ⇒ Object

core save method coordinating how to save the user. we dont’ want to ru validations based on the authentication mission we are trying to accomplish. instead, we just return save as false. the next time around, when we recieve the callback, we will run the validations. when you call ‘current_user_session’ in ApplicationController, it leads to calling ‘save’ on this User object via “session.record.save”, from the ‘persisting?’ method. So we don’t want any of this to occur when that save is called, and the only way to check currently is to check if there is a block_given?



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/authlogic_connect/common/user.rb', line 55

def save(options = {}, &block)
  self.errors.clear
  # log_state
  options = {} if options == false
  options[:validate] = true unless options.has_key?(:validate)
  save_options = ActiveRecord::VERSION::MAJOR < 3 ? options[:validate] : options
  
  # kill the block if we're starting authentication
  authenticate_via_protocol(block_given?, options) do |redirecting|
    block = nil if redirecting
    # forces you to validate, only if a block is given
    result = super(save_options) # validate!
    unless block.nil?
      cleanup_authentication_session(options)
      yield(result)
    end
    result
  end
end

#update_attributes(attributes, &block) ⇒ Object



31
32
33
34
# File 'lib/authlogic_connect/common/user.rb', line 31

def update_attributes(attributes, &block)
  self.attributes = attributes
  save(:validate => true, &block)
end