Class: Govspeak::ImagePresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/govspeak/presenters/image_presenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image, locale: "en") ⇒ ImagePresenter

Returns a new instance of ImagePresenter.



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

def initialize(image, locale: "en")
  @image = image
  @locale = locale
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



3
4
5
# File 'lib/govspeak/presenters/image_presenter.rb', line 3

def image
  @image
end

#localeObject (readonly)

Returns the value of attribute locale.



3
4
5
# File 'lib/govspeak/presenters/image_presenter.rb', line 3

def locale
  @locale
end

Instance Method Details

#alt_textObject



14
15
16
# File 'lib/govspeak/presenters/image_presenter.rb', line 14

def alt_text
  image.respond_to?(:alt_text) ? image.alt_text : image[:alt_text]
end

#captionObject



18
19
20
# File 'lib/govspeak/presenters/image_presenter.rb', line 18

def caption
  (image.respond_to?(:caption) ? image.caption : image[:caption]).to_s.strip.presence
end

#creditObject



22
23
24
# File 'lib/govspeak/presenters/image_presenter.rb', line 22

def credit
  (image.respond_to?(:credit) ? image.credit : image[:credit]).to_s.strip.presence
end

#figcaption?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/govspeak/presenters/image_presenter.rb', line 30

def figcaption?
  caption.present? || credit.present?
end

#figcaption_htmlObject



34
35
36
37
38
39
40
41
# File 'lib/govspeak/presenters/image_presenter.rb', line 34

def figcaption_html
  lines = []
  lines << "<figcaption>"
  lines << %(<p>#{caption}</p>) if caption.present?
  lines << %(<p>#{Govspeak::TranslationHelper.t_with_fallback('govspeak.image.figure.credit', credit:, locale:)}</p>) if credit.present?
  lines << "</figcaption>"
  lines.join
end

#idObject



26
27
28
# File 'lib/govspeak/presenters/image_presenter.rb', line 26

def id
  nil
end

#urlObject



10
11
12
# File 'lib/govspeak/presenters/image_presenter.rb', line 10

def url
  image.respond_to?(:url) ? image.url : image[:url]
end