Class: Image

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/image.rb

Constant Summary collapse

MAX_SIZE_IN_MB =

What is the max image size a user can upload

5
PAGES_PER_DIALOG =

when a dialog pops up with images, how many images per page should there be

18
PAGES_PER_DIALOG_THAT_HAS_SIZE_OPTIONS =

when a dialog pops up with images, but that dialog has image resize options how many images per page should there be

12
PAGES_PER_ADMIN_INDEX =

when listing images out in the admin area, how many images should show per page

20

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.per_page(dialog = false, has_size_options = false) ⇒ Object

How many images per page should be displayed?



35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/image.rb', line 35

def per_page(dialog = false, has_size_options = false)
  if dialog
    unless has_size_options
      PAGES_PER_DIALOG
    else
      PAGES_PER_DIALOG_THAT_HAS_SIZE_OPTIONS
    end
  else
    PAGES_PER_ADMIN_INDEX
  end
end

.user_image_sizesObject



47
48
49
50
51
52
53
# File 'app/models/image.rb', line 47

def user_image_sizes
  RefinerySetting.find_or_set(:user_image_sizes, {
    :small => '110x110>',
    :medium => '225x255>',
    :large => '450x450>'
  }, :destroyable => false)
end

Instance Method Details

#thumbnail(geometry = nil) ⇒ Object

Get a thumbnail job object given a geometry.



57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/image.rb', line 57

def thumbnail(geometry = nil)
  if geometry.is_a?(Symbol) and self.class.user_image_sizes.keys.include?(geometry)
    geometry = self.class.user_image_sizes[geometry]
  end

  if geometry.present? && !geometry.is_a?(Symbol)
    image.thumb(geometry)
  else
    image
  end
end

#thumbnail_dimensions(geometry) ⇒ Object

Intelligently works out dimensions for a thumbnail of this image based on the Dragonfly geometry string.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/image.rb', line 70

def thumbnail_dimensions(geometry)
  geometry = geometry.to_s
  width = original_width = self.image_width.to_f
  height = original_height = self.image_height.to_f
  geometry_width, geometry_height = geometry.to_s.split(%r{\#{1,2}|\+|>|!|x}im)[0..1].map(&:to_f)
  if (original_width * original_height > 0) && geometry =~ ::Dragonfly::ImageMagick::Processor::THUMB_GEOMETRY
    if geometry =~ ::Dragonfly::ImageMagick::Processor::RESIZE_GEOMETRY
      if geometry !~ %r{\d+x\d+>} || (geometry =~ %r{\d+x\d+>} && (width > geometry_width.to_f || height > geometry_height.to_f))
        # Try scaling with width factor first. (wf = width factor)
        wf_width = (original_width * (geometry_width / width)).ceil
        wf_height = (original_height * (geometry_width / width)).ceil

        # Scale with height factor (hf = height factor)
        hf_width = (original_width * (geometry_height / height)).ceil
        hf_height = (original_height * (geometry_height / height)).ceil

        # Take the highest value that doesn't exceed either axis limit.
        use_wf = wf_width <= geometry_width && wf_height <= geometry_height
        if use_wf && hf_width <= geometry_width && hf_height <= geometry_height
          use_wf = wf_width * wf_height > hf_width * hf_height
        end

        if use_wf
          width = wf_width
          height = wf_height
        else
          width = hf_width
          height = hf_height
        end
      end
    else
      # cropping
      width = geometry_width
      height = geometry_height
    end
  end

  {:width => width.to_i, :height => height.to_i}
end

#titleObject

Returns a titleized version of the filename my_file.jpg returns My File



112
113
114
# File 'app/models/image.rb', line 112

def title
  CGI::unescape(image_name.to_s).gsub(/\.\w+$/, '').titleize
end