Class: Lcms::Engine::DocumentPresenter

Inherits:
ContentPresenter show all
Defined in:
app/presenters/lcms/engine/document_presenter.rb

Constant Summary collapse

PDF_SUBTITLES =
{ full: '', sm: '_student_materials', tm: '_teacher_materials' }.freeze
SUBJECT_FULL =
{ 'ela' => 'ELA', 'math' => 'Math' }.freeze
TOC_RESOURCES =
[I18n.t('document.toc.tm'), I18n.t('document.toc.sm'), I18n.t('document.toc.credits')].freeze
TOPIC_FULL =
{ 'ela' => 'Unit', 'math' => 'Topic' }.freeze
TOPIC_SHORT =
{ 'ela' => 'U', 'math' => 'T' }.freeze

Constants inherited from ContentPresenter

ContentPresenter::CONFIG_PATH, ContentPresenter::DEFAULT_CONFIG, ContentPresenter::MATERIALS_CONFIG_PATH, ContentPresenter::PDF_EXT, ContentPresenter::THUMB_EXT

Instance Method Summary collapse

Methods inherited from ContentPresenter

base_config, #base_filename, #config, #content_type, #footer_margin_styles, #gdoc_folder, #gdoc_key, #initialize, materials_config, #materials_config_for, #orientation, #padding_styles

Methods inherited from BasePresenter

#initialize, #t

Constructor Details

This class inherits a constructor from Lcms::Engine::ContentPresenter

Instance Method Details

#cc_attributionObject



14
15
16
17
18
19
20
# File 'app/presenters/lcms/engine/document_presenter.rb', line 14

def cc_attribution
  core_cc = .cc_attribution
  return core_cc if (fs_cc = .cc_attribution).blank?
  return core_cc if core_cc.casecmp(fs_cc).zero?

  "#{core_cc} #{fs_cc}"
end

#color_codeObject



22
23
24
# File 'app/presenters/lcms/engine/document_presenter.rb', line 22

def color_code
  "#{subject}-base"
end

#color_code_gradeObject



26
27
28
# File 'app/presenters/lcms/engine/document_presenter.rb', line 26

def color_code_grade
  "#{subject}-#{grade}"
end

#content_for(context_type, options = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'app/presenters/lcms/engine/document_presenter.rb', line 30

def content_for(context_type, options = {})
  with_excludes = (options[:excludes] || []).any?
  content = render_content(context_type, options)
  content = update_activity_timing(content) if with_excludes
  content = remove_optional_break(content) if ela? && with_excludes
  content
end

#descriptionObject



38
39
40
# File 'app/presenters/lcms/engine/document_presenter.rb', line 38

def description
  .lesson_objective.presence || .description
end

#doc_typeObject



42
43
44
# File 'app/presenters/lcms/engine/document_presenter.rb', line 42

def doc_type
  assessment? ? 'assessment' : 'lesson'
end

#ela2?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/presenters/lcms/engine/document_presenter.rb', line 46

def ela2?
  ela? && grade.to_s == '2'
end

#ela6?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/presenters/lcms/engine/document_presenter.rb', line 50

def ela6?
  ela? && grade.to_s == '6'
end

#fs_metadataObject



54
55
56
# File 'app/presenters/lcms/engine/document_presenter.rb', line 54

def 
  @fs_metadata ||= DocTemplate::Objects::BaseMetadata.build_from()
end

#full_breadcrumb(unit_level: false) ⇒ Object



58
59
60
# File 'app/presenters/lcms/engine/document_presenter.rb', line 58

def full_breadcrumb(unit_level: false)
  resource ? Breadcrumbs.new(resource).full_title : (unit_level)
end

#full_breadcrumb_from_metadata(unit_level) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'app/presenters/lcms/engine/document_presenter.rb', line 62

def (unit_level)
  lesson_level = assessment? ? 'Assessment' : "Lesson #{lesson}" unless unit_level
  [
    SUBJECT_FULL[subject] || subject,
    grade.to_i.zero? ? grade : "Grade #{grade}",
    ll_strand? ? ld_module : "Module #{ld_module.try(:upcase)}",
    topic.present? ? "#{TOPIC_FULL[subject]} #{topic.try(:upcase)}" : nil,
    lesson_level
  ].compact.join(' / ')
end

#gradeObject



73
74
75
# File 'app/presenters/lcms/engine/document_presenter.rb', line 73

def grade
  .grade[/\d+/] || .grade
end

#ld_metadataObject



83
84
85
# File 'app/presenters/lcms/engine/document_presenter.rb', line 83

def 
  @ld_metadata ||= DocTemplate::Objects::BaseMetadata.build_from()
end

#ld_moduleObject



87
88
89
# File 'app/presenters/lcms/engine/document_presenter.rb', line 87

def ld_module
  ela? ? .module : .unit
end

#lessonObject



91
92
93
# File 'app/presenters/lcms/engine/document_presenter.rb', line 91

def lesson
  .lesson
end

#ll_strand?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/presenters/lcms/engine/document_presenter.rb', line 95

def ll_strand?
  .module =~ /strand/i
end

#math_practiceObject



99
100
101
# File 'app/presenters/lcms/engine/document_presenter.rb', line 99

def math_practice
  .lesson_mathematical_practice.squish
end

#pdf_filenameObject



107
108
109
110
111
# File 'app/presenters/lcms/engine/document_presenter.rb', line 107

def pdf_filename
  name = short_breadcrumb(join_with: '_', with_short_lesson: true)
  name += PDF_SUBTITLES[content_type.to_sym]
  "#{name}_v#{version.presence || 1}#{Lcms::Engine::ContentPresenter::PDF_EXT}"
end


113
114
115
# File 'app/presenters/lcms/engine/document_presenter.rb', line 113

def pdf_footer
  full_breadcrumb
end

#pdf_headerObject



103
104
105
# File 'app/presenters/lcms/engine/document_presenter.rb', line 103

def pdf_header
  "UnboundEd / #{full_breadcrumb}"
end

#remove_optional_break(content) ⇒ Object



77
78
79
80
81
# File 'app/presenters/lcms/engine/document_presenter.rb', line 77

def remove_optional_break(content)
  html = Nokogiri::HTML.fragment content
  html.at_css('.o-ld-optbreak-wrapper')&.remove
  html.to_html
end

#render_content(context_type, options = {}) ⇒ Object



129
130
131
132
133
134
# File 'app/presenters/lcms/engine/document_presenter.rb', line 129

def render_content(context_type, options = {})
  options[:parts_index] = document_parts_index
  rendered_layout = DocumentRenderer::Part.call(layout_content(context_type), options)
  content = DocTemplate.sanitizer.clean_content(rendered_layout, context_type)
  Lcms::Engine::ReactMaterialsResolver.resolve(content, self)
end

#short_breadcrumb(join_with: ' / ', with_short_lesson: false, with_subject: true, unit_level: false) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/presenters/lcms/engine/document_presenter.rb', line 137

def short_breadcrumb(join_with: ' / ', with_short_lesson: false, with_subject: true, unit_level: false)
  unless unit_level
    lesson_abbr =
      if assessment?
        with_short_lesson ? 'A' : 'Assessment'
      else
        with_short_lesson ? "L#{lesson}" : "Lesson #{lesson}"
      end
  end
  [
    with_subject ? SUBJECT_FULL[subject] || subject : nil,
    grade.to_i.zero? ? grade : "G#{grade}",
    ll_strand? ? 'LL' : "M#{ld_module.try(:upcase)}",
    topic.present? ? "#{TOPIC_SHORT[subject]}#{topic.try(:upcase)}" : nil,
    lesson_abbr
  ].compact.join(join_with)
end

#short_titleObject

rubocop:enable Metrics/PerceivedComplexity



156
157
158
# File 'app/presenters/lcms/engine/document_presenter.rb', line 156

def short_title
  assessment? ? doc_type : "Lesson #{lesson}"
end

#short_urlObject



160
161
162
163
164
# File 'app/presenters/lcms/engine/document_presenter.rb', line 160

def short_url
  @short_url ||= Bitly.client
                   .shorten(document_url(self))
                   .short_url
end

#standardsObject



166
167
168
# File 'app/presenters/lcms/engine/document_presenter.rb', line 166

def standards
  .standard.presence || .lesson_standard
end

#student_materialsObject



170
171
172
# File 'app/presenters/lcms/engine/document_presenter.rb', line 170

def student_materials
  materials.gdoc.(materials_config_for(:student))
end

#student_materials_propsObject



174
175
176
# File 'app/presenters/lcms/engine/document_presenter.rb', line 174

def student_materials_props
  Lcms::Engine::DocumentMaterialSerializer.new(self, student_materials)
end

#subjectObject



178
179
180
# File 'app/presenters/lcms/engine/document_presenter.rb', line 178

def subject
  &.resource_subject
end

#subject_to_strObject



182
183
184
# File 'app/presenters/lcms/engine/document_presenter.rb', line 182

def subject_to_str
  SUBJECT_FULL[subject] || subject
end

#teacher_materialsObject



191
192
193
# File 'app/presenters/lcms/engine/document_presenter.rb', line 191

def teacher_materials
  materials.gdoc.(materials_config_for(:teacher))
end

#teacher_materials_propsObject



195
196
197
# File 'app/presenters/lcms/engine/document_presenter.rb', line 195

def teacher_materials_props
  Lcms::Engine::DocumentMaterialSerializer.new(self, teacher_materials)
end

#teaserObject



199
200
201
# File 'app/presenters/lcms/engine/document_presenter.rb', line 199

def teaser
  .teaser
end

#titleObject



186
187
188
189
# File 'app/presenters/lcms/engine/document_presenter.rb', line 186

def title
  title = &.title
  resource&.prerequisite? ? "Prerequisite -  #{title}" : title
end

#topicObject



203
204
205
# File 'app/presenters/lcms/engine/document_presenter.rb', line 203

def topic
  ela? ? .unit : .topic
end

#unitObject



207
208
209
# File 'app/presenters/lcms/engine/document_presenter.rb', line 207

def unit
  @unit ||= resource&.parent
end

#update_activity_timing(content) ⇒ Object

Makes sure that time of group is equal to sum of timings of child activities



120
121
122
123
124
125
126
127
# File 'app/presenters/lcms/engine/document_presenter.rb', line 120

def update_activity_timing(content)
  html = Nokogiri::HTML.fragment content
  html.css('.o-ld-group').each do |group|
    group_time = group.css('.o-ld-title__time--h3').inject(0) { |time, section| time + section.text.to_i }
    group.at_css('.o-ld-title__time--h2').content = group_time.zero? ? "\u2014" : "#{group_time} mins"
  end
  html.to_html
end