Sha256: 954e5acd17f6d0279908df640f748d174be7d9dde0f9f3ee255c169382eba5ae

Contents?: true

Size: 1.45 KB

Versions: 19

Compression:

Stored size: 1.45 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.submission_status = #{Mumuki::Laboratory::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_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

19 entries across 19 versions & 1 rubygems

Version Path
mumuki-laboratory-5.13.0 app/models/topic.rb
mumuki-laboratory-5.12.1 app/models/topic.rb
mumuki-laboratory-5.12.0 app/models/topic.rb
mumuki-laboratory-5.11.0 app/models/topic.rb
mumuki-laboratory-5.10.4 app/models/topic.rb
mumuki-laboratory-5.10.3 app/models/topic.rb
mumuki-laboratory-5.10.2 app/models/topic.rb
mumuki-laboratory-5.10.1 app/models/topic.rb
mumuki-laboratory-5.10.0 app/models/topic.rb
mumuki-laboratory-5.9.1 app/models/topic.rb
mumuki-laboratory-5.9.0 app/models/topic.rb
mumuki-laboratory-5.8.3 app/models/topic.rb
mumuki-laboratory-5.8.1 app/models/topic.rb
mumuki-laboratory-5.8.0 app/models/topic.rb
mumuki-laboratory-5.7.0 app/models/topic.rb
mumuki-laboratory-5.6.3 app/models/topic.rb
mumuki-laboratory-5.6.2 app/models/topic.rb
mumuki-laboratory-5.6.1 app/models/topic.rb
mumuki-laboratory-5.6.0 app/models/topic.rb