Module: HasImage::ModelInstanceMethods
- Defined in:
- lib/has_image.rb
Instance Method Summary (collapse)
-
- (Object) absolute_path(thumbnail = nil)
Gets the absolute filesystem path for the image, or optionally, its thumbnail.
- - (Object) generate_thumbnail!(thumb_name)
-
- (Boolean) has_image?
Does the object have an image?.
-
- (Object) has_image_id
By default, just returns the model's id.
- - (Object) height
-
- (Object) image_data
(also: #uploaded_data)
nil placeholder in case this field is used in a form.
-
- (Object) image_data=(image_data)
(also: #uploaded_data=)
Sets the uploaded image data.
-
- (Boolean) image_data_valid?
Is the image data a file that ImageMagick can process, and is it within the allowed minimum and maximum sizes?.
- - (Object) image_size
-
- (Object) install_images
Processes and installs the image and its thumbnails.
-
- (Object) public_path(thumbnail = nil)
(also: #public_filename)
Gets the “web path” for the image, or optionally, its thumbnail.
-
- (Object) regenerate_thumbnails!
(also: #regenerate_thumbnails)
Regenerates the thumbails from the main image.
-
- (Object) remove_images
Deletes the image from the storage.
-
- (Object) storage
Gets an instance of the underlying storage functionality.
-
- (Object) update_images
Creates new images and removes the old ones when image_data has been set.
- - (Object) width
Instance Method Details
- (Object) absolute_path(thumbnail = nil)
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 |
- (Object) generate_thumbnail!(thumb_name)
242 243 244 |
# File 'lib/has_image.rb', line 242 def generate_thumbnail!(thumb_name) storage.generate_thumbnail(has_image_id, send([:column]), thumb_name) end |
- (Boolean) has_image?
Does the object have an image?
191 192 193 |
# File 'lib/has_image.rb', line 191 def has_image? !send([:column]).blank? end |
- (Object) has_image_id
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 |
- (Object) height
250 251 252 |
# File 'lib/has_image.rb', line 250 def height self[:height] || storage.measure(absolute_path, :height) end |
- (Object) image_data 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 |
- (Object) image_data=(image_data) 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 |
- (Boolean) image_data_valid?
Is the image data a file that ImageMagick can process, and is it within the allowed minimum and maximum sizes?
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.[:image_too_big_message]) elsif storage.image_too_small? errors.add_to_base(self.class.[:image_too_small_message]) elsif !HasImage::Processor.valid?(storage.temp_file) errors.add_to_base(self.class.[:invalid_image_message]) end end |
- (Object) image_size
254 255 256 |
# File 'lib/has_image.rb', line 254 def image_size self[:image_size] || [width, height].join('x') end |
- (Object) install_images
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 |
- (Object) public_path(thumbnail = nil) 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 |
- (Object) regenerate_thumbnails! 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([:column])) end |
- (Object) remove_images
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([:column]).blank? || ![:delete] self.class.transaction do begin storage.remove_images(self, send([: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([:column])} = NULL" conditions = "#{connection.quote_column_name(self.class.primary_key)} = #{connection.quote(id)}" self.class.update_all(updates, conditions) self.send("#{[:column]}=", nil) end rescue Errno::ENOENT logger.warn("Could not delete files for #{self.class.to_s} #{to_param}") end end end |
- (Object) storage
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() end |
- (Object) update_images
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 |
- (Object) width
246 247 248 |
# File 'lib/has_image.rb', line 246 def width self[:width] || storage.measure(absolute_path, :width) end |