Sha256: 31f7d7b98c024ac3d9414820b5883e8e39fe2219992b1402b1226d067718fe65

Contents?: true

Size: 1.9 KB

Versions: 35

Compression:

Stored size: 1.9 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

  ## 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)
    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

  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

35 entries across 35 versions & 2 rubygems

Version Path
mumuki-domain-7.12.0 app/models/topic.rb
mumuki-domain-7.11.1 app/models/topic.rb
mumuki-laboratory-7.11.1 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-c92f2a7212e2/app/models/topic.rb
mumuki-laboratory-7.11.0 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-2e15c3330133/app/models/topic.rb
mumuki-domain-7.11.0 app/models/topic.rb
mumuki-domain-7.10.1 app/models/topic.rb
mumuki-domain-7.10.0 app/models/topic.rb
mumuki-laboratory-7.9.2 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-7082a83493eb/app/models/topic.rb
mumuki-domain-7.9.2 app/models/topic.rb
mumuki-laboratory-7.9.1 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-b162dc91aa77/app/models/topic.rb
mumuki-domain-7.9.1 app/models/topic.rb
mumuki-laboratory-7.9.0 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-520f859ca8af/app/models/topic.rb
mumuki-domain-7.9.0 app/models/topic.rb
mumuki-domain-7.7.3 app/models/topic.rb
mumuki-domain-7.8.1 app/models/topic.rb
mumuki-domain-7.8.0 app/models/topic.rb
mumuki-domain-7.7.2 app/models/topic.rb
mumuki-domain-7.7.1 app/models/topic.rb
mumuki-laboratory-7.7.1 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-cb7a9018bb94/app/models/topic.rb
mumuki-laboratory-7.7.0 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-cb7a9018bb94/app/models/topic.rb