Class: Govspeak::AttachmentPresenter
- Inherits:
-
Object
- Object
- Govspeak::AttachmentPresenter
- 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
-
#attachment ⇒ Object
readonly
Returns the value of attribute attachment.
Instance Method Summary collapse
- #accessible? ⇒ Boolean
- #alternative_format_contact_email ⇒ Object
- #alternative_format_order_link ⇒ Object
- #attachment_attributes ⇒ Object
- #attachment_class ⇒ Object
- #attachment_details ⇒ Object
-
#attachment_thumbnail ⇒ Object
FIXME: usage of image_tag will cause these to render at /images/ which seems very host dependent.
- #body_for_mail(attachment_info) ⇒ Object
- #download_link ⇒ Object
- #external? ⇒ Boolean
- #file_abbr_tag(abbr, title) ⇒ Object
- #file_extension ⇒ Object
- #help_block_id ⇒ Object
- #help_block_toggle_id ⇒ Object
- #hide_thumbnail? ⇒ Boolean
- #humanized_content_type(file_extension) ⇒ Object
- #id ⇒ Object
-
#initialize(attachment) ⇒ AttachmentPresenter
constructor
A new instance of AttachmentPresenter.
- #link(body, url, options = {}) ⇒ Object
- #mail_to(email_address, name, options = {}) ⇒ Object
- #opendocument? ⇒ Boolean
- #order_url ⇒ Object
- #preview_url ⇒ Object
- #previewable? ⇒ Boolean
- #price ⇒ Object
- #reference ⇒ Object
- #references? ⇒ Boolean
-
#references_for_title ⇒ Object
FIXME this has english in it so will cause problems if the locale is not en.
- #section_class ⇒ Object
- #thumbnail_link ⇒ Object
- #title ⇒ Object
- #title_link_options ⇒ Object
- #unnumbered_command_paper? ⇒ Boolean
- #unnumbered_paper? ⇒ Boolean
- #url ⇒ Object
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 = end |
Instance Attribute Details
#attachment ⇒ Object (readonly)
Returns the value of attribute attachment.
7 8 9 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 7 def @attachment end |
Instance Method Details
#accessible? ⇒ Boolean
42 43 44 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 42 def accessible? [:accessible?] end |
#alternative_format_contact_email ⇒ Object
98 99 100 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 98 def alternative_format_contact_email "[email protected]" end |
#alternative_format_order_link ⇒ Object
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 = [] << " Title: #{title}" << " Original format: #{file_extension}" if file_extension.present? << " ISBN: #{[:isbn]}" if [:isbn].present? << " Unique reference: #{[:unique_reference]}" if [:unique_reference].present? << " Command paper number: #{[:command_paper_number]}" if [:command_paper_number].present? if [:hoc_paper_number].present? << " House of Commons paper number: #{[:hoc_paper_number]}" << " Parliamentary session: #{[:parliamentary_session]}" end = { subject: "Request for '#{title}' in an alternative format", body: body_for_mail() } mail_to(alternative_format_contact_email, alternative_format_contact_email, ) end |
#attachment_attributes ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 173 def attributes = [] if file_extension == "html" attributes << content_tag(:span, 'HTML', class: 'type') elsif [:external?] attributes << content_tag(:span, url, class: 'url') else attributes << content_tag(:span, humanized_content_type(file_extension), class: 'type') if file_extension attributes << content_tag(:span, number_to_human_size([:file_size]), class: 'file-size') if [:file_size] attributes << content_tag(:span, pluralize([:number_of_pages], "page"), class: 'page-length') if [:number_of_pages] end attributes.join(', ').html_safe end |
#attachment_class ⇒ Object
155 156 157 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 155 def [:external?] ? "hosted-externally" : "embedded" end |
#attachment_details ⇒ Object
257 258 259 260 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 257 def return if previewable? link(title, url, ) end |
#attachment_thumbnail ⇒ Object
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 if file_extension == "pdf" && [:thumbnail_url] image_tag([: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() <<-END Details of document required: #{.join("\n")} Please tell us: 1. What makes this format unsuitable for you? 2. What format you would prefer? END end |
#download_link ⇒ Object
167 168 169 170 171 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 167 def download_link = {} [:title] = number_to_human_size([:file_size]) if [:file_size].present? link("<strong>Download #{file_extension.upcase}</strong>", [:url], ) end |
#external? ⇒ Boolean
33 34 35 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 33 def external? [: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) content_tag(:abbr, abbr, title: title) end |
#file_extension ⇒ Object
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. [:file_extension] end |
#help_block_id ⇒ Object
269 270 271 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 269 def help_block_id "attachment-#{id}-accessibility-help" end |
#help_block_toggle_id ⇒ Object
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
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 |
#id ⇒ Object
17 18 19 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 17 def id [:id] end |
#link(body, url, options = {}) ⇒ Object
273 274 275 276 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 273 def link(body, url, = {}) = .map { |k, v| %{#{encode(k)}="#{encode(v)}"} }.join(" ") %{<a href="#{encode(url)}" #{}>#{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, = {}) query_string = .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
25 26 27 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 25 def opendocument? [:opendocument?] end |
#order_url ⇒ Object
21 22 23 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 21 def order_url [:order_url] end |
#preview_url ⇒ Object
187 188 189 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 187 def preview_url url + '/preview' end |
#previewable? ⇒ Boolean
238 239 240 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 238 def previewable? file_extension == "csv" end |
#price ⇒ Object
37 38 39 40 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 37 def price return unless [:price] Money.from_amount([:price], 'GBP').format end |
#reference ⇒ Object
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 [:isbn].present? ref << "ISBN " + content_tag(:span, [:isbn], class: "isbn") end if [:unique_reference].present? ref << content_tag(:span, [:unique_reference], class: "unique_reference") end if [:command_paper_number].present? ref << content_tag(:span, [:command_paper_number], class: "command_paper_number") end if [:hoc_paper_number].present? ref << content_tag(:span, "HC #{[:hoc_paper_number]}", class: 'house_of_commons_paper_number') + ' ' + content_tag(:span, [:parliamentary_session], class: 'parliamentary_session') end ref.join(', ').html_safe end |
#references? ⇒ Boolean
151 152 153 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 151 def references? ![:isbn].to_s.empty? || ![:unique_reference].to_s.empty? || ![:command_paper_number].to_s.empty? || ![:hoc_paper_number].to_s.empty? end |
#references_for_title ⇒ Object
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: #{[:isbn]}" if [:isbn].present? references << "Unique reference: #{[:unique_reference]}" if [:unique_reference].present? references << "Command paper number: #{[:command_paper_number]}" if [:command_paper_number].present? references << "HC: #{[:hoc_paper_number]} #{[:parliamentary_session]}" if [:hoc_paper_number].present? prefix = references.size == 1 ? "and its reference" : "and its references" references.any? ? ", #{prefix} (" + references.join(", ") + ")" : "" end |
#section_class ⇒ Object
56 57 58 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 56 def section_class [:external?] ? "hosted-externally" : "embedded" end |
#thumbnail_link ⇒ Object
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(, url, "aria-hidden" => "true", "class" => ) end |
#title ⇒ Object
242 243 244 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 242 def title [:title] end |
#title_link_options ⇒ Object
262 263 264 265 266 267 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 262 def = {} ["rel"] = "external" if [:external?] ["aria-describedby"] = help_block_id unless [:accessible?] end |
#unnumbered_command_paper? ⇒ Boolean
163 164 165 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 163 def unnumbered_command_paper? [:unnumbered_command_paper?] end |
#unnumbered_paper? ⇒ Boolean
159 160 161 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 159 def unnumbered_paper? [:unnumbered_command_paper?] || [:unnumbered_hoc_paper?] end |
#url ⇒ Object
29 30 31 |
# File 'lib/govspeak/presenters/attachment_presenter.rb', line 29 def url [:url] end |