Class: PublishingPlatformPublishingComponents::Presenters::AttachmentHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/publishing_platform_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 can be provided



11
12
13
# File 'lib/publishing_platform_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/publishing_platform_publishing_components/presenters/attachment_helper.rb', line 6

def attachment_data
  @attachment_data
end

Instance Method Details

#content_typeObject



43
44
45
46
47
48
# File 'lib/publishing_platform_publishing_components/presenters/attachment_helper.rb', line 43

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

#external?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/publishing_platform_publishing_components/presenters/attachment_helper.rb', line 39

def external?
  type == "external"
end

#file_sizeObject



54
55
56
# File 'lib/publishing_platform_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/publishing_platform_publishing_components/presenters/attachment_helper.rb', line 35

def html?
  type == "html"
end

#number_of_pagesObject



58
59
60
# File 'lib/publishing_platform_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/publishing_platform_publishing_components/presenters/attachment_helper.rb', line 19

def preview_url
  @attachment_data[:preview_url]
end

#referenceObject



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

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.join(", ")
end

#thumbnail_urlObject



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

def thumbnail_url
  @attachment_data[:thumbnail_url]
end

#titleObject



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

def title
  attachment_data.fetch(:title)
end

#typeObject



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

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

#urlObject



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

def url
  attachment_data.fetch(:url)
end