/
models.rb
64 lines (47 loc) · 1.44 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Person
include Mongoid::Document
include Mongoid::Paranoia
include Mongoid::Versioning
include Mongoid::Timestamps
field :login, type: String, default: 'me'
field :password, type: String
field :name, type: String, default: 'me'
field :age, type: Integer
field :email, type: String
field :role, type: String
field :site, type: String
index({ name: 1 }, { name: 'name' })
has_one :account
has_many :pets
has_and_belongs_to_many :friends, class_name: 'Person'
embeds_one :profile
embeds_many :sites
validates_presence_of :name
validates_presence_of :role, message: 'no role'
validates_confirmation_of :password
validates_acceptance_of :terms_of_use
validates_uniqueness_of :email
validates_uniqueness_of :login, scope: :site, case_sensitive: false
validates_length_of :password, minimum: 8, maximum: 16
validates_length_of :login, in: 5..12
validates_format_of :email, with: /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i
validates_inclusion_of :role, in: ['admin', 'user']
validates_exclusion_of :email, in: ['foo@bar.com', 'fizz@buzz.com']
validates_inclusion_of :age, in: 0..200
validates_exclusion_of :age, in: 30..60
end
class Pet
include Mongoid::Document
belongs_to :person, index: :true
end
class Account
include Mongoid::Document
belongs_to :person
end
class Profile
include Mongoid::Document
end
class Site
include Mongoid::Document
embedded_in :person
end