Module: AssetsHelper
- Included in:
- PagesHelper
- Defined in:
- app/helpers/assets_helper.rb
Overview
This module provides methods for rendering HTML that links views to assets.
image_if_present(slice.image, :medium)
# => <img alt="image" height="146" src="/system/50ca095/medium/image.jpg" width="220" />
Instance Method Summary collapse
-
#image_if_present(image, size, options = {}) ⇒ String
Returns an image tag for image of the specified size.
-
#link_image_if_linkable(link, image, size, options = {}) ⇒ String
Returns a link to an image of the specificed size.
Instance Method Details
#image_if_present(image, size, options = {}) ⇒ String
Returns an image tag for image of the specified size. If the image is nil or not present then nothing is returned. The image can be an Asset or Attachment. The available sizes are defined by Slices::Config.asset_styles. The image tag will have width and height attributes.
image_if_present(slice.image, :medium)
# => <img alt="image" height="146" src="/system/50ca095/medium/image.jpg" width="220" />
21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/helpers/assets_helper.rb', line 21 def image_if_present(image, size, = {}) if image.respond_to?(:asset) image = image.asset end size = size.to_sym if image.present? && Slices::Config.asset_styles.has_key?(size) .reverse_merge!(size: image.dimensions_for(size)) image_tag(image.url_for(size), ) end end |
#link_image_if_linkable(link, image, size, options = {}) ⇒ String
Returns a link to an image of the specificed size. If the image is nil or not present then nothing is returned, and if the link is not present then only the image is returned. The image can be an Asset or Attachment. The sizes are defined by Slices::Config.asset_styles
link_image_if_linkable(article.path, article.feature_image, :icon)
# => <a href="/article">
# => <img alt="image" height="146" src="/system/50ca095/icon/image.jpg" width="220" />
# => </a>
51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/helpers/assets_helper.rb', line 51 def link_image_if_linkable(link, image, size, = {}) = .has_key?(:image_options) ? .delete(:image_options) : {} image = image_if_present(image, size, ) if image.present? if link.present? link_to(image, link, ) else image end end end |