Sha256: 6be9a6b11ef38da874da6630731df6524880e2e9cde6709723fc3aa76e575662

Contents?: true

Size: 1.8 KB

Versions: 34

Compression:

Stored size: 1.8 KB

Contents

class Topic < Content
  numbered :lessons
  aggregate_of :lessons

  has_many :lessons, -> { order(number: :asc) }, dependent: :delete_all

  has_many :guides, -> { order('lessons.number') }, through: :lessons
  has_many :exercises, -> { order('exercises.number') }, through: :guides

  markdown_on :appendix

  alias_method :structural_children, :lessons

  def first_lesson
    lessons.first
  end

  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

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

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

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

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

  def monolesson?
    monolesson.present?
  end

  ## Forking

  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

  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

  private

  def lesson_for(slug)
    Guide.find_by!(slug: slug).as_lesson_of(self)
  rescue ActiveRecord::RecordNotFound
    raise "Guide for slug #{slug} could not be found"
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
mumuki-domain-9.23.0 app/models/topic.rb
mumuki-domain-9.22.1 app/models/topic.rb
mumuki-domain-9.22.0 app/models/topic.rb
mumuki-domain-9.21.0 app/models/topic.rb
mumuki-domain-9.20.0 app/models/topic.rb
mumuki-domain-9.19.0 app/models/topic.rb
mumuki-domain-9.18.0 app/models/topic.rb
mumuki-domain-9.17.0 app/models/topic.rb
mumuki-domain-9.16.0 app/models/topic.rb
mumuki-domain-9.15.0 app/models/topic.rb
mumuki-domain-9.14.1 app/models/topic.rb
mumuki-domain-9.14.0 app/models/topic.rb
mumuki-domain-9.13.1 app/models/topic.rb
mumuki-domain-9.13.0 app/models/topic.rb
mumuki-domain-9.12.0 app/models/topic.rb
mumuki-domain-9.11.0 app/models/topic.rb
mumuki-domain-9.10.0 app/models/topic.rb
mumuki-domain-9.9.0 app/models/topic.rb
mumuki-domain-9.8.1 app/models/topic.rb
mumuki-domain-9.8.0 app/models/topic.rb