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.
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([:column]), thumb_name) end |
#has_image? ⇒ Boolean
Does the object have an image?
191 192 193 |
# File 'lib/has_image.rb', line 191 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.
304 305 306 |
# File 'lib/has_image.rb', line 304 def has_image_id id end |
#height ⇒ Object
250 251 252 |
# File 'lib/has_image.rb', line 250 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
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?
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 |
#image_size ⇒ Object
254 255 256 |
# File 'lib/has_image.rb', line 254 def image_size self[:image_size] || [width, height].join('x') end |
#install_images ⇒ Object
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([:column])) end |
#remove_images ⇒ Object
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 |
#storage ⇒ Object
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 |
#update_images ⇒ Object
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 |
#width ⇒ Object
246 247 248 |
# File 'lib/has_image.rb', line 246 def width self[:width] || storage.measure(absolute_path, :width) end |