Class: UploadFileValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/paperclip_i18n/upload_file_validator.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ :content_type => nil,
:less_then => nil,
:presence => true }

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UploadFileValidator

Returns a new instance of UploadFileValidator.



6
7
8
9
# File 'lib/paperclip_i18n/upload_file_validator.rb', line 6

def initialize(options)
  super(options)
  @options = DEFAULT_OPTIONS.merge(@options)
end

Instance Method Details

#asset_available?(record, value) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/paperclip_i18n/upload_file_validator.rb', line 23

def asset_available?(record, value)
  !record.assets.i18ns.first.nil? || !value.nil?
end

#validate_each(record, attribute, value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/paperclip_i18n/upload_file_validator.rb', line 11

def validate_each(record, attribute, value)
  less_then = options[:less_then]
  content_type = options[:content_type]
  return unless options[:presence]
  record.errors[attribute] << 'must be provided' if options[:presence] and !asset_available?(record, value)
  
  if !value.nil?
    record.errors[attribute] << "must be smaller then #{less_then}" if not less_then.nil? and value.size > less_then
    record.errors[attribute] << "must be of a file valid type: #{content_type}" if not content_type.nil? and not value.content_type =~ content_type
  end
end