Class: Topic

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

Instance Method Summary collapse

Methods inherited from Content

#fork_to!, #public?, #to_resource_h

Methods included from WithUsages

#content_used_in?, #navigable_content_in, #usage_in_organization, #usage_in_organization_of_type, #used_in?

Methods included from WithSlug

#normalize_slug!, #rebase!, #rebased_dup, #transparent_id, #transparent_params

Methods included from WithProgress

#completed_for?, #completion_percentage_for, #dirty_progress_if_structural_children_changed!, #dirty_progresses!, #has_progress_for?, #once_completed_for?, #progress_for

Methods included from Mumuki::Domain::Syncable

#platform_class_name, #sync_key

Methods inherited from ApplicationRecord

active_between, aggregate_of, all_except, defaults, #delete, #destroy!, enum_prefixed_translations_for, numbered, organic_on, resource_fields, #save, #save_and_notify!, #save_and_notify_changes!, serialize_symbolized_hash_array, teaser_on, #update_and_notify!, update_or_create!, whitelist_attributes, with_pg_retry, with_temporary_token

Methods included from WithPgLock

#with_pg_lock

Instance Method Details

#as_chapter_of(book) ⇒ Object



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

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

#first_lessonObject



14
15
16
# File 'app/models/topic.rb', line 14

def first_lesson
  lessons.first
end

#fork_children_into!(dup, organization, syncer) ⇒ Object

Forking



48
49
50
# File 'app/models/topic.rb', line 48

def fork_children_into!(dup, organization, syncer)
  dup.lessons = lessons.map { |lesson| lesson.guide.fork_to!(organization, syncer, quiet: true).as_lesson_of(dup) }
end

#import_from_resource_h!(resource_h) ⇒ Object



18
19
20
21
22
23
24
# File 'app/models/topic.rb', line 18

def import_from_resource_h!(resource_h)
  dirty_progress_if_structural_children_changed! do
    self.assign_attributes resource_h.except(:lessons, :description)
    self.description = resource_h[:description].squeeze(' ')
    rebuild_lessons! resource_h[:lessons].to_a.map { |it| lesson_for(it) }
  end
end

#monolessonObject



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

def monolesson
  @monolesson ||= lessons.to_a.single
end

#monolesson?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/topic.rb', line 42

def monolesson?
  monolesson.present?
end

#pending_lessons(user) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'app/models/topic.rb', line 52

def pending_lessons(user)
  Exercise
    .with_pending_assignments_for(
      user,
      lessons
        .includes(:guide)
        .references(:guide)
        .joins('left join exercises exercises on exercises.guide_id = guides.id'))
    .group('guides.id', 'lessons.number', 'lessons.id')
end

#reindex_usages!Object



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

def reindex_usages!
  Chapter.where(topic: self).map(&:book).each(&:reindex_usages!)
end

#to_expanded_resource_hObject



26
27
28
# File 'app/models/topic.rb', line 26

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