class Imgix::Rails::PictureTag

Public Class Methods

new(path, source: nil, tag_options: {}, url_params: {}, breakpoints: {}, srcset_options: {}) click to toggle source
# File lib/imgix/rails/picture_tag.rb, line 8
def initialize(path, source: nil, tag_options: {}, url_params: {}, breakpoints: {}, srcset_options: {})
  @path = path
  @source = source
  @tag_options = tag_options
  @url_params = url_params
  @breakpoints = breakpoints
  @srcset_options = srcset_options
end

Public Instance Methods

render() click to toggle source
# File lib/imgix/rails/picture_tag.rb, line 17
def render
  content_tag(:picture, @tag_options) do
    @breakpoints.each do |media, opts|
      validate_opts(opts)

      source_tag_opts = opts[:tag_options] || {}
      source_tag_url_params = opts[:url_params] || {}
      srcset_options = opts[:srcset_options] || {}
      source_tag_opts[:media] ||= media
      source_tag_opts[:srcset] ||= srcset(url_params: @url_params.clone.merge(source_tag_url_params), srcset_options: srcset_options)

      concat(content_tag(:source, nil, source_tag_opts))
    end

    concat Imgix::Rails::ImageTag.new(@path, source: @source, url_params: @url_params, srcset_options: @srcset_options).render
  end
end

Private Instance Methods

validate_opts(opts = {}) click to toggle source
# File lib/imgix/rails/picture_tag.rb, line 36
def validate_opts(opts = {})
  unsupported_opts = opts.except(:tag_options, :url_params, :srcset_options)
    if unsupported_opts.length > 0
      raise "'#{unsupported_opts.keys.join("', '")}' key(s) not supported; use tag_options, url_params, srcset_options."
    end
end