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

#rebase!, #rebased_dup, #slug_parts

Methods included from WithDescription

#description_teaser

Methods included from Syncable

#platform_class_name, #sync_key

Methods inherited from ApplicationRecord

aggregate_of, all_except, defaults, numbered, #save, #save_and_notify!, #save_and_notify_changes!, serialize_symbolized_hash_array, #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

#fork_to!(organization, syncer) ⇒ Object

Forking



60
61
62
63
64
65
66
67
# File 'app/models/book.rb', line 60

def fork_to!(organization, syncer)
  rebased_dup(organization).tap do |dup|
    dup.chapters = chapters.map { |chapter| chapter.topic.fork_to!(organization, syncer).as_chapter_of(self) }
    dup.complements = complements.map { |complement| complement.guide.fork_to!(organization, syncer).as_complement_of(self) }
    dup.save!
    syncer.export! dup
  end
end

#import_from_resource_h!(resource_h) ⇒ Object



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

def import_from_resource_h!(resource_h)
  self.assign_attributes resource_h.except(:chapters, :complements, :id, :description)
  self.description = resource_h[:description]&.squeeze(' ')

  rebuild! resource_h[:chapters].map { |it| Topic.find_by!(slug: it).as_chapter_of(self) }
  rebuild_complements! resource_h[:complements].to_a.map { |it| Guide.find_by(slug: it)&.as_complement_of(self) }.compact
end

#index_usage!(organization) ⇒ Object



50
51
52
# File 'app/models/book.rb', line 50

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



41
42
43
44
45
46
47
48
# File 'app/models/book.rb', line 41

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

#reindex_usages!Object



54
55
56
# File 'app/models/book.rb', line 54

def reindex_usages!
  Organization.where(book: self).each &:reindex_usages!
end

#to_resource_hObject



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

def to_resource_h
  super.merge(
    chapters: chapters.map(&:slug),
    complements: complements.map(&:slug))
end

#to_sObject



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

def to_s
  slug
end