Class: Alchemy::Ingredients::PictureView

Inherits:
BaseView
  • Object
show all
Defined in:
app/components/alchemy/ingredients/picture_view.rb

Overview

Renders a picture ingredient view

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseView

#render?

Constructor Details

#initialize(ingredient, show_caption: nil, disable_link: nil, srcset: nil, sizes: nil, picture_options: {}, html_options: {}) ⇒ PictureView

Returns a new instance of PictureView.

Parameters:

  • ingredient (Alchemy::Ingredient)
  • show_caption (Boolean) (defaults to: nil)

    (true) Whether to show a caption or not, even if present on the picture.

  • disable_link (Boolean) (defaults to: nil)

    (false) Whether to disable the link even if the picture has a link.

  • srcset (Array<String>) (defaults to: nil)

    An array of srcset sizes that will generate variants of the picture.

  • sizes (Array<String>) (defaults to: nil)

    An array of sizes that will be passed to the img tag.

  • picture_options (Hash) (defaults to: {})

    Options that will be passed to the picture url. See PictureVariant for options.

  • html_options (Hash) (defaults to: {})

    Options that will be passed to the img tag.

See Also:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/components/alchemy/ingredients/picture_view.rb', line 24

def initialize(
  ingredient,
  show_caption: nil,
  disable_link: nil,
  srcset: nil,
  sizes: nil,
  picture_options: {},
  html_options: {}
)
  super(ingredient)
  @show_caption = settings_value(:show_caption, value: show_caption, default: true)
  @disable_link = settings_value(:disable_link, value: disable_link, default: false)
  @srcset = settings_value(:srcset, value: srcset, default: [])
  @sizes = settings_value(:sizes, value: sizes, default: [])
  @picture_options = picture_options || {}
  @html_options = html_options || {}
  @picture = ingredient.picture
end

Instance Attribute Details

Returns the value of attribute disable_link.



7
8
9
# File 'app/components/alchemy/ingredients/picture_view.rb', line 7

def disable_link
  @disable_link
end

#html_optionsObject (readonly)

Returns the value of attribute html_options.



7
8
9
# File 'app/components/alchemy/ingredients/picture_view.rb', line 7

def html_options
  @html_options
end

#ingredientObject (readonly)

Returns the value of attribute ingredient.



7
8
9
# File 'app/components/alchemy/ingredients/picture_view.rb', line 7

def ingredient
  @ingredient
end

#pictureObject (readonly)

Returns the value of attribute picture.



7
8
9
# File 'app/components/alchemy/ingredients/picture_view.rb', line 7

def picture
  @picture
end

#picture_optionsObject (readonly)

Returns the value of attribute picture_options.



7
8
9
# File 'app/components/alchemy/ingredients/picture_view.rb', line 7

def picture_options
  @picture_options
end

#show_captionObject (readonly)

Returns the value of attribute show_caption.



7
8
9
# File 'app/components/alchemy/ingredients/picture_view.rb', line 7

def show_caption
  @show_caption
end

#sizesObject (readonly)

Returns the value of attribute sizes.



7
8
9
# File 'app/components/alchemy/ingredients/picture_view.rb', line 7

def sizes
  @sizes
end

#srcsetObject (readonly)

Returns the value of attribute srcset.



7
8
9
# File 'app/components/alchemy/ingredients/picture_view.rb', line 7

def srcset
  @srcset
end

Instance Method Details

#callObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/components/alchemy/ingredients/picture_view.rb', line 43

def call
  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.html_safe
end