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!, whitelist_attributes

Instance Method Details

#first_chapterObject



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

def first_chapter
  chapters.first
end

#import_from_json!(json) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'app/models/book.rb', line 25

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



44
45
46
# File 'app/models/book.rb', line 44

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

#next_lesson_for(user) ⇒ Object



21
22
23
# File 'app/models/book.rb', line 21

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

#rebuild_complements!(complements) ⇒ Object

FIXME use rebuild



35
36
37
38
39
40
41
42
# File 'app/models/book.rb', line 35

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



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

def to_s
  slug
end