Class: Bureaucrat::Fields::IntegerField

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

Direct Known Subclasses

FloatField

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

#bound_data, #clean, #default_hidden_widget, #default_validators, #default_widget, #initialize_copy, #populate_object, #prepare_value, #run_validators, #validate, #widget_attrs

Constructor Details

#initialize(options = {}) ⇒ IntegerField

Returns a new instance of IntegerField.



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/bureaucrat/fields.rb', line 219

def initialize(options={})
  @max_value = options.delete(:max_value)
  @min_value = options.delete(:min_value)
  super(options)

  if @min_value
    validators << Validators::MinValueValidator.new(@min_value)
  end

  if @max_value
    validators << Validators::MaxValueValidator.new(@max_value)
  end
end

Instance Method Details

#default_error_messagesObject



233
234
235
236
237
# File 'lib/bureaucrat/fields.rb', line 233

def default_error_messages
  super.merge(invalid: 'Enter a whole number.',
              max_value: 'Ensure this value is less than or equal to %(max)s.',
              min_value: 'Ensure this value is greater than or equal to %(min)s.')
end

#to_object(value) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/bureaucrat/fields.rb', line 239

def to_object(value)
  value = super(value)

  if Validators.empty_value?(value)
    return nil
  end

  begin
    Integer(value.to_s)
  rescue ArgumentError
    raise ValidationError.new(error_messages[:invalid])
  end
end