Class: SexyValidations::Validators::File
- Inherits:
-
Object
- Object
- SexyValidations::Validators::File
- Defined in:
- lib/sexy_validations/validators/file.rb
Class Method Summary collapse
- .file_from_value(value) ⇒ Object
- .humanized_size(num) ⇒ Object
- .validate(model, attribute, value, options) ⇒ Object
Class Method Details
.file_from_value(value) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/sexy_validations/validators/file.rb', line 12 def self.file_from_value(value) case value.class.name when "File", "Tempfile" value when "Sequel::Plugins::Paperclip::Attachment" value.queued_file else raise ArgumentError, "#{value} is not a valid input" end end |
.humanized_size(num) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/sexy_validations/validators/file.rb', line 5 def self.humanized_size(num) for x in ['Byte','KB','MB','GB','TB'] return "%d %s"%[num, x] if num < 1024.0 num /= 1024.0 end end |
.validate(model, attribute, value, options) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sexy_validations/validators/file.rb', line 23 def self.validate(model, attribute, value, ) return unless value unless .is_a?(Hash) = { :size => , } end file = file_from_value(value) return unless file if [:size] min = [:size].min if file.size < min model.errors.add(attribute, "zu klein (mindestes #{humanized_size(min)})") end max = [:size].max if file.size > max model.errors.add(attribute, "zu groß (maximal #{humanized_size(max)})") end end end |