Class: Guide
Instance Method Summary
collapse
Methods included from WithStats
#started?, #stats_for
#ensure_expectations_format, #expectations=, #expectations_yaml, #expectations_yaml=, #own_expectations
Methods included from WithUsages
#usage_in_organization, #usage_in_organization_of_type
Methods included from WithSlug
#rebase!, #rebased_dup, #slug_parts
#description_teaser
Methods included from Syncable
#platform_class_name, #sync_key
aggregate_of, all_except, defaults, numbered, #save, #save_and_notify!, #save_and_notify_changes!, serialize_symbolized_hash_array, #update_and_notify!, update_or_create!, whitelist_attributes
Instance Method Details
#as_complement_of(book) ⇒ Object
106
107
108
|
# File 'app/models/guide.rb', line 106
def as_complement_of(book) book.complements.find_by(guide_id: id) || Complement.new(guide: self, book: book)
end
|
#as_lesson_of(topic) ⇒ Object
102
103
104
|
# File 'app/models/guide.rb', line 102
def as_lesson_of(topic)
topic.lessons.find_by(guide_id: id) || Lesson.new(guide: self, topic: topic)
end
|
#chapter ⇒ Object
27
28
29
|
# File 'app/models/guide.rb', line 27
def chapter
lesson.try(:chapter) end
|
#clear_progress!(user) ⇒ Object
15
16
17
18
19
20
21
|
# File 'app/models/guide.rb', line 15
def clear_progress!(user)
transaction do
exercises.each do |exercise|
exercise.find_assignment_for(user)&.destroy!
end
end
end
|
#done_for?(user) ⇒ Boolean
56
57
58
|
# File 'app/models/guide.rb', line 56
def done_for?(user)
stats_for(user).done?
end
|
#exercises_count ⇒ Object
31
32
33
|
# File 'app/models/guide.rb', line 31
def exercises_count
exercises.count
end
|
#first_exercise ⇒ Object
48
49
50
|
# File 'app/models/guide.rb', line 48
def first_exercise
exercises.first
end
|
#fork_to!(organization, syncer) ⇒ Object
114
115
116
117
118
119
120
|
# File 'app/models/guide.rb', line 114
def fork_to!(organization, syncer)
rebased_dup(organization).tap do |dup|
dup.exercises = exercises.map(&:dup)
dup.save!
syncer.export! dup
end
end
|
#import_from_resource_h!(resource_h) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'app/models/guide.rb', line 60
def import_from_resource_h!(resource_h)
self.assign_attributes whitelist_attributes(resource_h)
self.language = Language.for_name(resource_h.dig(:language, :name))
self.save!
resource_h[:exercises]&.each_with_index do |e, i|
exercise = Exercise.find_by(guide_id: self.id, bibliotheca_id: e[:id])
exercise_type = e[:type] || 'problem'
exercise = exercise ?
exercise.ensure_type!(exercise_type.as_module_name) :
exercise_type.as_module.new(guide_id: self.id, bibliotheca_id: e[:id])
exercise.import_from_resource_h! (i+1), e
end
new_ids = resource_h[:exercises].map { |it| it[:id] }
self.exercises.where.not(bibliotheca_id: new_ids).destroy_all
reload
end
|
#lesson ⇒ Object
23
24
25
|
# File 'app/models/guide.rb', line 23
def lesson
usage_in_organization_of_type Lesson
end
|
#next_exercise(user) ⇒ Object
44
45
46
|
# File 'app/models/guide.rb', line 44
def next_exercise(user)
pending_exercises(user).order('public.exercises.number asc').first
end
|
#pending_exercises(user) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'app/models/guide.rb', line 35
def pending_exercises(user)
exercises.
joins("left join public.assignments assignments
on assignments.exercise_id = exercises.id
and assignments.submitter_id = #{user.id}
and assignments.submission_status = #{Mumuki::Domain::Status::Submission::Passed.to_i}").
where('assignments.id is null')
end
|
#resettable? ⇒ Boolean
110
111
112
|
# File 'app/models/guide.rb', line 110
def resettable?
usage_in_organization.resettable?
end
|
52
53
54
|
# File 'app/models/guide.rb', line 52
def search_tags
exercises.flat_map(&:search_tags).uniq
end
|
#to_markdownified_resource_h ⇒ Object
91
92
93
94
95
96
97
98
99
100
|
# File 'app/models/guide.rb', line 91
def to_markdownified_resource_h
to_resource_h.tap do |guide|
%i(corollary description teacher_info).each do |it|
guide[it] = Mumukit::ContentType::Markdown.to_html(guide[it])
end
%i(hint corollary description teacher_info).each do |it|
guide[:exercises].each { |exercise| exercise[it] = Mumukit::ContentType::Markdown.to_html(exercise[it]) }
end
end
end
|
#to_resource_h ⇒ Object
82
83
84
85
86
87
88
89
|
# File 'app/models/guide.rb', line 82
def to_resource_h
as_json(only: %i(beta type id_format private expectations corollary teacher_info authors collaborators extra))
.symbolize_keys
.merge(super)
.merge(exercises: exercises.map(&:to_resource_h))
.merge(language: language.to_embedded_resource_h)
.compact
end
|