Class: Book

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

Instance Method Summary collapse

Methods inherited from Content

#fork_to!

Methods included from WithUsages

#usage_in_organization, #usage_in_organization_of_type

Methods included from WithSlug

#normalize_slug!, #rebase!, #rebased_dup, #transparent_id, #transparent_params

Methods included from WithDescription

#description_teaser

Methods included from Mumuki::Domain::Syncable

#platform_class_name, #sync_key

Methods inherited from ApplicationRecord

aggregate_of, all_except, defaults, #delete, #destroy!, numbered, organic_on, resource_fields, #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



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

def first_chapter
  chapters.first
end

#fork_children_into!(dup, organization, syncer) ⇒ Object

Forking



62
63
64
65
# File 'app/models/book.rb', line 62

def fork_children_into!(dup, organization, syncer)
  dup.chapters = chapters.map { |chapter| chapter.topic.fork_to!(organization, syncer, quiet: true).as_chapter_of(dup) }
  dup.complements = complements.map { |complement| complement.guide.fork_to!(organization, syncer, quiet: true).as_complement_of(dup) }
end

#import_from_resource_h!(resource_h) ⇒ Object



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

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



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

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

#next_lesson_for(user) ⇒ Object



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

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

#rebuild_complements!(complements) ⇒ Object

FIXME use rebuild



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

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



56
57
58
# File 'app/models/book.rb', line 56

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

#to_resource_hObject



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

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

#to_sObject



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

def to_s
  slug
end