-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.rb
134 lines (91 loc) · 2.91 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require "data_mapper"
class User
include DataMapper::Resource
property :id, Serial
property :username, String,
:required => true,
:unique => true
property :email, String,
:format => :email_address,
:required => true,
:unique => true,
:messages => {
:format => "You must enter a valid email address."
}
property :password, BCryptHash,
:required => true
validates_confirmation_of :password
attr_accessor :password_confirmation
validates_length_of :password_confirmation, :min => 6, :if => lambda { |t| t.validation_status == :new }
property :validation_status, Enum[ :new, :change], default: :new
property :selection_status, Enum[ :none, :class, :teacher], default: :none
# validates_presence_of :password, :if => lambda { |t| t.status == :new }
# validates_presence_of :username, :if => lambda { |t| t.status == :new }
# validates_presence_of :email, :if => lambda { |t| t.status == :new }
property :admin, Boolean, default: false
property :class_time, DateTime, default: DateTime.parse(Time.new(2020).to_s)
property :teacher_time, DateTime, default: DateTime.parse(Time.new(2020).to_s)
def valid_password?(unhashed_password)
self.password == unhashed_password
end
def self.find_by_email(email)
self.first(:email => email)
end
has n, :user_subjects
has n, :subjects, through: :user_subjects
has n, :user_lessons
has n, :lessons, through: :user_lessons
belongs_to :registry
end
class UserSubject
include DataMapper::Resource
property :id, Serial
belongs_to :user
belongs_to :subject
end
class Subject
include DataMapper::Resource
property :id, Serial
property :name, String
validates_uniqueness_of :name
has n, :lessons, constraint: :destroy
belongs_to :category
has n, :user_subjects, constraint: :destroy
has n, :users, through: :user_subjects
end
class UserLesson
include DataMapper::Resource
property :id, Serial
belongs_to :user
belongs_to :lesson
end
class Lesson
include DataMapper::Resource
property :id, Serial
property :space, Integer
property :block, Integer
has n, :user_lessons, constraint: :destroy
has n, :users, through: :user_lessons
belongs_to :subject
belongs_to :teacher
end
class Category
include DataMapper::Resource
property :id, Serial
property :name, String, :unique => true
has n, :subjects, constraint: :destroy
end
class Teacher
include DataMapper::Resource
property :id, Serial
property :name, String, :unique => true
has n, :lessons, constraint: :destroy
end
class Registry
include DataMapper::Resource
property :id, Serial
property :name, Integer
has n, :users, constraint: :destroy
end
DataMapper.finalize()
DataMapper.auto_upgrade!()