Module | Fleximage::Model::InstanceMethods |
In: |
lib/fleximage/model.rb
|
Provides methods that every model instance that acts_as_fleximage needs.
Delete the image file for this record. This is automatically ran after this record gets destroyed, but you can call it manually if you want to remove the image from the record.
# File lib/fleximage/model.rb, line 327 327: def delete_image_file 328: File.delete(file_path) if File.exists?(file_path) 329: end
Returns the path to the master image file for this record.
@some_image.directory_path #=> /var/www/myapp/uploaded_images
If this model has a created_at field, it will use a directory structure based on the creation date, to prevent hitting the OS imposed limit on the number files in a directory.
@some_image.directory_path #=> /var/www/myapp/uploaded_images/2008/3/30
# File lib/fleximage/model.rb, line 152 152: def directory_path 153: raise 'No image directory was defined, cannot generate path' unless self.class.image_directory 154: 155: # base directory 156: directory = "#{RAILS_ROOT}/#{self.class.image_directory}" 157: 158: # specific creation date based directory suffix. 159: creation = self[:created_at] || self[:created_on] 160: if self.class.use_creation_date_based_directories && creation 161: "#{directory}/#{creation.year}/#{creation.month}/#{creation.day}" 162: else 163: directory 164: end 165: end
Returns the path to the master image file for this record.
@some_image.file_path #=> /var/www/myapp/uploaded_images/123.png
# File lib/fleximage/model.rb, line 170 170: def file_path 171: "#{directory_path}/#{id}.#{self.class.image_storage_format}" 172: end
Return true if this record has an image.
# File lib/fleximage/model.rb, line 264 264: def has_image? 265: self.class.db_store? ? image_file_data : File.exists?(file_path) 266: end
Sets the image file for this record to an uploaded file. This can be called directly, or passively like from an ActiveRecord mass assignment.
Rails will automatically call this method for you, in most of the situations you would expect it to.
# via mass assignment, the most common form you'll probably use Photo.new(params[:photo]) Photo.create(params[:photo]) # via explicit assignment hash Photo.new(:image_file => params[:photo][:image_file]) Photo.create(:image_file => params[:photo][:image_file]) # Direct Assignment, usually not needed photo = Photo.new photo.image_file = params[:photo][:image_file] # via an association proxy p = Product.find(1) p.images.create(params[:photo])
# File lib/fleximage/model.rb, line 196 196: def image_file=(file) 197: # Get the size of the file. file.size works for form-uploaded images, file.stat.size works 198: # for file object created by File.open('foo.jpg', 'rb') 199: file_size = file.respond_to?(:size) ? file.size : file.stat.size 200: 201: if file.respond_to?(:read) && file_size > 0 202: # Create RMagick Image object from uploaded file 203: if file.path 204: @uploaded_image = Magick::Image.read(file.path).first 205: else 206: @uploaded_image = Magick::Image.from_blob(file.read).first 207: end 208: 209: set_magic_attributes(file) 210: 211: # Success, make sure everything is valid 212: @missing_image = false 213: @invalid_image = false 214: else 215: if self.class.require_image && !@uploaded_image 216: @missing_image = true 217: end 218: end 219: rescue Magick::ImageMagickError => e 220: if e.to_s =~ /no decode delegate for this image format/ 221: @invalid_image = true 222: else 223: raise e 224: end 225: end
Return the @image_file_url that was previously assigned. This is not saved in the database, and only exists to make forms happy.
# File lib/fleximage/model.rb, line 259 259: def image_file_url 260: @image_file_url 261: end
Assign the image via a URL, which will make the plugin go and fetch the image at the provided URL. The image will be stored locally as a master image for that record from then on. This is intended to be used along side the image upload to allow people the choice to upload from their local machine, or pull from the internet.
@photo.image_file_url = 'http://foo.com/bar.jpg'
# File lib/fleximage/model.rb, line 234 234: def image_file_url=(file_url) 235: @image_file_url = file_url 236: if file_url =~ %r{^https?://} 237: file = open(file_url) 238: 239: # Force a URL based file to have an original_filename 240: eval "class << file\ndef original_filename\n\"\#{file_url}\"\nend\nend\n" 241: 242: self.image_file = file 243: elsif file_url.empty? 244: @missing_image = true unless @uploaded_image 245: else 246: @invalid_image = true 247: end 248: end
Call from a .flexi view template. This enables the rendering of operators so that you can transform your image. This is the method that is the foundation of .flexi views. Every view should consist of image manipulation code inside a block passed to this method.
# app/views/photos/thumb.jpg.flexi @photo.operate do |image| image.resize '320x240' end
# File lib/fleximage/model.rb, line 277 277: def operate(&block) 278: returning self do 279: proxy = ImageProxy.new(load_image, self) 280: block.call(proxy) 281: @output_image = proxy.image 282: end 283: end