Class: Book
- Inherits:
-
Content
- Object
- ActiveRecord::Base
- ApplicationRecord
- Content
- Book
- Defined in:
- app/models/book.rb
Instance Method Summary collapse
-
#chapter_visibilities_in(workspace) ⇒ Object
experimental API - it may change in the future.
-
#enabled_chapters_in(workspace) ⇒ Object
progressive display.
- #first_chapter ⇒ Object
-
#fork_children_into!(dup, organization, syncer) ⇒ Object
Forking.
- #import_from_resource_h!(resource_h) ⇒ Object
- #index_usage!(organization) ⇒ Object
- #next_lesson_for(user) ⇒ Object
- #reindex_usages! ⇒ Object
- #structural_parent ⇒ Object
- #to_expanded_resource_h ⇒ Object
- #to_s ⇒ Object
Methods inherited from Content
#fork_to!, #public?, #to_resource_h
Methods included from WithProgress
#completed_for?, #completion_percentage_for, #dirty_progress_if_structural_children_changed!, #dirty_progresses!, #progress_for
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
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
#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
75 76 77 |
# File 'app/models/book.rb', line 75 def chapter_visibilities_in(workspace) chapters.zip(enabled_chapters_in(workspace)).map { |chapter, enabled| [chapter, !enabled.nil?] } end |
#enabled_chapters_in(workspace) ⇒ Object
progressive display
68 69 70 |
# File 'app/models/book.rb', line 68 def enabled_chapters_in(workspace) workspace.enabled_containers(chapters) end |
#first_chapter ⇒ Object
22 23 24 |
# File 'app/models/book.rb', line 22 def first_chapter chapters.first end |
#fork_children_into!(dup, organization, syncer) ⇒ Object
Forking
57 58 59 60 |
# File 'app/models/book.rb', line 57 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
30 31 32 33 34 35 36 37 38 |
# File 'app/models/book.rb', line 30 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
46 47 48 49 |
# File 'app/models/book.rb', line 46 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
26 27 28 |
# File 'app/models/book.rb', line 26 def next_lesson_for(user) user.try(:last_lesson) || first_lesson end |
#reindex_usages! ⇒ Object
51 52 53 |
# File 'app/models/book.rb', line 51 def reindex_usages! Organization.where(book: self).each &:reindex_usages! end |
#structural_parent ⇒ Object
62 63 64 |
# File 'app/models/book.rb', line 62 def structural_parent nil end |
#to_expanded_resource_h ⇒ Object
40 41 42 43 44 |
# File 'app/models/book.rb', line 40 def super.merge( chapters: chapters.map(&:slug), complements: complements.map(&:slug)) end |
#to_s ⇒ Object
18 19 20 |
# File 'app/models/book.rb', line 18 def to_s slug end |