Class: Guide
- Inherits:
-
Content
- Object
- ActiveRecord::Base
- ApplicationRecord
- Content
- Guide
- Includes:
- WithExpectations, WithLanguage, WithStats
- Defined in:
- app/models/guide.rb
Constant Summary collapse
- BASIC_RESOURCE_FIELDS =
%i( authors beta collaborators corollary custom_expectations expectations extra id_format learn_more private settings sources teacher_info type)
Instance Method Summary collapse
-
#as_complement_of(book) ⇒ Object
FIXME duplication.
- #as_lesson_of(topic) ⇒ Object
- #chapter ⇒ Object
- #clear_progress!(user, organization = Organization.current) ⇒ Object
- #done_for?(user) ⇒ Boolean
- #exercises_count ⇒ Object
- #first_exercise ⇒ Object
-
#fork_children_into!(dup, _organization, _syncer) ⇒ Object
Forking.
- #import_from_resource_h!(resource_h) ⇒ Object
- #lesson ⇒ Object
-
#locate_exercise!(bibliotheca_id) ⇒ Object
Finds an exercise by bibliotheca_id within this guide.
- #next_exercise(user) ⇒ Object
-
#pending_exercises(user) ⇒ Object
TODO: Make use of pending_siblings logic.
- #resettable? ⇒ Boolean
- #search_tags ⇒ Object
-
#to_expanded_resource_h(options = {}) ⇒ Object
Keep this list up to date with Mumuki::Domain::Store::Github::GuideSchema.
- #to_markdownified_resource_h ⇒ Object
Methods included from WithStats
Methods included from WithExpectations
#ensure_expectations_format, #expectations=, #expectations_yaml, #expectations_yaml=, #raw_expectations
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!, #once_completed_for?, #progress_for
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, teaser_on, #update_and_notify!, update_or_create!, whitelist_attributes
Instance Method Details
#as_complement_of(book) ⇒ Object
FIXME duplication
121 122 123 |
# File 'app/models/guide.rb', line 121 def as_complement_of(book) #FIXME duplication book.complements.find_by(guide_id: id) || Complement.new(guide: self, book: book) end |
#as_lesson_of(topic) ⇒ Object
117 118 119 |
# File 'app/models/guide.rb', line 117 def as_lesson_of(topic) topic.lessons.find_by(guide_id: id) || Lesson.new(guide: self, topic: topic) end |
#chapter ⇒ Object
36 37 38 |
# File 'app/models/guide.rb', line 36 def chapter lesson.try(:chapter) #FIXME temporary end |
#clear_progress!(user, organization = Organization.current) ⇒ Object
24 25 26 27 28 29 30 |
# File 'app/models/guide.rb', line 24 def clear_progress!(user, organization=Organization.current) transaction do exercises.each do |exercise| exercise.find_assignment_for(user, organization)&.destroy! end end end |
#done_for?(user) ⇒ Boolean
69 70 71 |
# File 'app/models/guide.rb', line 69 def done_for?(user) stats_for(user).done? end |
#exercises_count ⇒ Object
40 41 42 |
# File 'app/models/guide.rb', line 40 def exercises_count exercises.count end |
#first_exercise ⇒ Object
61 62 63 |
# File 'app/models/guide.rb', line 61 def first_exercise exercises.first end |
#fork_children_into!(dup, _organization, _syncer) ⇒ Object
Forking
131 132 133 |
# File 'app/models/guide.rb', line 131 def fork_children_into!(dup, _organization, _syncer) dup.exercises = exercises.map(&:dup) end |
#import_from_resource_h!(resource_h) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/guide.rb', line 78 def import_from_resource_h!(resource_h) dirty_progress_if_structural_children_changed! do self.assign_attributes whitelist_attributes(resource_h) self.language = Language.for_name(resource_h.dig(:language, :name)) self.save! resource_h[:exercises]&.each_with_index do |e, i| exercise = Exercise.find_by(guide_id: self.id, bibliotheca_id: e[:id]) exercise_type = e[:type] || 'problem' exercise = exercise ? exercise.ensure_type!(exercise_type.as_module_name) : exercise_type.as_module.new(guide_id: self.id, bibliotheca_id: e[:id]) exercise.import_from_resource_h! (i+1), e end new_ids = resource_h[:exercises].map { |it| it[:id] } self.exercises.where.not(bibliotheca_id: new_ids).destroy_all reload end end |
#lesson ⇒ Object
32 33 34 |
# File 'app/models/guide.rb', line 32 def lesson usage_in_organization_of_type Lesson end |
#locate_exercise!(bibliotheca_id) ⇒ Object
Finds an exercise by bibliotheca_id within this guide
74 75 76 |
# File 'app/models/guide.rb', line 74 def locate_exercise!(bibliotheca_id) exercises.find_by!(bibliotheca_id: bibliotheca_id) end |
#next_exercise(user) ⇒ Object
44 45 46 |
# File 'app/models/guide.rb', line 44 def next_exercise(user) user.next_exercise_at(self) end |
#pending_exercises(user) ⇒ Object
TODO: Make use of pending_siblings logic
49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/guide.rb', line 49 def pending_exercises(user) exercises. joins("left join public.assignments assignments on assignments.exercise_id = exercises.id and assignments.submitter_id = #{user.id} and assignments.submission_status in ( #{Mumuki::Domain::Status::Submission::Passed.to_i}, #{Mumuki::Domain::Status::Submission::ManualEvaluationPending.to_i} )"). where('assignments.id is null') end |
#resettable? ⇒ Boolean
125 126 127 |
# File 'app/models/guide.rb', line 125 def resettable? usage_in_organization.resettable? end |
#search_tags ⇒ Object
65 66 67 |
# File 'app/models/guide.rb', line 65 def exercises.flat_map(&:search_tags).uniq end |
#to_expanded_resource_h(options = {}) ⇒ Object
Keep this list up to date with Mumuki::Domain::Store::Github::GuideSchema
104 105 106 107 108 109 110 111 |
# File 'app/models/guide.rb', line 104 def (={}) as_json(only: BASIC_RESOURCE_FIELDS) .symbolize_keys .merge(super) .merge(exercises: exercises.map { |it| it.to_resource_h() }) .merge(language: language.) .tap { |it| it.markdownified!(:corollary, :description, :teacher_info) if [:markdownified] } end |
#to_markdownified_resource_h ⇒ Object
113 114 115 |
# File 'app/models/guide.rb', line 113 def to_markdownified_resource_h to_resource_h(markdownified: true) end |