-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlesson.rb
More file actions
72 lines (54 loc) · 2.2 KB
/
lesson.rb
File metadata and controls
72 lines (54 loc) · 2.2 KB
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
# frozen_string_literal: true
class Lesson < ApplicationRecord
self.ignored_columns += [:archived_at]
belongs_to :school, optional: true
belongs_to :school_class, optional: true
belongs_to :parent, optional: true, class_name: :Lesson, foreign_key: :copied_from_id, inverse_of: :copies
has_many :copies, dependent: :nullify, class_name: :Lesson, foreign_key: :copied_from_id, inverse_of: :parent
has_one :project, dependent: :destroy
has_many :remixes, through: :project
has_many :school_projects, through: :remixes
accepts_nested_attributes_for :project
before_validation :assign_school_from_school_class
validates :user_id, presence: true
validates :name, presence: true
validates :visibility, presence: true, inclusion: { in: %w[private teachers students public] }
validate :user_has_the_school_owner_or_school_teacher_role_for_the_school
validate :user_is_the_school_teacher_for_the_school_class
def self.users
User.from_userinfo(ids: pluck(:user_id))
end
def self.with_users
by_id = users.index_by(&:id)
all.map { |instance| [instance, by_id[instance.user_id]] }
end
def with_user
[self, User.from_userinfo(ids: user_id).first]
end
def submitted_count
return 0 unless project
project.remixes.count { |remix| remix.school_project&.submitted? }
end
def recalculate_submitted_projects_count!
with_lock do
count = school_projects.in_state(:submitted).count
update!(submitted_projects_count: count)
end
end
private
def assign_school_from_school_class
self.school ||= school_class&.school
end
def user_has_the_school_owner_or_school_teacher_role_for_the_school
return unless user_id_changed? && errors.blank? && school
user = User.new(id: user_id)
return if user.school_owner?(school)
return if user.school_teacher?(school)
msg = "'#{user_id}' does not have the 'school-owner' or 'school-teacher' role for organisation '#{school.id}'"
errors.add(:user, msg)
end
def user_is_the_school_teacher_for_the_school_class
return if !school_class || school_class.teacher_ids.include?(user_id)
errors.add(:user, "'#{user_id}' is not the 'school-teacher' for school_class '#{school_class.id}'")
end
end