SpeciesFileGroup/taxonworks

View on GitHub
app/helpers/images_helper.rb

Summary

Maintainability
A
25 mins
Test Coverage
module ImagesHelper

  # !! Rails already provides image_tag, i.e. it is not required here.

  def image_link(image, size: :thumb)
    return nil if image.nil?
    link_to(image_tag(image.image_file.url(size)), image)
  end

  def images_search_form
    render('/images/quick_search_form')
  end

  # @return [String]
  #   like `jpeg`, `png`
  def image_type(image)
    image.image_file.content_type.split('/').last.downcase
  end


  # "/images/scale_to_box/:x/:y/:width/:height/:box_width/:box_height" 
  def original_as_png_via_api(image, api: true)
   h = image.height
   w = image.width 

   s = api ? '/api/v1' : ''

   s << "/images/#{image.id}/scale_to_box/0/0/#{w}/#{h}/#{w}/#{h}"
   s
  end

  # Return a ShortenedUrl to the original file image
  # @params image [Image, Integer]
  def image_short_url(image, api: true)
    if !image.kind_of?(::Image) && (Integer(image) rescue false)
      image = ::Image.find(image)
    end

    if api
      short_url( api_v1_image_file_path( image) )
    else
      short_url( image.image_file.url(:original) )
    end
  end

  # @return [True]
  #   indicates a custom partial should be used, see list_helper.rb
  def images_recent_objects_partial
    true 
  end

  def image_autocomplete_tag(image)
    content_tag(:figure) do
      (
        image_tag(image.image_file.url(:thumb)) +
        content_tag(:caption, "id:#{image.id}", class: ['feedback', 'feedback-primary', 'feedback-thin']) 
      ).html_safe
    end
  end

  def thumb_list_tag(object)
    if object.depictions.any?
      object.depictions.collect{|a|
        content_tag(:div)  do
          link_to( depiction_tag(a, size: :medium), a.image.image_file.url())
        end
      }.join.html_safe
    end
  end

  def image_display_url(image)
    case image.image_file_content_type
    when 'image/tiff'
      "/images/#{image.id}/extract/0/0/#{image.width}/#{image.height}"
    else
      root_url + image.image_file.url[1..-1]
    end
  end

end