Class: Alchemy::EssencePictureView

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

Overview

Renders an essence picture 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(content, options = {}, html_options = {}) ⇒ EssencePictureView

Returns a new instance of EssencePictureView.


19
20
21
22
23
24
25
# File 'app/models/alchemy/essence_picture_view.rb', line 19

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

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.


10
11
12
# File 'app/models/alchemy/essence_picture_view.rb', line 10

def content
  @content
end

#essenceObject (readonly)

Returns the value of attribute essence.


10
11
12
# File 'app/models/alchemy/essence_picture_view.rb', line 10

def essence
  @essence
end

#html_optionsObject (readonly)

Returns the value of attribute html_options.


10
11
12
# File 'app/models/alchemy/essence_picture_view.rb', line 10

def html_options
  @html_options
end

#optionsObject (readonly)

Returns the value of attribute options.


10
11
12
# File 'app/models/alchemy/essence_picture_view.rb', line 10

def options
  @options
end

#pictureObject (readonly)

Returns the value of attribute picture.


10
11
12
# File 'app/models/alchemy/essence_picture_view.rb', line 10

def picture
  @picture
end

Instance Method Details

#alt_textObject


85
86
87
# File 'app/models/alchemy/essence_picture_view.rb', line 85

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

#captionObject


47
48
49
50
51
# File 'app/models/alchemy/essence_picture_view.rb', line 47

def caption
  return unless show_caption?

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

#img_tagObject


57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/alchemy/essence_picture_view.rb', line 57

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

#is_linked?Boolean

Returns:

  • (Boolean)

73
74
75
# File 'app/models/alchemy/essence_picture_view.rb', line 73

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

#renderObject


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

def render
  return if picture.blank?

  output = caption ? img_tag + caption : img_tag

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

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

#show_caption?Boolean

Returns:

  • (Boolean)

69
70
71
# File 'app/models/alchemy/essence_picture_view.rb', line 69

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

#srcObject


53
54
55
# File 'app/models/alchemy/essence_picture_view.rb', line 53

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

#srcsetObject


77
78
79
80
81
82
83
# File 'app/models/alchemy/essence_picture_view.rb', line 77

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