Class: Bureaucrat::Fields::FileField
- Inherits:
-
Field
- Object
- Field
- Bureaucrat::Fields::FileField
show all
- Defined in:
- lib/bureaucrat/fields.rb
Overview
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
#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 data.object_id == Widgets::ClearableFileInput::FILE_INPUT_CONTRADICTION.object_id
raise ValidationError.new(error_messages[:contradiction])
end
if data == false
unless @required
return false
end
data = nil
end
if !data && initial
initial
else
super(data)
end
end
|
#default_error_messages ⇒ Object
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
|
#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
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
|