Sha256: 61768cf4af2c375f5ee7432395d7492a2d30e367bb934a04c5189b744f127e68

Contents?: true

Size: 1.81 KB

Versions: 7

Compression:

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

  after_save :reindex_usages!

  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

  def first_lesson
    lessons.first
  end

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

  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

7 entries across 7 versions & 2 rubygems

Version Path
mumuki-laboratory-7.0.12 vendor/bundle/ruby/2.3.0/bundler/gems/mumuki-domain-74da3d4af98c/app/models/topic.rb
mumuki-domain-7.0.6 app/models/topic.rb
mumuki-domain-7.0.5 app/models/topic.rb
mumuki-domain-7.0.4 app/models/topic.rb
mumuki-domain-7.0.3 app/models/topic.rb
mumuki-laboratory-7.0.11 vendor/bundle/ruby/2.3.0/bundler/gems/mumuki-domain-f892f79c60f0/app/models/topic.rb
mumuki-domain-7.0.2 app/models/topic.rb