Class: Guide

Inherits:
Content show all
Includes:
WithExpectations, WithLanguage, WithStats
Defined in:
app/models/guide.rb

Instance Method Summary collapse

Methods included from WithStats

#started?, #stats_for

Methods included from WithExpectations

#ensure_expectations_format, #expectations=, #expectations_yaml, #expectations_yaml=, #own_expectations

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

#as_complement_of(book) ⇒ Object

FIXME duplication



115
116
117
# File 'app/models/guide.rb', line 115

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



111
112
113
# File 'app/models/guide.rb', line 111

def as_lesson_of(topic)
  topic.lessons.find_by(guide_id: id) || Lesson.new(guide: self, topic: topic)
end

#chapterObject



29
30
31
# File 'app/models/guide.rb', line 29

def chapter
  lesson.try(:chapter) #FIXME temporary
end

#clear_progress!(user, organization = Organization.current) ⇒ Object



17
18
19
20
21
22
23
# File 'app/models/guide.rb', line 17

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

Returns:

  • (Boolean)


58
59
60
# File 'app/models/guide.rb', line 58

def done_for?(user)
  stats_for(user).done?
end

#exercises_countObject



33
34
35
# File 'app/models/guide.rb', line 33

def exercises_count
  exercises.count
end

#first_exerciseObject



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

def first_exercise
  exercises.first
end

#fork_children_into!(dup, _organization, _syncer) ⇒ Object

Forking



125
126
127
# File 'app/models/guide.rb', line 125

def fork_children_into!(dup, _organization, _syncer)
  dup.exercises = exercises.map(&:dup)
end

#import_from_resource_h!(resource_h) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/guide.rb', line 67

def import_from_resource_h!(resource_h)
  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

#lessonObject



25
26
27
# File 'app/models/guide.rb', line 25

def lesson
  usage_in_organization_of_type Lesson
end

#locate_exercise!(bibliotheca_id) ⇒ Object

Finds an exercise by bibliotheca_id within this guide



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

def locate_exercise!(bibliotheca_id)
  exercises.find_by!(bibliotheca_id: bibliotheca_id)
end

#next_exercise(user) ⇒ Object



46
47
48
# File 'app/models/guide.rb', line 46

def next_exercise(user)
  pending_exercises(user).order('public.exercises.number asc').first
end

#pending_exercises(user) ⇒ Object



37
38
39
40
41
42
43
44
# File 'app/models/guide.rb', line 37

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 = #{Mumuki::Domain::Status::Submission::Passed.to_i}").
      where('assignments.id is null')
end

#resettable?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/models/guide.rb', line 119

def resettable?
  usage_in_organization.resettable?
end

#search_tagsObject



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

def search_tags
  exercises.flat_map(&:search_tags).uniq
end

#to_markdownified_resource_hObject



100
101
102
103
104
105
106
107
108
109
# File 'app/models/guide.rb', line 100

def to_markdownified_resource_h
  to_resource_h.tap do |guide|
    %i(corollary description teacher_info).each do |it|
      guide[it] = Mumukit::ContentType::Markdown.to_html(guide[it])
    end
    %i(hint corollary description teacher_info).each do |it|
      guide[:exercises].each { |exercise| exercise[it] = Mumukit::ContentType::Markdown.to_html(exercise[it]) }
    end
  end
end

#to_resource_hObject

Keep this list up to date with Mumukit::Sync::Store::Github::Schema::Guide



91
92
93
94
95
96
97
98
# File 'app/models/guide.rb', line 91

def to_resource_h
  as_json(only: %i(beta type id_format private expectations corollary teacher_info sources learn_more authors collaborators extra settings))
    .symbolize_keys
    .merge(super)
    .merge(exercises: exercises.map(&:to_resource_h))
    .merge(language: language.to_embedded_resource_h)
    .compact
end