Class: UploadFileValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- UploadFileValidator
- 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
- #asset_available?(record, value) ⇒ Boolean
-
#initialize(options) ⇒ UploadFileValidator
constructor
A new instance of UploadFileValidator.
- #validate_each(record, attribute, value) ⇒ Object
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() super() @options = DEFAULT_OPTIONS.merge(@options) end |
Instance Method Details
#asset_available?(record, value) ⇒ 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 = [:less_then] content_type = [:content_type] return unless [:presence] record.errors[attribute] << 'must be provided' if [: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 |