Class: Govspeak::AttachmentPresenter

Inherits:
Object
  • Object
show all
Includes:
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.



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

def initialize(attachment)
  @attachment = attachment
end

Instance Attribute Details

#attachmentObject (readonly)

Returns the value of attribute attachment.



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

def attachment
  @attachment
end

Instance Method Details

#attachment_attributesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 35

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

#file_abbr_tag(abbr, title) ⇒ Object



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

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

#file_extensionObject



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

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

#humanized_content_type(file_extension) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 57

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



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

def id
  attachment[:id]
end


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

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

#titleObject



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

def title
  attachment[:title]
end

#urlObject



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

def url
  attachment[:url]
end