Sha256: 1952e747b43c4e6eee1d26f446ba4271bdee98269217f328b5ed128ef16831d5

Contents?: true

Size: 1.43 KB

Versions: 20

Compression:

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

  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.status = #{Mumuki::Laboratory::Status::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_json!(json)
    self.assign_attributes json.except('lessons', 'id', 'description', 'teacher_info')
    self.description = json['description'].squeeze(' ')
    rebuild! json['lessons'].map { |it| lesson_for(it) }
    Organization.all.each { |org| org.reindex_usages! }
  end

  def as_chapter_of(book)
    book.chapters.find_by(topic_id: id) || Chapter.new(topic: self, book: book)
  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

20 entries across 20 versions & 1 rubygems

Version Path
mumuki-laboratory-5.5.0 app/models/topic.rb
mumuki-laboratory-5.4.0 app/models/topic.rb
mumuki-laboratory-5.3.0 app/models/topic.rb
mumuki-laboratory-5.2.1 app/models/topic.rb
mumuki-laboratory-5.2.0 app/models/topic.rb
mumuki-laboratory-5.1.1 app/models/topic.rb
mumuki-laboratory-5.1.0 app/models/topic.rb
mumuki-laboratory-5.0.12 app/models/topic.rb
mumuki-laboratory-5.0.11 app/models/topic.rb
mumuki-laboratory-5.0.10 app/models/topic.rb
mumuki-laboratory-5.0.9 app/models/topic.rb
mumuki-laboratory-5.0.8 app/models/topic.rb
mumuki-laboratory-5.0.7 app/models/topic.rb
mumuki-laboratory-5.0.6 app/models/topic.rb
mumuki-laboratory-5.0.5 app/models/topic.rb
mumuki-laboratory-5.0.4 app/models/topic.rb
mumuki-laboratory-5.0.3 app/models/topic.rb
mumuki-laboratory-5.0.2 app/models/topic.rb
mumuki-laboratory-5.0.1 app/models/topic.rb
mumuki-laboratory-5.0.0 app/models/topic.rb