Class: Govspeak::AttachmentPresenter

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::AssetTagHelper, ActionView::Helpers::NumberHelper, ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper
Defined in:
lib/govspeak/presenters/attachment_presenter.rb

Constant Summary collapse

MS_WORD_DOCUMENT_HUMANIZED_CONTENT_TYPE =
"MS Word Document".freeze
MS_EXCEL_SPREADSHEET_HUMANIZED_CONTENT_TYPE =
"MS Excel Spreadsheet".freeze
MS_POWERPOINT_PRESENTATION_HUMANIZED_CONTENT_TYPE =
"MS Powerpoint Presentation".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attachment) ⇒ AttachmentPresenter

Returns a new instance of AttachmentPresenter.



13
14
15
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 13

def initialize(attachment)
  @attachment = attachment
end

Instance Attribute Details

#attachmentObject (readonly)

Returns the value of attribute attachment.



7
8
9
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 7

def attachment
  @attachment
end

Instance Method Details

#accessible?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 42

def accessible?
  attachment[:accessible?]
end

#alternative_format_contact_emailObject



98
99
100
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 98

def alternative_format_contact_email
  "[email protected]"
end


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 65

def alternative_format_order_link
  attachment_info = []
  attachment_info << "  Title: #{title}"
  attachment_info << "  Original format: #{file_extension}" if file_extension.present?
  attachment_info << "  ISBN: #{attachment[:isbn]}" if attachment[:isbn].present?
  attachment_info << "  Unique reference: #{attachment[:unique_reference]}" if attachment[:unique_reference].present?
  attachment_info << "  Command paper number: #{attachment[:command_paper_number]}" if attachment[:command_paper_number].present?
  if attachment[:hoc_paper_number].present?
    attachment_info << "  House of Commons paper number: #{attachment[:hoc_paper_number]}"
    attachment_info << "  Parliamentary session: #{attachment[:parliamentary_session]}"
  end

  options = {
    subject: "Request for '#{title}' in an alternative format",
    body: body_for_mail(attachment_info)
  }

  mail_to(alternative_format_contact_email, alternative_format_contact_email, options)
end

#attachment_attributesObject



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 173

def attachment_attributes
  attributes = []
  if file_extension == "html"
    attributes << (:span, 'HTML', class: 'type')
  elsif attachment[:external?]
    attributes << (:span, url, class: 'url')
  else
    attributes << (:span, humanized_content_type(file_extension), class: 'type') if file_extension
    attributes << (:span, number_to_human_size(attachment[:file_size]), class: 'file-size') if attachment[:file_size]
    attributes << (:span, pluralize(attachment[:number_of_pages], "page"), class: 'page-length') if attachment[:number_of_pages]
  end
  attributes.join(', ').html_safe
end

#attachment_classObject



155
156
157
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 155

def attachment_class
  attachment[:external?] ? "hosted-externally" : "embedded"
end

#attachment_detailsObject



257
258
259
260
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 257

def attachment_details
  return if previewable?
  link(title, url, title_link_options)
end

#attachment_thumbnailObject

FIXME: usage of image_tag will cause these to render at /images/ which seems very host dependent. I assume this will need links to static urls.



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 104

def attachment_thumbnail
  if file_extension == "pdf" && attachment[:thumbnail_url]
    image_tag(attachment[:thumbnail_url])
  elsif file_extension == "html"
    image_tag('pub-cover-html.png')
  elsif %w{doc docx odt}.include?(file_extension)
    image_tag('pub-cover-doc.png')
  elsif %w{xls xlsx ods csv}.include?(file_extension)
    image_tag('pub-cover-spreadsheet.png')
  else
    image_tag('pub-cover.png')
  end
end

#body_for_mail(attachment_info) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 85

def body_for_mail(attachment_info)
  <<-END
Details of document required:

#{attachment_info.join("\n")}

Please tell us:

  1. What makes this format unsuitable for you?
  2. What format you would prefer?
END
end


167
168
169
170
171
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 167

def download_link
  options = {}
  options[:title] = number_to_human_size(attachment[:file_size]) if attachment[:file_size].present?
  link("<strong>Download #{file_extension.upcase}</strong>", attachment[:url], options)
end

#external?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 33

def external?
  attachment[:external?]
end

#file_abbr_tag(abbr, title) ⇒ Object



195
196
197
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 195

def file_abbr_tag(abbr, title)
  (:abbr, abbr, title: title)
end

#file_extensionObject



246
247
248
249
250
251
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 246

def file_extension
  # Note: this is a separate parameter rather than being calculated from the
  # filename because at the time of writing not all apps were using the effects
  # of this field.
  attachment[:file_extension]
end

#help_block_idObject



269
270
271
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 269

def help_block_id
  "attachment-#{id}-accessibility-help"
end

#help_block_toggle_idObject



52
53
54
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 52

def help_block_toggle_id
  "attachment-#{id}-accessibility-request"
end

#hide_thumbnail?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 253

def hide_thumbnail?
  defined?(hide_thumbnail) && hide_thumbnail
end

#humanized_content_type(file_extension) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 199

def humanized_content_type(file_extension)
  file_extension_vs_humanized_content_type = {
    "chm"  => file_abbr_tag('CHM', 'Microsoft Compiled HTML Help'),
    "csv"  => file_abbr_tag('CSV', 'Comma-separated Values'),
    "diff" => file_abbr_tag('DIFF', 'Plain text differences'),
    "doc"  => MS_WORD_DOCUMENT_HUMANIZED_CONTENT_TYPE,
    "docx" => MS_WORD_DOCUMENT_HUMANIZED_CONTENT_TYPE,
    "dot"  => file_abbr_tag('DOT', 'MS Word Document Template'),
    "dxf"  => file_abbr_tag('DXF', 'AutoCAD Drawing Exchange Format'),
    "eps"  => file_abbr_tag('EPS', 'Encapsulated PostScript'),
    "gif"  => file_abbr_tag('GIF', 'Graphics Interchange Format'),
    "gml"  => file_abbr_tag('GML', 'Geography Markup Language'),
    "html" => file_abbr_tag('HTML', 'Hypertext Markup Language'),
    "ics" => file_abbr_tag('ICS', 'iCalendar file'),
    "jpg"  => "JPEG",
    "odp"  => file_abbr_tag('ODP', 'OpenDocument Presentation'),
    "ods"  => file_abbr_tag('ODS', 'OpenDocument Spreadsheet'),
    "odt"  => file_abbr_tag('ODT', 'OpenDocument Text document'),
    "pdf"  => file_abbr_tag('PDF', 'Portable Document Format'),
    "png"  => file_abbr_tag('PNG', 'Portable Network Graphic'),
    "ppt"  => MS_POWERPOINT_PRESENTATION_HUMANIZED_CONTENT_TYPE,
    "pptx" => MS_POWERPOINT_PRESENTATION_HUMANIZED_CONTENT_TYPE,
    "ps"   => file_abbr_tag('PS', 'PostScript'),
    "rdf"  => file_abbr_tag('RDF', 'Resource Description Framework'),
    "rtf"  => file_abbr_tag('RTF', 'Rich Text Format'),
    "sch"  => file_abbr_tag('SCH', 'XML based Schematic'),
    "txt"  => "Plain text",
    "wsdl" => file_abbr_tag('WSDL', 'Web Services Description Language'),
    "xls"  => MS_EXCEL_SPREADSHEET_HUMANIZED_CONTENT_TYPE,
    "xlsm" => file_abbr_tag('XLSM', 'MS Excel Macro-Enabled Workbook'),
    "xlsx" => MS_EXCEL_SPREADSHEET_HUMANIZED_CONTENT_TYPE,
    "xlt"  => file_abbr_tag('XLT', 'MS Excel Spreadsheet Template'),
    "xsd"  => file_abbr_tag('XSD', 'XML Schema'),
    "xslt" => file_abbr_tag('XSLT', 'Extensible Stylesheet Language Transformation'),
    "zip"  => file_abbr_tag('ZIP', 'Zip archive'),
  }
  file_extension_vs_humanized_content_type.fetch(file_extension.to_s.downcase, '')
end

#idObject



17
18
19
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 17

def id
  attachment[:id]
end


273
274
275
276
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 273

def link(body, url, options = {})
  options_str = options.map { |k, v| %{#{encode(k)}="#{encode(v)}"} }.join(" ")
  %{<a href="#{encode(url)}" #{options_str}>#{body}</a>}
end

#mail_to(email_address, name, options = {}) ⇒ Object



60
61
62
63
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 60

def mail_to(email_address, name, options = {})
  query_string = options.slice(:subject, :body).map { |k, v| "#{urlencode(k)}=#{urlencode(v)}" }.join("&")
  "<a href='mailto:#{encode(email_address)}?#{encode(query_string)}'>#{name}</a>"
end

#opendocument?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 25

def opendocument?
  attachment[:opendocument?]
end

#order_urlObject



21
22
23
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 21

def order_url
  attachment[:order_url]
end

#preview_urlObject



187
188
189
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 187

def preview_url
  url + '/preview'
end

#previewable?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 238

def previewable?
  file_extension == "csv"
end

#priceObject



37
38
39
40
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 37

def price
  return unless attachment[:price]
  Money.from_amount(attachment[:price], 'GBP').format
end

#referenceObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 118

def reference
  ref = []
  if attachment[:isbn].present?
    ref << "ISBN " + (:span, attachment[:isbn], class: "isbn")
  end

  if attachment[:unique_reference].present?
    ref << (:span, attachment[:unique_reference], class: "unique_reference")
  end

  if attachment[:command_paper_number].present?
    ref << (:span, attachment[:command_paper_number], class: "command_paper_number")
  end

  if attachment[:hoc_paper_number].present?
    ref << (:span, "HC #{attachment[:hoc_paper_number]}", class: 'house_of_commons_paper_number') + ' ' +
      (:span, attachment[:parliamentary_session], class: 'parliamentary_session')
  end

  ref.join(', ').html_safe
end

#references?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 151

def references?
  !attachment[:isbn].to_s.empty? || !attachment[:unique_reference].to_s.empty? || !attachment[:command_paper_number].to_s.empty? || !attachment[:hoc_paper_number].to_s.empty?
end

#references_for_titleObject

FIXME this has english in it so will cause problems if the locale is not en



141
142
143
144
145
146
147
148
149
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 141

def references_for_title
  references = []
  references << "ISBN: #{attachment[:isbn]}" if attachment[:isbn].present?
  references << "Unique reference: #{attachment[:unique_reference]}" if attachment[:unique_reference].present?
  references << "Command paper number: #{attachment[:command_paper_number]}" if attachment[:command_paper_number].present?
  references << "HC: #{attachment[:hoc_paper_number]} #{attachment[:parliamentary_session]}" if attachment[:hoc_paper_number].present?
  prefix = references.size == 1 ? "and its reference" : "and its references"
  references.any? ? ", #{prefix} (" + references.join(", ") + ")" : ""
end

#section_classObject



56
57
58
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 56

def section_class
  attachment[:external?] ? "hosted-externally" : "embedded"
end


46
47
48
49
50
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 46

def thumbnail_link
  return if hide_thumbnail?
  return if previewable?
  link(attachment_thumbnail, url, "aria-hidden" => "true", "class" => attachment_class)
end

#titleObject



242
243
244
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 242

def title
  attachment[:title]
end


262
263
264
265
266
267
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 262

def title_link_options
  title_link_options = {}
  title_link_options["rel"] = "external" if attachment[:external?]
  title_link_options["aria-describedby"] = help_block_id unless attachment[:accessible?]
  title_link_options
end

#unnumbered_command_paper?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 163

def unnumbered_command_paper?
  attachment[:unnumbered_command_paper?]
end

#unnumbered_paper?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 159

def unnumbered_paper?
  attachment[:unnumbered_command_paper?] || attachment[:unnumbered_hoc_paper?]
end

#urlObject



29
30
31
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 29

def url
  attachment[:url]
end