Class: Alchemy::PictureView

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::AssetTagHelper, ActionView::Helpers::UrlHelper
Defined in:
app/presenters/alchemy/picture_view.rb

Overview

Renders a picture ingredient view

Constant Summary collapse

DEFAULT_OPTIONS =
{
  show_caption: true,
  disable_link: false,
  srcset: [],
  sizes: [],
}.with_indifferent_access

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ingredient, options = {}, html_options = {}) ⇒ PictureView

Returns a new instance of PictureView.


19
20
21
22
23
24
# File 'app/presenters/alchemy/picture_view.rb', line 19

def initialize(ingredient, options = {}, html_options = {})
  @ingredient = ingredient
  @options = DEFAULT_OPTIONS.merge(ingredient.settings).merge(options || {})
  @html_options = html_options || {}
  @picture = ingredient.picture
end

Instance Attribute Details

#html_optionsObject (readonly)

Returns the value of attribute html_options.


10
11
12
# File 'app/presenters/alchemy/picture_view.rb', line 10

def html_options
  @html_options
end

#ingredientObject (readonly)

Returns the value of attribute ingredient.


10
11
12
# File 'app/presenters/alchemy/picture_view.rb', line 10

def ingredient
  @ingredient
end

#optionsObject (readonly)

Returns the value of attribute options.


10
11
12
# File 'app/presenters/alchemy/picture_view.rb', line 10

def options
  @options
end

#pictureObject (readonly)

Returns the value of attribute picture.


10
11
12
# File 'app/presenters/alchemy/picture_view.rb', line 10

def picture
  @picture
end

Instance Method Details

#alt_textObject


84
85
86
# File 'app/presenters/alchemy/picture_view.rb', line 84

def alt_text
  ingredient.alt_tag.presence || html_options.delete(:alt) || ingredient.picture.name&.humanize
end

#captionObject


46
47
48
49
50
# File 'app/presenters/alchemy/picture_view.rb', line 46

def caption
  return unless show_caption?

  @_caption ||= (:figcaption, ingredient.caption)
end

#img_tagObject


56
57
58
59
60
61
62
63
64
65
66
# File 'app/presenters/alchemy/picture_view.rb', line 56

def img_tag
  @_img_tag ||= image_tag(
    src, {
      alt: alt_text,
      title: ingredient.title.presence,
      class: caption ? nil : ingredient.css_class.presence,
      srcset: srcset.join(", ").presence,
      sizes: options[:sizes].join(", ").presence,
    }.merge(caption ? {} : html_options)
  )
end

#is_linked?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'app/presenters/alchemy/picture_view.rb', line 72

def is_linked?
  !options[:disable_link] && ingredient.link.present?
end

#renderObject


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/presenters/alchemy/picture_view.rb', line 26

def render
  return if picture.blank?

  output = caption ? img_tag + caption : img_tag

  if is_linked?
    output = link_to(output, url_for(ingredient.link), {
      title: ingredient.link_title.presence,
      target: ingredient.link_target == "blank" ? "_blank" : nil,
      data: { link_target: ingredient.link_target.presence },
    })
  end

  if caption
    (:figure, output, { class: ingredient.css_class.presence }.merge(html_options))
  else
    output
  end
end

#show_caption?Boolean

Returns:

  • (Boolean)

68
69
70
# File 'app/presenters/alchemy/picture_view.rb', line 68

def show_caption?
  options[:show_caption] && ingredient.caption.present?
end

#srcObject


52
53
54
# File 'app/presenters/alchemy/picture_view.rb', line 52

def src
  ingredient.picture_url(options.except(*DEFAULT_OPTIONS.keys))
end

#srcsetObject


76
77
78
79
80
81
82
# File 'app/presenters/alchemy/picture_view.rb', line 76

def srcset
  options[:srcset].map do |size|
    url = ingredient.picture_url(size: size)
    width, height = size.split("x")
    width.present? ? "#{url} #{width}w" : "#{url} #{height}h"
  end
end