Sha256: d9e5ac64ea0013d2a15b94301557aadc83f82a927f6a1b4d27570d3d3af914ba

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

class AttachmentSizeValidator < RailsUploads::Validators::Base
  
  # validates :prop, :attachment_size => { :in => 0..4.megabytes }
  
  def validate_each(record, attribute, value)
    if !has_default?(record, attribute) and value 
      if options.has_key? :in
        unless options[:in].include? value.size
          add_error record, attribute, 'attachment_size_in', :greater_than => options[:in].begin, :less_than => options[:in].end
        end          
      else    
        if options.has_key? :less_than and value.size > options[:less_than]
          add_error record, attribute, 'attachment_size_less_than', :less_than => options[:less_than]
        end         
        if options.has_key? :greater_than and value.size < options[:greater_than]
          add_error record, attribute, 'attachment_size_greater_than', :greater_than => options[:greater_than]
        end         
      end      
    end
  end

  protected

  def add_error(record, attribute, type, options)
    super(record, attribute, "errors.messages.#{type}", options)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_uploads-0.1.4 lib/rails_uploads/validators/attachment_size_validator.rb