Class: Topic

Inherits:
Content show all
Defined in:
app/models/topic.rb

Instance Method Summary collapse

Methods included from WithUsages

#usage_in_organization, #usage_in_organization_of_type

Methods included from WithSlug

#import!, #slug_parts

Methods included from WithDescription

#description_teaser

Methods inherited from ApplicationRecord

aggregate_of, all_except, defaults, name_model_as, numbered, #save, #save_and_notify!, #save_and_notify_changes!, #update_and_notify!, update_or_create!

Instance Method Details

#as_chapter_of(book) ⇒ Object



35
36
37
# File 'app/models/topic.rb', line 35

def as_chapter_of(book)
  book.chapters.find_by(topic_id: id) || Chapter.new(topic: self, book: book)
end

#first_lessonObject



24
25
26
# File 'app/models/topic.rb', line 24

def first_lesson
  lessons.first
end

#import_from_json!(json) ⇒ Object



28
29
30
31
32
33
# File 'app/models/topic.rb', line 28

def import_from_json!(json)
  self.assign_attributes json.except('lessons', 'id', 'description', 'teacher_info')
  self.description = json['description'].squeeze(' ')
  rebuild! json['lessons'].map { |it| lesson_for(it) }
  Organization.all.each { |org| org.reindex_usages! }
end

#pending_lessons(user) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/topic.rb', line 12

def pending_lessons(user)
  guides.
      joins('left join public.exercises exercises
              on exercises.guide_id = guides.id').
      joins("left join public.assignments assignments
              on assignments.exercise_id = exercises.id
              and assignments.submitter_id = #{user.id}
              and assignments.status = #{Mumuki::Laboratory::Status::Passed.to_i}").
      where('assignments.id is null').
      group('public.guides.id', 'lessons.number').map(&:lesson)
end