Sha256: 9fa475d2316a9bc79a58ceeee7a91e7c1e2dfef94966e227e26222715d1d7710

Contents?: true

Size: 1.18 KB

Versions: 20

Compression:

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

  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

20 entries across 20 versions & 1 rubygems

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