Class: Bureaucrat::Fields::FileField

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

Overview

TODO: rewrite

Instance Attribute Summary

Attributes inherited from Field

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

Instance Method Summary (collapse)

Methods inherited from Field

#default_hidden_widget, #default_validators, #initialize_copy, #populate_object, #prepare_value, #run_validators, #validate, #widget_attrs

Constructor Details

- (FileField) initialize(options)

A new instance of FileField



388
389
390
391
392
# File 'lib/bureaucrat/fields.rb', line 388

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

Instance Method Details

- (Object) bound_data(data, initial)



465
466
467
468
469
470
471
# File 'lib/bureaucrat/fields.rb', line 465

def bound_data(data, initial)
  if data.nil? || data.object_id == Widgets::ClearableFileInput::FILE_INPUT_CONTRADICTION.object_id
    initial
  else
    data
  end
end

- (Object) clean(data, initial = nil)



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/bureaucrat/fields.rb', line 437

def clean(data, initial = nil)
  # If the widget got contradictory inputs, we raise a validation error
  if data.object_id ==  Widgets::ClearableFileInput::FILE_INPUT_CONTRADICTION.object_id
    raise ValidationError.new(error_messages[:contradiction])
  end

  # False means the field value should be cleared; further validation is
  # not needed.
  if data == false
    unless @required
      return false
    end

    # If the field is required, clearing is not possible (the widget
    # shouldn't return false data in that case anyway). false is not
    # an 'empty_value'; if a false value makes it this far
    # it should be validated from here on out as nil (so it will be
    # caught by the required check).
    data = nil
  end

  if !data && initial
    initial
  else
    super(data)
  end
end

- (Object) default_error_messages



394
395
396
397
398
399
400
# File 'lib/bureaucrat/fields.rb', line 394

def default_error_messages
  super.merge(:invalid => 'No file was submitted. Check the encoding type on the form.',
              :missing => 'No file was submitted.',
              :empty => 'The submitted file is empty.',
              :max_length => 'Ensure this filename has at most %(max)d characters (it has %(length)d).',
              :contradiction => 'Please either submit a file or check the clear checkbox, not both.')
end

- (Object) default_widget



402
403
404
# File 'lib/bureaucrat/fields.rb', line 402

def default_widget
  Widgets::ClearableFileInput
end

- (Object) to_object(data)



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/bureaucrat/fields.rb', line 406

def to_object(data)
  if Validators.empty_value?(data)
    return nil
  end

  # UploadedFile objects should have name and size attributes.
  begin
    file_name = data.name
    file_size = data.size
  rescue NoMethodError
    raise ValidationError.new(error_messages[:invalid])
  end

  if @max_length && file_name.length > @max_length
    msg = Utils.format_string(error_messages[:max_length],
                              :max => @max_length,
                              :length => file_name.length)
    raise ValidationError.new(msg)
  end

  if Utils.blank_value?(file_name)
    raise ValidationError.new(error_messages[:invalid])
  end

  if !@allow_empty_file && !file_size
    raise ValidationError.new(error_messages[:empty])
  end

  data
end