Module: Artwork::Model

Defined in:
lib/artwork/model.rb

Instance Method Summary collapse

Instance Method Details

#artwork_thumb_for(attachment_name, size) ⇒ Object



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
# File 'lib/artwork/model.rb', line 5

def artwork_thumb_for(attachment_name, size)
  desired_thumb = Thumbnail.new(size)
  matching_thumb_name = nil

  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) } \
      .select(&:compatible?) \
      .reject(&:retina?) \
      .sort

    thumbs.each do |thumb|
      if desired_size <= thumb.width and thumb.label == desired_thumb.label
        matching_thumb_name = thumb.name
        break
      end
    end

    # 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.
    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

#artwork_url(attachment_name, size, options = nil) ⇒ Object



40
41
42
43
# File 'lib/artwork/model.rb', line 40

def artwork_url(attachment_name, size, options = nil)
  thumb_name = artwork_thumb_for(attachment_name, size)
  send(attachment_name).url(thumb_name, options)
end

#attachment_styles_for(attachment_name) ⇒ Object



45
46
47
# File 'lib/artwork/model.rb', line 45

def attachment_styles_for(attachment_name)
  self.class.attachment_definitions[attachment_name.to_sym][:styles].keys
end