Never been to ShareSnippets before?

ShareSnippets е публично хранилище за програмен код. Лесно можете да съберете собствена колекция от отрязъци от код, да ги систематизирате с етикети, ключови думи, и да ги споделите (или ако не искате, да ги запазите за себе си!)

Attachment_FU: Валидация (See related posts)

Модел:
has_attachment :content_type => :image, 
                   :storage => :file_system, 
                   :max_size => 100.kilobytes,
                   :resize_to => '320x240>',
                   :thumbnails => { :thumb => '100x100>' }
  
  validates_attachment  :content_type => "Файла който сте избрал е различен от JPEG, PNG или GIF",
                        :empty => 'Изберете файл',
                        :size => "Файла който сте избрал е с големина по-голяма от максималната позволена големина от 100 KB"

Добавете в:
RAILS_APP/vendor/plugins/attachment_fu/init.rb

Следният код:
Technoweenie::AttachmentFu::InstanceMethods.module_eval do
  protected
    def attachment_valid?
      if self.filename.nil?
        errors.add_to_base attachment_validation_options[:empty]
        return
      end
      [:content_type, :size].each do |option|
        if attachment_validation_options[option] && attachment_options[option] && !attachment_options[option].include?(self.send(option))
          errors.add_to_base attachment_validation_options[option]
        end
      end
    end
end

Technoweenie::AttachmentFu::ClassMethods.module_eval do
  # Options: 
  # *  <tt>:empty</tt> - Base error message when no file is uploaded. Default is "No file uploaded" 
  # *  <tt>:content_type</tt> - Base error message when the uploaded file is not a valid content type.
  # *  <tt>:size</tt> - Base error message when the uploaded file is not a valid size.
  #
  # Example:
  #   validates_attachment :content_type => "The file you uploaded was not a JPEG, PNG or GIF",
  #                        :size         => "The image you uploaded was larger than the maximum size of 10MB" 
  def validates_attachment(options={})
    options[:empty] ||= "No file uploaded" 
    class_inheritable_accessor :attachment_validation_options
    self.attachment_validation_options = options
    validate :attachment_valid?
  end

You need to create an account or log in to post comments to this site.


Related Posts