Class: Bureaucrat::Fields::FileField

Inherits:
Field
  • Object
show all
Defined in:
lib/bureaucrat/fields.rb

Overview

TODO: add tests

Constant Summary

Constants included from Validation::Validators

Validation::Validators::EMAIL_RE

Instance Attribute Summary

Attributes inherited from Field

#error_messages, #help_text, #hidden_widget, #initial, #label, #required, #show_hidden_initial, #widget

Instance Method Summary collapse

Methods inherited from Field

hidden_widget, inherited, #initialize_copy, set_error, #validating, widget, #widget_attrs

Methods included from Validation::Converters

to_big_decimal, to_bool, to_float, to_integer

Methods included from Validation::Validates

fail_with

Methods included from Validation::Validators

empty_value?, has_max_decimal_places, has_max_digits, has_max_length, has_max_whole_digits, has_min_length, included_in, is_array, is_email, is_not_greater_than, is_not_lesser_than, is_present, is_true, matches_regex, not_empty

Constructor Details

#initialize(options) ⇒ FileField

Returns a new instance of FileField.



300
301
302
303
# File 'lib/bureaucrat/fields.rb', line 300

def initialize(options)
  @max_length = options.delete(:max_length)
  super(options)
end

Instance Method Details

#clean(data, initial = nil) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/bureaucrat/fields.rb', line 305

def clean(data, initial=nil)
  super(initial || data)

  if !required && empty_value?(data)
    return nil
  elsif !data && initial
    return initial
  end

  # TODO: file validators?
  validating do
      # UploadedFile objects should have name and size attributes.
      begin
        file_name = data.name
        file_size = data.size
      rescue NoMethodError
        fail_with(:invalid)
      end

      fail_with(:max_length, :max => @max_length, :length => file_name.length) if
        @max_length && file_name.length > @max_length

      fail_with(:invalid) unless file_name
      fail_with(:empty) unless file_size || file_size == 0
    end

  data
end