Module: HasImage
- Defined in:
- lib/has_image.rb,
lib/has_image/railtie.rb,
lib/has_image/storage.rb,
lib/has_image/version.rb,
lib/has_image/processor.rb,
lib/has_image/view_helpers.rb
Overview
HasImage
HasImage allows you to very easily attach images to a Rails model. For some more basic info on what it does, please see its project page on GitHub.
Install HasImage by using Ruby Gems:
sudo gem install has_image
To use HasImage in your project, you must add a varchar column to your model. By default, this column should be named “has_image_file,” though you may easily change this. For option defaults, see HasImage#default_options_for and ClassMethods#has_image.
Basic Examples
Uses all default options. This works, but is likely not what you need.
class Photo < ActiveRecord::Base
has_image
end
Resize the uploaded image to 800x800 and create a 150x150 thumbnail.
has_image :resize_to "800x800", :thumbnails => {:square => "150x150"}
Resize the image and set a max file size to 4 megabytes.
has_image :resize_to "100x150", :max_size => 4.megabytes
Some slightly more advanced examples
Localizing HasImage
has_image :invalid_image_message => "No se puede procesar la imagen."
Using has_image with Capistrano
When deploying using Capistrano, you will likely want to keep images under a “system” directory so that newly deployed versions have access to them:
has_image :resize_to => "150x150",
:thumbnails => {
:square => "75x75",
},
:base_path => File.join(Rails.root, 'public', 'system')
Testing with HasImage:
If you want your tests to actually run the image processing, you should make sure your tests write the image files somewhere outside your public directory. Add something like this to your config/environments/test.rb:
config.after_initialize do
MyClass.[:base_path] = File.join(RAILS_ROOT, "tmp")
end
If you want to stub out calls to has_image so that your tests don’t do the (slow) image processing, here’s an example using Test::Unit and Mocha:
def setup
Photo.any_instance.stubs(:image_data=).returns(true)
Photo.any_instance.stubs(:install_images).returns(true)
Photo.any_instance.stubs(:image_data_valid?).returns(true)
end
Defined Under Namespace
Modules: ClassMethods, ModelClassMethods, ModelInstanceMethods, ViewHelpers Classes: FileTooBigError, FileTooSmallError, InvalidGeometryError, Processor, ProcessorError, Railtie, Storage, StorageError
Constant Summary collapse
- VERSION =
"0.4.0.1"
Class Method Summary collapse
-
.default_options_for(klass) ⇒ Object
If you’re invoking this method, you need to pass in the class for which you want to get default options; this is used to determine the path where the images will be stored in the file system.
-
.enable ⇒ Object
Enables has_image functionality.
-
.included(base) ⇒ Object
:nodoc:.
Class Method Details
.default_options_for(klass) ⇒ Object
If you’re invoking this method, you need to pass in the class for which you want to get default options; this is used to determine the path where the images will be stored in the file system. Take a look at HasImage::ClassMethods#has_image to see examples of how to set the options in your model.
This method is called by your model when you call has_image. It’s placed here rather than in the model’s class methods to make it easier to access for testing. Unless you’re working on the code, it’s unlikely you’ll ever need to invoke this method.
-
:resize_to => “200x200”,
-
:thumbnails => {},
-
:auto_generate_thumbnails => true,
-
:delete => true,
-
:max_size => 12.megabytes,
-
:min_size => 4.kilobytes,
-
:path_prefix => klass.table_name,
-
:base_path => File.join(RAILS_ROOT, ‘public’),
-
:column => :has_image_file,
-
:convert_to => “JPEG”,
-
:output_quality => “85”,
-
:invalid_image_message => “Can’t process the image.”,
-
:image_too_small_message => “The image is too small.”,
-
:image_too_big_message => “The image is too big.”
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/has_image.rb', line 132 def (klass) { :resize_to => "200x200", :thumbnails => {}, :auto_generate_thumbnails => true, :delete => true, :max_size => 12.megabytes, :min_size => 4.kilobytes, :path_prefix => klass.table_name, :base_path => File.join(RAILS_ROOT, 'public'), :column => :has_image_file, :convert_to => "JPEG", :output_quality => "85", :invalid_image_message => "Can't process the image.", :image_too_small_message => "The image is too small.", :image_too_big_message => "The image is too big." } end |
.enable ⇒ Object
Enables has_image functionality. You probably don’t need to ever invoke this.
101 102 103 104 105 |
# File 'lib/has_image.rb', line 101 def enable # :nodoc: return if ActiveRecord::Base.respond_to? :has_image ActiveRecord::Base.send(:include, HasImage) ActionView::Base.send(:include, ViewHelpers) end |
.included(base) ⇒ Object
:nodoc:
95 96 97 |
# File 'lib/has_image.rb', line 95 def included(base) # :nodoc: base.extend(ClassMethods) end |