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



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

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



20
21
22
# File 'app/presenters/lcms/engine/document_presenter.rb', line 20

def color_code
  "#{subject}-base"
end

#color_code_gradeObject



24
25
26
# File 'app/presenters/lcms/engine/document_presenter.rb', line 24

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

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



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

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



36
37
38
# File 'app/presenters/lcms/engine/document_presenter.rb', line 36

def description
  .lesson_objective.presence || .description
end

#doc_typeObject



40
41
42
# File 'app/presenters/lcms/engine/document_presenter.rb', line 40

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

#ela2?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/presenters/lcms/engine/document_presenter.rb', line 44

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

#ela6?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/presenters/lcms/engine/document_presenter.rb', line 48

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

#fs_metadataObject



52
53
54
# File 'app/presenters/lcms/engine/document_presenter.rb', line 52

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

#full_breadcrumb(unit_level: false) ⇒ Object



56
57
58
# File 'app/presenters/lcms/engine/document_presenter.rb', line 56

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

#full_breadcrumb_from_metadata(unit_level) ⇒ Object



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

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



71
72
73
# File 'app/presenters/lcms/engine/document_presenter.rb', line 71

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

#ld_metadataObject



81
82
83
# File 'app/presenters/lcms/engine/document_presenter.rb', line 81

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

#ld_moduleObject



85
86
87
# File 'app/presenters/lcms/engine/document_presenter.rb', line 85

def ld_module
  ela? ? .module : .unit
end

#lessonObject



89
90
91
# File 'app/presenters/lcms/engine/document_presenter.rb', line 89

def lesson
  .lesson
end

#ll_strand?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/presenters/lcms/engine/document_presenter.rb', line 93

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

#math_practiceObject



97
98
99
# File 'app/presenters/lcms/engine/document_presenter.rb', line 97

def math_practice
  .lesson_mathematical_practice.squish
end

#pdf_filenameObject



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

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


111
112
113
# File 'app/presenters/lcms/engine/document_presenter.rb', line 111

def pdf_footer
  full_breadcrumb
end

#pdf_headerObject



101
102
103
# File 'app/presenters/lcms/engine/document_presenter.rb', line 101

def pdf_header
  "UnboundEd / #{full_breadcrumb}"
end

#remove_optional_break(content) ⇒ Object



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

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



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

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



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

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



154
155
156
# File 'app/presenters/lcms/engine/document_presenter.rb', line 154

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

#short_urlObject



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

def short_url
  @short_url ||= Bitly.client
                   .shorten(Rails.application.routes.url_helpers.document_url(self))
                   .short_url
end

#standardsObject



164
165
166
# File 'app/presenters/lcms/engine/document_presenter.rb', line 164

def standards
  .standard.presence || .lesson_standard
end

#student_materialsObject



168
169
170
# File 'app/presenters/lcms/engine/document_presenter.rb', line 168

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

#student_materials_propsObject



172
173
174
# File 'app/presenters/lcms/engine/document_presenter.rb', line 172

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

#subjectObject



176
177
178
# File 'app/presenters/lcms/engine/document_presenter.rb', line 176

def subject
  &.resource_subject
end

#subject_to_strObject



180
181
182
# File 'app/presenters/lcms/engine/document_presenter.rb', line 180

def subject_to_str
  SUBJECT_FULL[subject] || subject
end

#teacher_materialsObject



189
190
191
# File 'app/presenters/lcms/engine/document_presenter.rb', line 189

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

#teacher_materials_propsObject



193
194
195
# File 'app/presenters/lcms/engine/document_presenter.rb', line 193

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

#teaserObject



197
198
199
# File 'app/presenters/lcms/engine/document_presenter.rb', line 197

def teaser
  .teaser
end

#titleObject



184
185
186
187
# File 'app/presenters/lcms/engine/document_presenter.rb', line 184

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

#topicObject



201
202
203
# File 'app/presenters/lcms/engine/document_presenter.rb', line 201

def topic
  ela? ? .unit : .topic
end

#unitObject



205
206
207
# File 'app/presenters/lcms/engine/document_presenter.rb', line 205

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



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

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