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

  • 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



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

def alternative_format_contact_email
  attachment_data[:alternative_format_contact_email]
end

#content_typeObject



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

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

#content_type_abbrObject



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

def content_type_abbr
  content_type.abbr
end

#content_type_nameObject



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

def content_type_name
  content_type.name
end

#file_sizeObject



38
39
40
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 38

def file_size
  attachment_data[:file_size]
end

#is_official_documentObject



70
71
72
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 70

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



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

def number_of_pages
  attachment_data[:number_of_pages]
end

#referenceObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 50

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

#titleObject



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

def title
  attachment_data.fetch(:title)
end

#unnumbered_referenceObject



64
65
66
67
68
# File 'lib/govuk_publishing_components/presenters/attachment_helper.rb', line 64

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

#urlObject



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

def url
  attachment_data.fetch(:url)
end