Sha256: 27bb948774306a6bf0a8eec0c9e53ba11b7b5392cdfae33efff2360dda901a08

Contents?: true

Size: 1.26 KB

Versions: 10

Compression:

Stored size: 1.26 KB

Contents

class Book < Content
  numbered :chapters
  aggregate_of :chapters

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

  has_many :exercises, through: :chapters
  has_many :discussions, through: :exercises

  delegate :first_lesson, to: :first_chapter

  def to_s
    slug
  end

  def first_chapter
    chapters.first
  end

  def next_lesson_for(user)
    user.try(:last_lesson)|| first_lesson
  end

  def import_from_json!(json)
    self.assign_attributes json.except('chapters', 'complements', 'id', 'description', 'teacher_info')
    self.description = json['description'].squeeze(' ')

    rebuild! json['chapters'].map { |it| Topic.find_by!(slug: it).as_chapter_of(self) }
    rebuild_complements!  (json['complements']||[]).map { |it| Guide.find_by!(slug: it).as_complement_of(self) }

    Organization.all.each { |org| org.reindex_usages! }
  end

  def rebuild_complements!(complements) #FIXME use rebuild
    transaction do
      self.complements.all_except(complements).delete_all
      self.update! :complements => complements
      complements.each &:save!
    end
    reload
  end

  def index_usage!(organization)
    [chapters, complements].flatten.each { |item| item.index_usage! organization }
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mumuki-laboratory-5.9.1 app/models/book.rb
mumuki-laboratory-5.9.0 app/models/book.rb
mumuki-laboratory-5.8.3 app/models/book.rb
mumuki-laboratory-5.8.1 app/models/book.rb
mumuki-laboratory-5.8.0 app/models/book.rb
mumuki-laboratory-5.7.0 app/models/book.rb
mumuki-laboratory-5.6.3 app/models/book.rb
mumuki-laboratory-5.6.2 app/models/book.rb
mumuki-laboratory-5.6.1 app/models/book.rb
mumuki-laboratory-5.6.0 app/models/book.rb