module RailsPicture::RailsPictureHelper

Add a helper method to Rails views

Constants

IMG_FORMATS
SOURCE_FORMATS

Public Instance Methods

picture_tag(filename, **options) click to toggle source
# File lib/rails_picture/rails_picture_helper.rb, line 10
def picture_tag(filename, **options)
  html = []

  SOURCE_FORMATS.each { |f| html << build_source_tag(filename, f) }

  html << build_image_tag(filename, **options)

  tag.picture { safe_join html }
end

Private Instance Methods

asset_path(filename, format) click to toggle source
# File lib/rails_picture/rails_picture_helper.rb, line 36
def asset_path(filename, format)
  path = resolve_asset_path "#{filename}.#{format}"
  path && "/assets/#{path}"
end
build_image_tag(filename, **options) click to toggle source
# File lib/rails_picture/rails_picture_helper.rb, line 26
def build_image_tag(filename, **options)
  tag.img(src: find_img_format(filename), alt: filename.humanize, **options)
end
build_source_tag(filename, format) click to toggle source
# File lib/rails_picture/rails_picture_helper.rb, line 22
def build_source_tag(filename, format)
  tag.source(srcset: asset_path(filename, format), type: "image/#{format}")
end
find_img_format(filename) click to toggle source
# File lib/rails_picture/rails_picture_helper.rb, line 30
def find_img_format(filename)
  IMG_FORMATS.map do |format|
    asset_path(filename, format)
  end.compact.first
end