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.



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

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

#has_image?Boolean

Does the object have an image?

Returns:

  • (Boolean)


175
176
177
# File 'lib/has_image.rb', line 175

def has_image?
  !has_image_file.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.



259
260
261
# File 'lib/has_image.rb', line 259

def has_image_id
  id
end

#image_data=(image_data) ⇒ Object

Sets the uploaded image data. Image data can be an instance of Tempfile, or an instance of any class than inherits from IO.



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

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)


188
189
190
191
192
193
194
195
196
197
# File 'lib/has_image.rb', line 188

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

#install_imagesObject

Processes and installs the image and its thumbnails.



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

def install_images
  return if !storage.temp_file
  update_attribute(:has_image_file, storage.install_images(self))
end

#public_path(thumbnail = nil) ⇒ Object

Gets the “web path” for the image, or optionally, its thumbnail.



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

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

#regenerate_thumbnailsObject

Regenerates the thumbails from the main image.



211
212
213
# File 'lib/has_image.rb', line 211

def regenerate_thumbnails
  storage.regenerate_thumbnails(has_image_id, has_image_file)
end

#remove_imagesObject

Deletes the image from the storage.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/has_image.rb', line 216

def remove_images
  return if has_image_file.blank?
  self.class.transaction do
    begin
      storage.remove_images(self, has_image_file)
      # 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_file = NULL WHERE id = #{id}")          
        self.has_image_file = 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.



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

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.



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

def update_images
  return if storage.temp_file.blank?
  remove_images
  update_attribute(:has_image_file, storage.install_images(has_image_id))      
end