Module | Fleximage::Model::ClassMethods |
In: |
lib/fleximage/model.rb
|
Provides class methods for Fleximage for use in model classes. The only class method is acts_as_fleximage which integrates Fleximage functionality into a model class.
The following class level accessors also get inserted.
Example:
class Photo < ActiveRecord::Base acts_as_fleximage do image_directory 'public/images/uploaded' use_creation_date_based_directories true image_storage_format :png require_image true missing_image_message 'is required' invalid_image_message 'was not a readable image' default_image_path 'public/images/no_photo_yet.png' output_image_jpg_quality 85 preprocess_image do |image| image.resize '1024x768' end end # normal model methods... end
Internal method to ask this class if it stores image in the DB.
# File lib/fleximage/model.rb, line 85 85: def self.db_store? 86: columns.find do |col| 87: col.name == 'image_file_data' 88: end 89: end
Call this class method just like you would call operate in a view. The image transoformation in the provided block will be run on every uploaded image before its saved as the master image.
# File lib/fleximage/model.rb, line 80 80: def self.preprocess_image(&block) 81: preprocess_image_operation(block) 82: end
Use this method to include Fleximage functionality in your model. It takes an options hash with a single required key, :image_directory. This key should point to the directory you want your images stored on your server. Or configure with a nice looking block.
# File lib/fleximage/model.rb, line 71 71: def acts_as_fleximage(options = {}) 72: 73: # Insert class methods 74: class_eval do 75: include Fleximage::Model::InstanceMethods 76: 77: # Call this class method just like you would call +operate+ in a view. 78: # The image transoformation in the provided block will be run on every uploaded image before its saved as the 79: # master image. 80: def self.preprocess_image(&block) 81: preprocess_image_operation(block) 82: end 83: 84: # Internal method to ask this class if it stores image in the DB. 85: def self.db_store? 86: columns.find do |col| 87: col.name == 'image_file_data' 88: end 89: end 90: end 91: 92: # Where images get stored 93: dsl_accessor :image_directory 94: 95: # Put uploads from different days into different subdirectories 96: dsl_accessor :use_creation_date_based_directories, :default => true 97: 98: # The format are master images are stored in 99: dsl_accessor :image_storage_format, :default => Proc.new { :png } 100: 101: # Require a valid image. Defaults to true. Set to false if its ok to have no image for 102: dsl_accessor :require_image, :default => true 103: 104: # Missing image message 105: dsl_accessor :missing_image_message, :default => 'is required' 106: 107: # Invalid image message 108: dsl_accessor :invalid_image_message, :default => 'was not a readable image' 109: 110: # Sets the quality of rendered JPGs 111: dsl_accessor :output_image_jpg_quality, :default => 85 112: 113: # Set a default image to use when no image has been assigned to this record 114: dsl_accessor :default_image_path 115: 116: # A block that processes an image before it gets saved as the master image of a record. 117: # Can be helpful to resize potentially huge images to something more manageable. Set via 118: # the "preprocess_image { |image| ... }" class method. 119: dsl_accessor :preprocess_image_operation 120: 121: # Image related save and destroy callbacks 122: after_destroy :delete_image_file 123: before_save :pre_save 124: after_save :post_save 125: 126: # execute configuration block 127: yield if block_given? 128: 129: # set the image directory from passed options 130: image_directory options[:image_directory] if options[:image_directory] 131: 132: # Require the declaration of a master image storage directory 133: if !image_directory && !db_store? 134: raise "No place to put images! Declare this via the :image_directory => 'path/to/directory' option\n"+ 135: "Or add a database column named image_file_data for DB storage" 136: end 137: end