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

#rebase!, #rebased_dup, #slug_parts, #sync_key

Methods included from WithDescription

#description_teaser

Methods inherited from ApplicationRecord

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_chapter_of(book) ⇒ Object



39
40
41
# File 'app/models/topic.rb', line 39

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

#fork_to!(organization, syncer) ⇒ Object

Forking



45
46
47
48
49
50
51
# File 'app/models/topic.rb', line 45

def fork_to!(organization, syncer)
  rebased_dup(organization).tap do |dup|
    dup.lessons = lessons.map { |lesson| lesson.guide.fork_to!(organization, syncer).as_lesson_of(self) }
    dup.save!
    syncer.export! dup
  end
end

#import_from_resource_h!(resource_h) ⇒ Object



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

def import_from_resource_h!(resource_h)
  self.assign_attributes resource_h.except(:lessons, :description)
  self.description = resource_h[:description].squeeze(' ')
  rebuild! resource_h[:lessons].to_a.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.submission_status = #{Mumuki::Domain::Status::Submission::Passed.to_i}").
      where('assignments.id is null').
      group('public.guides.id', 'lessons.number').map(&:lesson)
end

#to_resource_hObject



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

def to_resource_h
  super.merge(appendix: appendix, lessons: lessons.map(&:slug)).compact
end