Module: HasImage::ClassMethods

Defined in:
lib/has_image.rb

Instance Method Summary collapse

Instance Method Details

#has_image(options = {}) ⇒ Object

To use HasImage with a Rails model, all you have to do is add a column named “has_image_file.” For configuration defaults, you might want to take a look at the default options specified in HasImage#default_options_for. The different setting options are described below.

Options:

  • :resize_to - Dimensions to resize to. This should be an ImageMagick geometry string. Fixed sizes are recommended.

  • :thumbnails - A hash of thumbnail names and dimensions. The dimensions should be ImageMagick geometry strings. Fixed sized are recommended.

  • :min_size - Minimum file size allowed. It’s recommended that you set this size in kilobytes.

  • :max_size - Maximum file size allowed. It’s recommended that you set this size in megabytes.

  • :base_path - Where to install the images. You should probably leave this alone, except for tests.

  • :path_prefix - Where to install the images, relative to basepath. You should probably leave this alone.

  • :convert_to - An ImageMagick format to convert images to. Recommended formats: JPEG, PNG, GIF.

  • :output_quality - Image output quality passed to ImageMagick.

  • :invalid_image_message - The message that will be shown when the image data can’t be processed.

  • :image_too_small_message - The message that will be shown when the image file is too small. You should ideally set this to something that tells the user what the minimum is.

  • :image_too_big_message - The message that will be shown when the image file is too big. You should ideally set this to something that tells the user what the maximum is.

Examples:

has_image # uses all default options
has_image :resize_to "800x800", :thumbnails => {:square => "150x150"}
has_image :resize_to "100x150", :max_size => 500.kilobytes
has_image :invalid_image_message => "No se puede procesar la imagen."


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/has_image.rb', line 151

def has_image(options = {})
  options.assert_valid_keys(:resize_to, :thumbnails, :max_size, :min_size,
    :path_prefix, :base_path, :convert_to, :output_quality,
    :invalid_image_message, :image_too_big_message, :image_too_small_message)
  options = HasImage.default_options_for(self).merge(options)
  class_inheritable_accessor :has_image_options
  write_inheritable_attribute(:has_image_options, options)
  
  after_create :install_images
  after_save :update_images
  after_destroy :remove_images
  
  validate_on_create :image_data_valid?
  
  include ModelInstanceMethods
  extend  ModelClassMethods

end