Class: Book

Inherits:
Content show all
Defined in:
app/models/book.rb

Instance Method Summary collapse

Methods included from WithUsages

#usage_in_organization, #usage_in_organization_of_type

Methods included from WithSlug

#import!, #slug_parts

Methods included from WithDescription

#description_teaser

Methods inherited from ApplicationRecord

aggregate_of, all_except, defaults, name_model_as, numbered, #save, #save_and_notify!, #save_and_notify_changes!, #update_and_notify!, update_or_create!

Instance Method Details

#first_chapterObject



14
15
16
# File 'app/models/book.rb', line 14

def first_chapter
  chapters.first
end

#import_from_json!(json) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'app/models/book.rb', line 22

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

#index_usage!(organization) ⇒ Object



41
42
43
# File 'app/models/book.rb', line 41

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

#next_lesson_for(user) ⇒ Object



18
19
20
# File 'app/models/book.rb', line 18

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

#rebuild_complements!(complements) ⇒ Object

FIXME use rebuild



32
33
34
35
36
37
38
39
# File 'app/models/book.rb', line 32

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

#to_sObject



10
11
12
# File 'app/models/book.rb', line 10

def to_s
  slug
end