Module: MongoMapper::Token::ClassMethods

Defined in:
lib/mongomapper_token.rb

Instance Method Summary collapse

Instance Method Details

#find_by_token(token) ⇒ Object



24
25
26
# File 'lib/mongomapper_token.rb', line 24

def find_by_token(token)
  self.first(:conditions => {:token => token})
end

#token(*args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mongomapper_token.rb', line 6

def token(*args)
  options = args.extract_options!
  options[:length] ||= 4
  options[:contains] ||= :alphanumeric
  options[:index] = true unless options.has_key?(:index)

  self.key :token, :type => String
  self.ensure_index [[:token, 1]], :unique => true if options[:index]

  set_callback(:create, :before) do |document|
    document.create_token(options[:length], options[:contains])
  end

  set_callback(:save, :before) do |document|
    document.create_token_if_nil(options[:length], options[:contains])
  end
end