5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/artwork/model.rb', line 5
def artwork_thumb_for(attachment_name, size, alternative_sizes = nil)
size = determine_alternative_size_for(alternative_sizes) || size
size, base_resolution = size.to_s.split('@')
base_resolution ||= Artwork.base_resolution
desired_thumb = Thumbnail.new(size)
matching_thumb_name = nil
ratio_for_current_resolution = base_resolution.to_f / Artwork.current_resolution.to_f
if desired_thumb.compatible?
desired_size = desired_thumb.width / ratio_for_current_resolution
thumbs = attachment_styles_for(attachment_name).map { |thumb_name| Thumbnail.new(thumb_name) }
thumbs = thumbs \
.select(&:compatible?) \
.reject(&:retina?) \
.select { |thumb| desired_thumb.label.nil? or desired_thumb.label == thumb.label.to_s } \
.select { |thumb| desired_thumb.aspect_ratio.nil? or desired_thumb.same_aspect_ratio_with?(thumb) } \
.sort
thumbs.each do |thumb|
if desired_size <= thumb.width
matching_thumb_name = thumb.name
break
end
end
return nil if thumbs.empty?
matching_thumb_name ||= thumbs.last.name
end
matching_thumb_name ||= size.to_sym
if Artwork.load_2x_images? and attachment_styles_for(attachment_name).include?(:"#{matching_thumb_name}_2x")
matching_thumb_name = :"#{matching_thumb_name}_2x"
end
matching_thumb_name.to_sym
end
|