lib/fleximage/model.rb
# 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.
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.
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 .
def acts_as_fleximage(options = {})
        include Fleximage::Model::InstanceMethods
        def self.preprocess_image(&block)
          preprocess_image_operation(block)
        end
        def self.db_store?
          return false if s3_store?
          if respond_to?(:columns)
            columns.find do |col|
              col.name == 'image_file_data'
            end
          else
            false
          end
        end
        def self.s3_store?
          !!s3_bucket
        end
        def self.file_store?
          !db_store? && !s3_store?
        end
        def self.has_store?
          respond_to?(:columns) && (db_store? || s3_store? || image_directory)
        end
        validate :validate_image if respond_to?(:validate)
        attr_reader :image_file_temp
        attr_accessor :jpg_compression_quality
        dsl_accessor :image_directory
        dsl_accessor :s3_bucket
        dsl_accessor :use_creation_date_based_directories, :default => true
        dsl_accessor :image_storage_format, :default => Proc.new { :png }
        dsl_accessor :require_image, :default => true
        def self.translate_error_message(name, fallback, options = {})
          translation = I18n.translate "activerecord.errors.models.#{self.model_name.underscore}.#{name}", options
          if translation.match /translation missing:/
            I18n.translate "activerecord.errors.messages.#{name}", options.merge({ :default => fallback })
          end
        end
        def self.missing_image_message(str = nil)
          if str.nil?
            if @missing_image_message
              @missing_image_message
            else
              translate_error_message("missing_image", "is required")
            end
          else
            @missing_image_message = str
          end
        end
        def self.invalid_image_message(str = nil)
          if str.nil?
            if @invalid_image_message
              @invalid_image_message
            else
              translate_error_message("invalid_image", "was not a readable image")
            end
          else
            @invalid_image_message = str
          end
        end
        def self.image_too_small_message(str = nil)
          fb = "is too small (Minimum: {{minimum}})"
          if str.nil?
            minimum_size = Fleximage::Operator::Base.size_to_xy(validates_image_size).join('x')
            if @image_too_small_message
              @image_too_small_message.gsub("{{minimum}}", minimum_size)
            else
              translate_error_message("image_too_small", fb.gsub("{{minimum}}", minimum_size), :minimum => minimum_size)
            end
          else
            @image_too_small_message = str
          end
        end
        dsl_accessor :output_image_jpg_quality, :default => 85
        dsl_accessor :default_image_path
        dsl_accessor :default_image
        dsl_accessor :preprocess_image_operation
        dsl_accessor :validates_image_size
        if respond_to?(:before_save)
          after_destroy :delete_image_file
          before_save   :pre_save
          after_save    :post_save
        end
        yield if block_given?
        if s3_bucket
          begin
            AWS::S3::Bucket.find(s3_bucket)
          rescue AWS::S3::NoSuchBucket
            AWS::S3::Bucket.create(s3_bucket)
          end
        end
        image_directory options[:image_directory] if options[:image_directory]
        if respond_to?(:validate) && !image_directory && !db_store? && !s3_store? && !default_image && !default_image_path
          raise "No place to put images!  Declare this via the :image_directory => 'path/to/directory' option\n"+
                "Or add a database column named image_file_data for DB storage\n"+
                "Or set :virtual to true if this class has no image store at all\n"+
                "Or set a default image to show with :default_image or :default_image_path"
        end
      end
def acts_as_fleximage ( options = { } ) include Fleximage :: Model :: InstanceMethods def self . preprocess_image ( & block ) preprocess_image_operation ( block ) end def self . db_store? return false if s3_store? if respond_to? ( :columns ) columns . find do | col | col . name == 'image_file_data' end else false end end def self . s3_store? ! ! s3_bucket end def self . file_store? ! db_store? && ! s3_store? end def self . has_store? respond_to? ( :columns ) && ( db_store? || s3_store? || image_directory ) end validate :validate_image if respond_to? ( :validate ) attr_reader :image_file_temp attr_accessor :jpg_compression_quality dsl_accessor :image_directory dsl_accessor :s3_bucket dsl_accessor :use_creation_date_based_directories , :default => true dsl_accessor :image_storage_format , :default => Proc . new { :png } dsl_accessor :require_image , :default => true def self . translate_error_message ( name , fallback , options = { } ) translation = I18n . translate "activerecord.errors.models.#{self.model_name.underscore}.#{name}" , options if translation . match / translation missing: / I18n . translate "activerecord.errors.messages.#{name}" , options . merge ( { :default => fallback } ) end end def self . missing_image_message ( str = nil ) if str . nil? if @missing_image_message @missing_image_message else translate_error_message ( "missing_image" , "is required" ) end else @missing_image_message = str end end def self . invalid_image_message ( str = nil ) if str . nil? if @invalid_image_message @invalid_image_message else translate_error_message ( "invalid_image" , "was not a readable image" ) end else @invalid_image_message = str end end def self . image_too_small_message ( str = nil ) fb = "is too small (Minimum: {{minimum}})" if str . nil? minimum_size = Fleximage :: Operator :: Base . size_to_xy ( validates_image_size ) . join ( 'x' ) if @image_too_small_message @image_too_small_message . gsub ( "{{minimum}}" , minimum_size ) else translate_error_message ( "image_too_small" , fb . gsub ( "{{minimum}}" , minimum_size ) , :minimum => minimum_size ) end else @image_too_small_message = str end end dsl_accessor :output_image_jpg_quality , :default => 85 dsl_accessor :default_image_path dsl_accessor :default_image dsl_accessor :preprocess_image_operation dsl_accessor :validates_image_size if respond_to? ( :before_save ) after_destroy :delete_image_file before_save :pre_save after_save :post_save end yield if block_given? if s3_bucket begin AWS :: S3 :: Bucket . find ( s3_bucket ) rescue AWS :: S3 :: NoSuchBucket AWS :: S3 :: Bucket . create ( s3_bucket ) end end image_directory options [ :image_directory ] if options [ :image_directory ] if respond_to? ( :validate ) && ! image_directory && ! db_store? && ! s3_store? && ! default_image && ! default_image_path raise "No place to put images!  Declare this via the :image_directory => 'path/to/directory' option\n" + "Or add a database column named image_file_data for DB storage\n" + "Or set :virtual to true if this class has no image store at all\n" + "Or set a default image to show with :default_image or :default_image_path" end end
Use this method to include Fleximage functionality in your model.
Use this method to include Fleximage functionality in your model .
# Include the necesary instance methods # 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. # Internal method to ask this class if it stores image in the DB. # validation callback # The filename of the temp image.  Used for storing of good images when validation fails # and the form needs to be redisplayed. # Setter for jpg compression quality at the instance level # Where images get stored # Amazon S3 bucket where the master images are stored # Put uploads from different days into different subdirectories # The format are master images are stored in # Require a valid image.  Defaults to true.  Set to false if its ok to have no image for # Missing image message #dsl_accessor :missing_image_message, :default => 'is required' # Invalid image message #dsl_accessor :invalid_image_message, :default => 'was not a readable image' # Image too small message # Should include {{minimum}} # Sets the quality of rendered JPGs # Set a default image to use when no image has been assigned to this record # Set a default image based on a a size and fill # A block that processes an image before it gets saved as the master image of a record. # Can be helpful to resize potentially huge images to something more manageable. Set via # the "preprocess_image { |image| ... }" class method. # Set a minimum size ([x, y] e.g. 200, '800x600', [800, 600]) # Set '0x600' to just enforce y size or # '800x0' to just validate x size. # Image related save and destroy callbacks # execute configuration block # Create S3 bucket if it's not present # set the image directory from passed options # Require the declaration of a master image storage directory
options
<not_specific>