Module: Passport::User::InstanceMethods

Defined in:
lib/passport/core/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

user.facebook_token user.facebook_token?



51
52
53
54
55
56
57
58
59
# File 'lib/passport/core/user.rb', line 51

def method_missing(meth, *args, &block)
  if meth.to_s =~ /(\w+)_token(\?)?/
    service = $1
    return access_token?(service) unless $2.blank?
    return access_token(service)
  end

  super(meth, *args, &block)
end

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
16
# File 'lib/passport/core/user.rb', line 11

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

Instance Method Details

#access_token(service) ⇒ Object



41
42
43
# File 'lib/passport/core/user.rb', line 41

def access_token(service)
  self.access_tokens.detect { |token| token.service == service.to_s }
end

#access_token?(service) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/passport/core/user.rb', line 45

def access_token?(service)
  access_token(service).blank?
end

#save(options = {}) {|result| ... } ⇒ Object

Yields:

  • (result)


18
19
20
21
22
23
24
# File 'lib/passport/core/user.rb', line 18

def save(options = {}, &block)
  block = nil if block_given? && Passport.process?
  options[:validate] = true unless options.has_key?(:validate)
  result = super(options, &block)
  yield(result) unless block.nil?
  result
end

#update_attributes(attributes, &block) ⇒ Object



36
37
38
39
# File 'lib/passport/core/user.rb', line 36

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

#validate_passportObject



26
27
28
29
30
31
32
33
34
# File 'lib/passport/core/user.rb', line 26

def validate_passport
  Passport.authenticate(self) do |token|
    if token
      access_tokens << token
    else
      errors.add("Passport validation error")
    end
  end
end