forked from codebiff/sinatra-authentication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.rb
40 lines (29 loc) · 945 Bytes
/
models.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require "data_mapper"
require "bcrypt"
require "securerandom"
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db/auth.sqlite")
class User
include DataMapper::Resource
attr_accessor :password, :password_confirmation
property :id, Serial
property :email, String, :required => true, :unique => true, :format => :email_address
property :password_hash, Text
property :password_salt, Text
property :token, String
property :created_at, DateTime
property :admin, Boolean, :default => false
validates_presence_of :password
validates_confirmation_of :password
validates_length_of :password, :min => 6
after :create do
self.token = SecureRandom.hex
end
def generate_token
self.update!(:token => SecureRandom.hex)
end
def admin?
self.admin
end
end
DataMapper.finalize
DataMapper.auto_upgrade!