lib/artwork/model.rb in artwork-0.7.3 vs lib/artwork/model.rb in artwork-1.0.0

- old
+ new

@@ -1,61 +1,82 @@ -require 'artwork/thumbnail' - module Artwork module Model - def artwork_thumb_for(attachment_name, size, alternative_sizes = nil) + def artwork_url(attachment_name, size, options = {}) + thumb_name = attachment_style_for(attachment_name, size, options) + + return nil unless thumb_name + + send(attachment_name).url(thumb_name, options) + end + + def attachment_styles_for(attachment_name) + self.class.attachment_definitions[attachment_name.to_sym][:styles].keys + end + + def attachment_style_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 + thumb = + if DesiredThumbnail.compatible?(size) + artwork_thumb_for(attachment_name, size) + else + plain_thumb_for(attachment_name, size) + end - desired_thumb = Thumbnail.new(size) - matching_thumb_name = nil + return nil unless thumb - ratio_for_current_resolution = base_resolution.to_f / Artwork.current_resolution.to_f + if thumb.respond_to?(:name) + thumb.name.to_sym + else + thumb.to_sym + end + end - if desired_thumb.compatible? - desired_size = desired_thumb.width / ratio_for_current_resolution + def artwork_thumbs_for(attachment_name) + attachment_styles_for(attachment_name).map { |style| Thumbnail.from_style(style) } + end - thumbs = attachment_styles_for(attachment_name).map { |thumb_name| Thumbnail.new(thumb_name) } + def artwork_thumb_for(attachment_name, size) + desired_thumb = DesiredThumbnail.from_style(size) - 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 = artwork_thumbs_for(attachment_name) - thumbs.each do |thumb| - if desired_size <= thumb.width - matching_thumb_name = thumb.name - break - end - end + thumbs = thumbs.select(&:compatible?).select { |thumb| desired_thumb.is_like?(thumb) }.sort - # If we did not find any matching attachment definitions, - # the desired size is probably larger than all of our thumb widths, - # So pick the last (largest) one we have. - return nil if thumbs.empty? - matching_thumb_name ||= thumbs.last.name + if Artwork.load_2x_images? + retina_artwork_thumb_for(attachment_name, thumbs, desired_thumb) + else + normal_artwork_thumb_for(attachment_name, thumbs, desired_thumb) end + end - matching_thumb_name ||= size.to_sym + def plain_thumb_for(attachment_name, label) + all_styles = attachment_styles_for(attachment_name) - if Artwork.load_2x_images? and attachment_styles_for(attachment_name).include?(:"#{matching_thumb_name}_2x") - matching_thumb_name = :"#{matching_thumb_name}_2x" - end + normal_thumb = label.to_sym + retina_thumb = :"#{label}_2x" - matching_thumb_name.to_sym + if Artwork.load_2x_images? and all_styles.include?(retina_thumb) + retina_thumb + elsif all_styles.include?(normal_thumb) + normal_thumb + end end - def artwork_url(attachment_name, size, options = {}) - thumb_name = artwork_thumb_for(attachment_name, size, options) - send(attachment_name).url(thumb_name, options) + def normal_artwork_thumb_for(attachment_name, thumbs, desired_thumb) + usable_thumbs = thumbs.reject(&:retina?) + + thumb = usable_thumbs.find { |current| desired_thumb.expected_width <= current.width } + + thumb || usable_thumbs.max_by(&:width) end - def attachment_styles_for(attachment_name) - self.class.attachment_definitions[attachment_name.to_sym][:styles].keys + def retina_artwork_thumb_for(attachment_name, thumbs, desired_thumb) + retina_thumbs = thumbs.select(&:retina?) + + thumb = retina_thumbs.find { |current| desired_thumb.expected_width <= current.width } + + thumb || normal_artwork_thumb_for(attachment_name, thumbs, desired_thumb) || retina_thumbs.max_by(&:width) end private def determine_alternative_size_for(alternative_sizes)