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

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" />

Parameters:

  • image (Asset, Attachment)

    Asset or Attachemnt

  • size (Symbol)

    Size of image

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

    Optional options for the Rails image_tag

Returns:

  • (String)

    Image tag



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, options = {})
  if image.respond_to?(:asset)
    image = image.asset
  end

  size = size.to_sym
  if image.present? && Slices::Config.asset_styles.has_key?(size)
    options.reverse_merge!(size: image.dimensions_for(size))
    image_tag(image.url_for(size), options)
  end
end

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>

Parameters:

  • link (Object)

    Link to pass to link_to

  • image (Asset, Attachment)

    Asset or Attachemnt

  • size (Symbol)

    Size of image

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

    Options for the Rails image_tag, use :image_options to pass options to the image_tag

Returns:

  • (String)

    Image wrapped with a link tag



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, options = {})
  image_options = options.has_key?(:image_options) ? options.delete(:image_options) : {}
  image = image_if_present(image, size, image_options)
  if image.present?
    if link.present?
      link_to(image, link, options)
    else
      image
    end
  end
end