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.



223
224
225
# File 'lib/has_image.rb', line 223

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

#generate_thumbnail!(thumb_name) ⇒ Object



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

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)


182
183
184
# File 'lib/has_image.rb', line 182

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.



293
294
295
# File 'lib/has_image.rb', line 293

def has_image_id
  id
end

#heightObject



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

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



196
197
198
# File 'lib/has_image.rb', line 196

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



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

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)


203
204
205
206
207
208
209
210
211
212
# File 'lib/has_image.rb', line 203

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



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

def image_size
  [width, height] * 'x'
end

#install_imagesObject

Processes and installs the image and its thumbnails.



277
278
279
280
# File 'lib/has_image.rb', line 277

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



216
217
218
# File 'lib/has_image.rb', line 216

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.



228
229
230
# File 'lib/has_image.rb', line 228

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

#remove_imagesObject

Deletes the image from the storage.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/has_image.rb', line 250

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?
        # Resorting to SQL here to avoid triggering callbacks. There must be
        # a better way to do this.
        self.connection.execute("UPDATE #{self.class.table_name} SET #{has_image_options[:column]} = NULL WHERE id = #{id}")          
        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.



284
285
286
# File 'lib/has_image.rb', line 284

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.



270
271
272
273
274
# File 'lib/has_image.rb', line 270

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

#widthObject



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

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