Module: SimplestAuth::Model::ClassMethods

Defined in:
lib/simplest_auth/model.rb

Instance Method Summary collapse

Instance Method Details

#active_record?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/simplest_auth/model.rb', line 23

def active_record?
  defined?(ActiveRecord) && ancestors.include?(ActiveRecord::Base)
end

#authenticate(email, password) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/simplest_auth/model.rb', line 35

def authenticate(email, password)
  if active_record?
    found = where(:email => email).first
  elsif data_mapper? || mongo_mapper?
    found = first(:email => email)
  end

  (found && found.authentic?(password)) ? found : nil
end

#authenticate_by(ident) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/simplest_auth/model.rb', line 45

def authenticate_by(ident)
  if active_record?
    instance_eval <<-EOM
      def authenticate(#{ident}, password)
        found = where(:#{ident} => #{ident}).first
        (found && found.authentic?(password)) ? found : nil
      end
    EOM
  elsif data_mapper? || mongo_mapper?
    instance_eval <<-EOM
      def authenticate(#{ident}, password)
        found = first(:#{ident} => #{ident})
        (found && found.authentic?(password)) ? found : nil
      end
    EOM
  end
end

#data_mapper?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/simplest_auth/model.rb', line 27

def data_mapper?
  defined?(DataMapper) && ancestors.include?(DataMapper::Resource)
end

#mongo_mapper?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/simplest_auth/model.rb', line 31

def mongo_mapper?
  defined?(MongoMapper) && ancestors.include?(MongoMapper::Document)
end

#session_keyObject



63
64
65
66
67
68
69
# File 'lib/simplest_auth/model.rb', line 63

def session_key
  if name.to_s.respond_to?(:underscore)
    "#{name.underscore}_id".to_sym
  else
    "#{name.downcase}_id".to_sym
  end
end