Class: ContentModules::LessonModule

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

Overview

Container for didactic content.

Constant Summary collapse

SLIDESHOW_PROVIDER_TYPE =
BitCore::ContentProviders::SlideshowProvider.to_s

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sort(arm_id, lesson_ids) ⇒ Object



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

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



53
54
55
56
57
# File 'app/models/content_modules/lesson_module.rb', line 53

def build_slide(attrs = {})
  update_slideshow

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

#destroy_slide(slide) ⇒ Object



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

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



43
44
45
46
47
# File 'app/models/content_modules/lesson_module.rb', line 43

def lesson_provider
  @lesson_provider ||=
    (content_providers.find_by_type(SLIDESHOW_PROVIDER_TYPE) ||
    add_content_provider(SLIDESHOW_PROVIDER_TYPE))
end

#plain_titleObject



37
38
39
40
41
# File 'app/models/content_modules/lesson_module.rb', line 37

def plain_title
  return "" if title.nil?

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

#pretty_titleObject



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

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



49
50
51
# File 'app/models/content_modules/lesson_module.rb', line 49

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

#sort(slide_ids) ⇒ Object



76
77
78
79
80
# File 'app/models/content_modules/lesson_module.rb', line 76

def sort(slide_ids)
  update_slideshow

  lesson_provider.source_content.sort(slide_ids)
end