Class: Alchemy::PictureView
- Inherits:
-
Object
- Object
- Alchemy::PictureView
- 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
-
#html_options ⇒ Object
readonly
Returns the value of attribute html_options.
-
#ingredient ⇒ Object
readonly
Returns the value of attribute ingredient.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#picture ⇒ Object
readonly
Returns the value of attribute picture.
Instance Method Summary collapse
- #alt_text ⇒ Object
- #caption ⇒ Object
- #img_tag ⇒ Object
-
#initialize(ingredient, options = {}, html_options = {}) ⇒ PictureView
constructor
A new instance of PictureView.
- #is_linked? ⇒ Boolean
- #render ⇒ Object
- #show_caption? ⇒ Boolean
- #src ⇒ Object
- #srcset ⇒ Object
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, = {}, = {}) @ingredient = ingredient @options = DEFAULT_OPTIONS.merge(ingredient.settings).merge( || {}) @html_options = || {} @picture = ingredient.picture end |
Instance Attribute Details
#html_options ⇒ Object (readonly)
Returns the value of attribute html_options.
10 11 12 |
# File 'app/presenters/alchemy/picture_view.rb', line 10 def @html_options end |
#ingredient ⇒ Object (readonly)
Returns the value of attribute ingredient.
10 11 12 |
# File 'app/presenters/alchemy/picture_view.rb', line 10 def ingredient @ingredient end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'app/presenters/alchemy/picture_view.rb', line 10 def @options end |
#picture ⇒ Object (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_text ⇒ Object
84 85 86 |
# File 'app/presenters/alchemy/picture_view.rb', line 84 def alt_text ingredient.alt_tag.presence || .delete(:alt) || ingredient.picture.name&.humanize end |
#caption ⇒ Object
46 47 48 49 50 |
# File 'app/presenters/alchemy/picture_view.rb', line 46 def return unless @_caption ||= content_tag(:figcaption, ingredient.) end |
#img_tag ⇒ Object
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: ? nil : ingredient.css_class.presence, srcset: srcset.join(", ").presence, sizes: [:sizes].join(", ").presence, }.merge( ? {} : ) ) end |
#is_linked? ⇒ Boolean
72 73 74 |
# File 'app/presenters/alchemy/picture_view.rb', line 72 def is_linked? ![:disable_link] && ingredient.link.present? end |
#render ⇒ Object
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 = ? img_tag + : 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 content_tag(:figure, output, { class: ingredient.css_class.presence }.merge()) else output end end |
#show_caption? ⇒ Boolean
68 69 70 |
# File 'app/presenters/alchemy/picture_view.rb', line 68 def [:show_caption] && ingredient..present? end |
#src ⇒ Object
52 53 54 |
# File 'app/presenters/alchemy/picture_view.rb', line 52 def src ingredient.picture_url(.except(*DEFAULT_OPTIONS.keys)) end |
#srcset ⇒ Object
76 77 78 79 80 81 82 |
# File 'app/presenters/alchemy/picture_view.rb', line 76 def srcset [: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 |