Module: HasImage::ModelInstanceMethods

Defined in:
lib/has_image.rb

Instance Method Summary collapse

Instance Method Details

#absolute_path(thumbnail = nil) ⇒ Object

Gets the absolute filesystem path for the image, or optionally, its thumbnail.



232
233
234
# File 'lib/has_image.rb', line 232

def absolute_path(thumbnail = nil)
  storage.filesystem_path_for(self, thumbnail)
end

#generate_thumbnail!(thumb_name) ⇒ Object



242
243
244
# File 'lib/has_image.rb', line 242

def generate_thumbnail!(thumb_name)
  storage.generate_thumbnail(has_image_id, send(has_image_options[:column]), thumb_name)
end

#has_image?Boolean

Does the object have an image?

Returns:

  • (Boolean)


191
192
193
# File 'lib/has_image.rb', line 191

def has_image?
  !send(has_image_options[:column]).blank?
end

#has_image_idObject

By default, just returns the model's id. Since this id is used to divide the images up in directories, you can override this to return a related model's id if you want the images to be grouped differently. For example, if a “member” has_many “photos” you can override this to return member.id to group images by member.



304
305
306
# File 'lib/has_image.rb', line 304

def has_image_id
  id
end

#heightObject



250
251
252
# File 'lib/has_image.rb', line 250

def height
  self[:height] || storage.measure(absolute_path, :height)
end

#image_dataObject Also known as: uploaded_data

nil placeholder in case this field is used in a form. Aliased as uploaded_data for compatibility with attachment_fu



205
206
207
# File 'lib/has_image.rb', line 205

def image_data
  nil
end

#image_data=(image_data) ⇒ Object Also known as: uploaded_data=

Sets the uploaded image data. Image data can be an instance of Tempfile, or an instance of any class than inherits from IO. aliased as uploaded_data= for compatibility with attachment_fu



198
199
200
201
# File 'lib/has_image.rb', line 198

def image_data=(image_data)
  return if image_data.blank?
  storage.image_data = image_data
end

#image_data_valid?Boolean

Is the image data a file that ImageMagick can process, and is it within the allowed minimum and maximum sizes?

Returns:

  • (Boolean)


212
213
214
215
216
217
218
219
220
221
# File 'lib/has_image.rb', line 212

def image_data_valid?
  return if !storage.temp_file
  if storage.image_too_big?
    errors.add_to_base(self.class.has_image_options[:image_too_big_message])
  elsif storage.image_too_small?
    errors.add_to_base(self.class.has_image_options[:image_too_small_message])
  elsif !HasImage::Processor.valid?(storage.temp_file)
    errors.add_to_base(self.class.has_image_options[:invalid_image_message])
  end
end

#image_sizeObject



254
255
256
# File 'lib/has_image.rb', line 254

def image_size
  self[:image_size] || [width, height].join('x')
end

#install_imagesObject

Processes and installs the image and its thumbnails.



288
289
290
291
# File 'lib/has_image.rb', line 288

def install_images
  return if !storage.temp_file
  populate_attributes
end

#public_path(thumbnail = nil) ⇒ Object Also known as: public_filename

Gets the “web path” for the image, or optionally, its thumbnail. Aliased as public_filename for compatibility with attachment-Fu



225
226
227
# File 'lib/has_image.rb', line 225

def public_path(thumbnail = nil)
  storage.public_path_for(self, thumbnail)
end

#regenerate_thumbnails!Object Also known as: regenerate_thumbnails

Regenerates the thumbails from the main image.



237
238
239
# File 'lib/has_image.rb', line 237

def regenerate_thumbnails!
  storage.generate_thumbnails(has_image_id, send(has_image_options[:column]))
end

#remove_imagesObject

Deletes the image from the storage.



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/has_image.rb', line 259

def remove_images
  return if send(has_image_options[:column]).blank? || !has_image_options[:delete]
  self.class.transaction do
    begin
      storage.remove_images(self, send(has_image_options[:column]))
      # The record will be frozen if we're being called after destroy.
      unless frozen?
        # FIXME: although this is cleaner now, it introduces a new issue
        # with partial updates.
        updates    = "#{connection.quote_column_name(has_image_options[:column])} = NULL"
        conditions = "#{connection.quote_column_name(self.class.primary_key)} = #{connection.quote(id)}"
        self.class.update_all(updates, conditions)
        self.send("#{has_image_options[:column]}=", nil)
      end
    rescue Errno::ENOENT
      logger.warn("Could not delete files for #{self.class.to_s} #{to_param}")
    end
  end
end

#storageObject

Gets an instance of the underlying storage functionality. See HasImage::Storage.



295
296
297
# File 'lib/has_image.rb', line 295

def storage
  @storage ||= HasImage::Storage.new(has_image_options)
end

#update_imagesObject

Creates new images and removes the old ones when image_data has been set.



281
282
283
284
285
# File 'lib/has_image.rb', line 281

def update_images
  return if storage.temp_file.blank?
  remove_images
  populate_attributes
end

#widthObject



246
247
248
# File 'lib/has_image.rb', line 246

def width
  self[:width] || storage.measure(absolute_path, :width)
end