forked from TheOdinProject/theodinproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.rb
58 lines (44 loc) · 1.53 KB
/
user.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
class User < ApplicationRecord
acts_as_voter
after_create :enroll_in_foundations
devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable,
:omniauthable, omniauth_providers: %i[github google]
validates :email, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :username, length: { in: 2..100 }
validates :learning_goal, length: { maximum: 1700 }
has_many :lesson_completions, foreign_key: :student_id, dependent: :destroy
has_many :completed_lessons, through: :lesson_completions, source: :lesson
has_many :project_submissions, dependent: :destroy
has_many :user_providers, dependent: :destroy
has_many :flags, foreign_key: :flagger_id, dependent: :destroy
belongs_to :path
def progress_for(course)
@progress ||= Hash.new { |hash, c| hash[c] = CourseProgress.new(c, self) }
@progress[course]
end
def completed?(lesson)
completed_lessons.pluck(:id).include?(lesson.id)
end
def latest_completed_lesson
return if last_lesson_completed.nil?
Lesson.find(last_lesson_completed.lesson_id)
end
def active_for_authentication?
super && !banned?
end
def inactive_message
!banned? ? super : :banned
end
def dismissed_flags
flags.where(taken_action: :dismiss)
end
private
def last_lesson_completed
lesson_completions.order(created_at: :asc).last
end
def enroll_in_foundations
default_path = Path.default_path
update(path_id: default_path.id) if default_path.present?
end
end