Sha256: 8445c1cecaafa45b9ceba27b04aed8936adae12a19c48fde0876160cb5b40db9

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module Passport
  module User
  
    def self.included(base)
      base.validate :validate_passport, :if => lambda { Passport.authenticating?(:user) }
      base.send :include, InstanceMethods
    end
    
    module InstanceMethods
      
      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
      
      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
      
      def validate_passport
        Passport.authenticate(self) do |token|
          if token
            access_tokens << token
          else
            errors.add("Passport validation error")
          end
        end
      end
      
      def update_attributes(attributes, &block)
        self.attributes = attributes
        save(:validate => true, &block)
      end
      
      def access_token(service)
        self.access_tokens.detect { |token| token.service == service.to_s }
      end
      
      def access_token?(service)
        access_token(service).blank?
      end
      
      # user.facebook_token
      # user.facebook_token?
      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
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
passport-0.1.1 lib/passport/core/user.rb