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

#initialize(options) ⇒ FileField

Returns a new instance of FileField.



391
392
393
394
395
# File 'lib/bureaucrat/fields.rb', line 391

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

Instance Method Details

#bound_data(data, initial) ⇒ Object



468
469
470
471
472
473
474
# File 'lib/bureaucrat/fields.rb', line 468

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

#clean(data, initial = nil) ⇒ Object



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

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

#default_error_messagesObject



397
398
399
400
401
402
403
# File 'lib/bureaucrat/fields.rb', line 397

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

#default_widgetObject



405
406
407
# File 'lib/bureaucrat/fields.rb', line 405

def default_widget
  Widgets::ClearableFileInput
end

#to_object(data) ⇒ Object



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
436
437
438
# File 'lib/bureaucrat/fields.rb', line 409

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