Module: HasImage::ModelInstanceMethods
- Defined in:
- lib/has_image.rb
Instance Method Summary collapse
-
#absolute_path(thumbnail = nil) ⇒ Object
Gets the absolute filesystem path for the image, or optionally, its thumbnail.
- #generate_thumbnail!(thumb_name) ⇒ Object
-
#has_image? ⇒ Boolean
Does the object have an image?.
-
#has_image_id ⇒ Object
By default, just returns the model’s id.
- #height ⇒ Object
-
#image_data ⇒ Object
(also: #uploaded_data)
nil placeholder in case this field is used in a form.
-
#image_data=(image_data) ⇒ Object
(also: #uploaded_data=)
Sets the uploaded image data.
-
#image_data_valid? ⇒ Boolean
Is the image data a file that ImageMagick can process, and is it within the allowed minimum and maximum sizes?.
- #image_size ⇒ Object
-
#install_images ⇒ Object
Processes and installs the image and its thumbnails.
-
#public_path(thumbnail = nil) ⇒ Object
(also: #public_filename)
Gets the “web path” for the image, or optionally, its thumbnail.
-
#regenerate_thumbnails! ⇒ Object
(also: #regenerate_thumbnails)
Regenerates the thumbails from the main image.
-
#remove_images ⇒ Object
Deletes the image from the storage.
-
#storage ⇒ Object
Gets an instance of the underlying storage functionality.
-
#update_images ⇒ Object
Creates new images and removes the old ones when image_data has been set.
- #width ⇒ Object
Instance Method Details
#absolute_path(thumbnail = nil) ⇒ Object
Gets the absolute filesystem path for the image, or optionally, its thumbnail.
233 234 235 |
# File 'lib/has_image.rb', line 233 def absolute_path(thumbnail = nil) storage.filesystem_path_for(self, thumbnail) end |
#generate_thumbnail!(thumb_name) ⇒ Object
243 244 245 |
# File 'lib/has_image.rb', line 243 def generate_thumbnail!(thumb_name) storage.generate_thumbnail(has_image_id, send([:column]), thumb_name) end |
#has_image? ⇒ Boolean
Does the object have an image?
192 193 194 |
# File 'lib/has_image.rb', line 192 def has_image? !send([:column]).blank? end |
#has_image_id ⇒ Object
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.
305 306 307 |
# File 'lib/has_image.rb', line 305 def has_image_id id end |
#height ⇒ Object
251 252 253 |
# File 'lib/has_image.rb', line 251 def height self[:height] || storage.measure(absolute_path, :height) end |
#image_data ⇒ Object 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
206 207 208 |
# File 'lib/has_image.rb', line 206 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
199 200 201 202 |
# File 'lib/has_image.rb', line 199 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?
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/has_image.rb', line 213 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 |
#image_size ⇒ Object
255 256 257 |
# File 'lib/has_image.rb', line 255 def image_size self[:image_size] || [width, height].join('x') end |
#install_images ⇒ Object
Processes and installs the image and its thumbnails.
289 290 291 292 |
# File 'lib/has_image.rb', line 289 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
226 227 228 |
# File 'lib/has_image.rb', line 226 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.
238 239 240 |
# File 'lib/has_image.rb', line 238 def regenerate_thumbnails! storage.generate_thumbnails(has_image_id, send([:column])) end |
#remove_images ⇒ Object
Deletes the image from the storage.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/has_image.rb', line 260 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 |
#storage ⇒ Object
Gets an instance of the underlying storage functionality. See HasImage::Storage.
296 297 298 |
# File 'lib/has_image.rb', line 296 def storage @storage ||= HasImage::Storage.new() end |
#update_images ⇒ Object
Creates new images and removes the old ones when image_data has been set.
282 283 284 285 286 |
# File 'lib/has_image.rb', line 282 def update_images return if storage.temp_file.blank? remove_images populate_attributes end |
#width ⇒ Object
247 248 249 |
# File 'lib/has_image.rb', line 247 def width self[:width] || storage.measure(absolute_path, :width) end |