Class: Book

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

Instance Method Summary collapse

Methods inherited from Content

#fork_to!, #public?, #to_resource_h

Methods included from WithUsages

#content_used_in?, #navigable_content_in, #usage_in_organization, #usage_in_organization_of_type, #used_in?

Methods included from WithSlug

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

Methods included from WithProgress

#completed_for?, #completion_percentage_for, #dirty_progress_if_structural_children_changed!, #dirty_progresses!, #has_progress_for?, #once_completed_for?, #progress_for

Methods included from Mumuki::Domain::Syncable

#platform_class_name, #sync_key

Methods inherited from ApplicationRecord

active_between, aggregate_of, all_except, defaults, #delete, #destroy!, enum_prefixed_translations_for, numbered, organic_on, resource_fields, #save, #save_and_notify!, #save_and_notify_changes!, serialize_symbolized_hash_array, teaser_on, #update_and_notify!, update_or_create!, whitelist_attributes, with_pg_retry, with_temporary_token

Methods included from WithPgLock

#with_pg_lock

Instance Method Details

#chapter_visibilities_in(workspace) ⇒ Object

experimental API - it may change in the future. This method assumes no gaps in the sequences are introduced by enabled_chapters_in



77
78
79
# File 'app/models/book.rb', line 77

def chapter_visibilities_in(workspace)
  chapters.zip(enabled_chapters_in(workspace)).map { |chapter, enabled| [chapter, !enabled.nil?] }
end

#discussions_in_organization(organization = Organization.current) ⇒ Object



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

def discussions_in_organization(organization = Organization.current)
  Discussion.where(organization: organization, item: organization.exercises).includes(exercise: [:language, :guide])
end

#enabled_chapters_in(workspace) ⇒ Object

progressive display



70
71
72
# File 'app/models/book.rb', line 70

def enabled_chapters_in(workspace)
  workspace.enabled_containers(chapters)
end

#first_chapterObject



24
25
26
# File 'app/models/book.rb', line 24

def first_chapter
  chapters.first
end

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

Forking



59
60
61
62
# File 'app/models/book.rb', line 59

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



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

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

    rebuild_chapters! 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
end

#index_usage!(organization) ⇒ Object



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

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



28
29
30
# File 'app/models/book.rb', line 28

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

#reindex_usages!Object



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

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

#structural_parentObject



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

def structural_parent
  nil
end

#to_expanded_resource_hObject



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

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

#to_sObject



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

def to_s
  slug
end