Class: GovukPublishingComponents::Presenters::AttachmentHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_publishing_components/presenters/attachment_helper.rb

Defined Under Namespace

Classes: SupportedContentType, UnsupportedContentType

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attachment_data) ⇒ AttachmentHelper

Expects a hash of attachment data

  • title and url are required

  • type, content_type, filename, file_size, number of pages, alternative_format_contact_email can be provided



11
12
13
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 11

def initialize(attachment_data)
  @attachment_data = attachment_data.with_indifferent_access
end

Instance Attribute Details

#attachment_dataObject (readonly)

Returns the value of attribute attachment_data.



6
7
8
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 6

def attachment_data
  @attachment_data
end

Instance Method Details

#alternative_format_contact_emailObject



62
63
64
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 62

def alternative_format_contact_email
  attachment_data[:alternative_format_contact_email]
end

#content_typeObject



39
40
41
42
43
44
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 39

def content_type
  @content_type ||= SupportedContentType.find(
    attachment_data[:content_type],
    attachment_data[:filename] ? File.extname(attachment_data[:filename]) : nil,
  )
end

#content_type_abbrObject



46
47
48
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 46

def content_type_abbr
  content_type.abbr
end

#content_type_nameObject



50
51
52
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 50

def content_type_name
  content_type.name
end

#file_sizeObject



54
55
56
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 54

def file_size
  attachment_data[:file_size]
end

#html?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 35

def html?
  type == "html"
end

#is_official_documentObject



86
87
88
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 86

def is_official_document
  attachment_data[:command_paper_number].present? || attachment_data[:hoc_paper_number].present? || attachment_data[:unnumbered_command_paper].eql?(true) || attachment_data[:unnumbered_hoc_paper].eql?(true)
end

#number_of_pagesObject



58
59
60
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 58

def number_of_pages
  attachment_data[:number_of_pages]
end

#preview_urlObject



19
20
21
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 19

def preview_url
  @attachment_data[:preview_url]
end

#referenceObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 66

def reference
  reference = []
  reference << "ISBN #{attachment_data[:isbn]}" if attachment_data[:isbn].present?
  reference << attachment_data[:unique_reference] if attachment_data[:unique_reference].present?
  reference << attachment_data[:command_paper_number] if attachment_data[:command_paper_number].present?
  if attachment_data[:hoc_paper_number].present?
    hoc_reference = "HC #{attachment_data[:hoc_paper_number]}"
    hoc_reference += " #{attachment_data[:parliamentary_session]}" if attachment_data[:parliamentary_session].present?
    reference << hoc_reference
  end

  reference.join(", ")
end

#thumbnail_urlObject



15
16
17
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 15

def thumbnail_url
  @attachment_data[:thumbnail_url]
end

#titleObject



23
24
25
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 23

def title
  attachment_data.fetch(:title)
end

#typeObject



31
32
33
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 31

def type
  attachment_data.fetch(:type, "file")
end

#unnumbered_referenceObject



80
81
82
83
84
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 80

def unnumbered_reference
  unnumbered_reference = "Unnumbered command paper" if attachment_data[:unnumbered_command_paper].eql?(true) && attachment_data[:command_paper_number].blank?
  unnumbered_reference = "Unnumbered act paper" if attachment_data[:unnumbered_hoc_paper].eql?(true) && attachment_data[:hoc_paper_number].blank?
  unnumbered_reference
end

#urlObject



27
28
29
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 27

def url
  attachment_data.fetch(:url)
end