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.

  • image_directory: (String, no default) Where the master images are stored, directory path relative to your app root.
  • use_creation_date_based_directories: (Boolean, default true) If true, master images will be stored in directories based on creation date. For example: "#{image_directory}/2007/11/24/123.png" for an image with an id of 123 and a creation date of November 24, 2007. Turing this off would cause the path to be "#{image_directory}/123.png" instead. This helps keep the OS from having directories that are too full.
  • image_storage_format: (:png or :jpg, default :png) The format of your master images. Using :png will give you the best quality, since the master images as stored as lossless version of the original upload. :jpg will apply lossy compression, but the master image file sizes will be much smaller. If storage space is a concern, us :jpg.
  • require_image: (Boolean, default true) The model will raise a validation error if no image is uploaded with the record. Setting to false allows record to be saved with no images.
  • missing_image_message: (String, default "is required") Validation message to display when no image was uploaded for a record.
  • invalid_image_message: (String default "was not a readable image") Validation message when an image is uploaded, but is not an image format that can be read by RMagick.
  • output_image_jpg_quality: (Integer, default 85) When rendering JPGs, this represents the amount of compression. Valid values are 0-100, where 0 is very small and very ugly, and 100 is near lossless but very large in filesize.
  • default_image_path: (String, nil default) If no image is present for this record, the image at this path will be used instead. Useful for a placeholder graphic for new content that may not have an image just yet.
  • preprocess_image: (Block, no default) 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.

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

Methods

Included Modules

Fleximage::Model::InstanceMethods

Public Class methods

Internal method to ask this class if it stores image in the DB.

[Source]

    # 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.

[Source]

    # File lib/fleximage/model.rb, line 80
80:           def self.preprocess_image(&block)
81:             preprocess_image_operation(block)
82:           end

Public Instance methods

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.

[Source]

     # 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

[Validate]