Commit feaea728 authored by azul's avatar azul

style: refactor thumbnail_img_tag

parent bb270a60
......@@ -140,38 +140,41 @@ module Common::Ui::ImageHelper
def thumbnail_img_tag(asset, thumbnail_name, options = {}, html_options = {})
thumbnail = asset.thumbnail(thumbnail_name)
if thumbnail and thumbnail.height and thumbnail.width
options[:crop] ||= options[:crop!]
if options[:crop] or options[:scale]
target_width, target_height = (options[:crop] || options[:scale]).split(/x/).map(&:to_f)
if target_width > thumbnail.width || target_height > thumbnail.height
# thumbnail is actually _smaller_ than our target area
border_width = 1
margin_x = ((target_width - thumbnail.width) / 2) - border_width
margin_y = ((target_height - thumbnail.height) / 2) - border_width
image_tag(thumbnail.url, html_options.merge(size: "#{thumbnail.width}x#{thumbnail.height}",
style: "margin: #{margin_y}px #{margin_x}px;"))
elsif options[:crop]
# extra thumbnail will be hidden by overflow:hidden
ratio = [target_width / thumbnail.width, target_height / thumbnail.height].max
ratio = [1, ratio].min
height = (thumbnail.height * ratio).round
width = (thumbnail.width * ratio).round
image_tag(thumbnail.url, html_options.merge(size: "#{width}x#{height}"))
elsif options[:scale]
# set image tag to use new scale
ratio = [target_width / thumbnail.width, target_height / thumbnail.height, 1].min
height = (thumbnail.height * ratio).round
width = (thumbnail.width * ratio).round
image_tag(thumbnail.url, html_options.merge(size: "#{width}x#{height}"))
end
else
image_tag(thumbnail.url, html_options.merge(size: "#{thumbnail.width}x#{thumbnail.height}"))
thumb_options = thumbnail_img_options(thumbnail, options)
image_tag thumbnail.url, html_options.merge(thumb_options)
else
width, height = (options[:crop!] || '').split(/x/).map(&:to_f)
thumbnail_or_icon(asset, thumbnail, width, height, html_options)
end
end
def thumbnail_img_options(thumbnail, options = {})
options[:crop] ||= options[:crop!]
if options[:crop] or options[:scale]
target_width, target_height = (options[:crop] || options[:scale]).split(/x/).map(&:to_f)
if target_width > thumbnail.width || target_height > thumbnail.height
# thumbnail is actually _smaller_ than our target area
border_width = 1
margin_x = ((target_width - thumbnail.width) / 2) - border_width
margin_y = ((target_height - thumbnail.height) / 2) - border_width
return { size: "#{thumbnail.width}x#{thumbnail.height}",
style: "margin: #{margin_y}px #{margin_x}px;" }
elsif options[:crop]
# extra thumbnail will be hidden by overflow:hidden
ratio = [target_width / thumbnail.width, target_height / thumbnail.height].max
ratio = [1, ratio].min
height = (thumbnail.height * ratio).round
width = (thumbnail.width * ratio).round
return { size: "#{width}x#{height}" }
elsif options[:scale]
# set image tag to use new scale
ratio = [target_width / thumbnail.width, target_height / thumbnail.height, 1].min
height = (thumbnail.height * ratio).round
width = (thumbnail.width * ratio).round
return { size: "#{width}x#{height}" }
end
elsif options[:crop!]
target_width, target_height = options[:crop!].split(/x/).map(&:to_f)
thumbnail_or_icon(asset, thumbnail, target_width, target_height, html_options)
else
thumbnail_or_icon(asset, thumbnail, html_options)
return { size: "#{thumbnail.width}x#{thumbnail.height}" }
end
end
......@@ -195,26 +198,6 @@ module Common::Ui::ImageHelper
link_to img, url, options.slice(:class, :title, :style, :method, :remote)
end
# links to an asset with a thumbnail preview
def old_link_to_asset(asset, thumbnail_name, options = {})
thumbnail = asset.thumbnail(thumbnail_name)
img = thumbnail_img_tag(asset, thumbnail_name, options)
if size = (options[:crop] || options[:scale] || options[:crop!])
target_width, target_height = size.split(/x/).map(&:to_f)
elsif thumbnail and thumbnail.width and thumbnail.height
target_width = thumbnail.width
target_height = thumbnail.height
else
target_width = 32
target_height = 32
end
style = "height:#{target_height}px;width:#{target_width}px"
klass = options[:class] || 'thumbnail'
url = options[:url] || asset.url
method = options[:method] || 'get'
link_to img, url, class: klass, title: asset.filename, style: style, method: method
end
def thumbnail_or_icon(asset, thumbnail, width = nil, height = nil, html_options = {})
if thumbnail
image_tag(thumbnail.url, html_options)
......
---
assets_001:
bee:
content_type: image/jpeg
size: "100266"
type: Image
......
require 'test_helper'
class ImageHelperTest < ActiveSupport::TestCase
class ViewMock
include Common::Ui::ImageHelper
def image_tag(*args)
@args = args
end
def image_tag_args
@args
end
end
def test_stub
assert ViewMock.include? Common::Ui::ImageHelper
end
def test_fallback
mock.thumbnail_img_tag asset_without_thumbs, :medium
assert_called_image_tag_with '/images/png/16/mime_image.png',
style: 'vertical-align: middle;'
end
def test_medium_thumbnail
mock.thumbnail_img_tag asset, :medium
assert_called_image_tag_with '/assets/1/bee_medium.jpg',
size: '133x200'
end
def test_large_thumbnail
mock.thumbnail_img_tag asset, :large
assert_called_image_tag_with '/assets/1/bee_large.jpg',
size: '333x500'
end
def test_crop_thumbnail
mock.thumbnail_img_tag asset, :large, crop: '100x100'
assert_called_image_tag_with '/assets/1/bee_large.jpg',
size: '100x150'
end
def test_scale_thumbnail
mock.thumbnail_img_tag asset, :large, scale: '100x100'
assert_called_image_tag_with '/assets/1/bee_large.jpg',
size: '67x100'
end
protected
def asset
asset = asset_without_thumbs
asset.create_thumbnail_records
asset
end
def asset_without_thumbs
assets(:bee)
end
def assert_called_image_tag_with(*args)
assert_equal args, mock.image_tag_args
end
def mock
@mock ||= ViewMock.new
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment