Class: ContentModules::LessonModule

Inherits:
BitCore::ContentModule
  • Object
show all
Defined in:
app/models/content_modules/lesson_module.rb

Overview

Container for didactic content.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sort(arm_id, lesson_ids) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/content_modules/lesson_module.rb', line 6

def self.sort(arm_id, lesson_ids)
  start_position = Arm.find(arm_id)
                   .bit_core_tools.find_by_type("Tools::Learn")
                   .content_modules
                   .where(type: [nil, "BitCore::ContentModule"])
                   .order(position: :asc).maximum(:position)
  start_position ||= 1
  start_position += 1

  transaction do
    connection.execute "SET CONSTRAINTS " \
                         "bit_core_content_module_position DEFERRED"
    lesson_ids.each_with_index do |id, idx|
      find(id).update_attribute(:position, idx + start_position)
    end
  end
end

Instance Method Details

#build_slide(attrs = {}) ⇒ Object



51
52
53
54
55
# File 'app/models/content_modules/lesson_module.rb', line 51

def build_slide(attrs = {})
  update_slideshow

  slides.build(attrs.merge(position: slides.count + 1))
end

#destroy_slide(slide) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/content_modules/lesson_module.rb', line 57

def destroy_slide(slide)
  position = slide.position

  slide.transaction do
    if slide.destroy!
      slides.where("position >= ?", position).each_with_index do |s, i|
        s.update_column(:position, position + i)
      end
    end
  end

  true

rescue
  false
end

#lesson_providerObject



41
42
43
44
45
# File 'app/models/content_modules/lesson_module.rb', line 41

def lesson_provider
  @lesson_provider ||= (content_providers.first ||
    add_content_provider("BitCore::ContentProviders::SlideshowProvider")
  )
end

#plain_titleObject



35
36
37
38
39
# File 'app/models/content_modules/lesson_module.rb', line 35

def plain_title
  return "" if title.nil?

  title.gsub(/\*\*/, "")
end

#pretty_titleObject



24
25
26
27
28
29
30
31
32
33
# File 'app/models/content_modules/lesson_module.rb', line 24

def pretty_title
  return "" if title.nil?

  Redcarpet::Markdown.new(
    Redcarpet::Render::HTML.new(
      filter_html: true,
      safe_links_only: true
    )
  ).render(title).html_safe
end

#slidesObject



47
48
49
# File 'app/models/content_modules/lesson_module.rb', line 47

def slides
  lesson_provider.try(:source_content).try(:slides) || []
end

#sort(slide_ids) ⇒ Object



74
75
76
77
78
# File 'app/models/content_modules/lesson_module.rb', line 74

def sort(slide_ids)
  update_slideshow

  lesson_provider.source_content.sort(slide_ids)
end