Class: Bureaucrat::Fields::IntegerField
- Defined in:
- lib/bureaucrat/fields.rb
Direct Known Subclasses
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
- #default_error_messages ⇒ Object
-
#initialize(options = {}) ⇒ IntegerField
constructor
A new instance of IntegerField.
- #to_object(value) ⇒ Object
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(={}) @max_value = .delete(:max_value) @min_value = .delete(:min_value) super() 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_messages ⇒ Object
233 234 235 236 237 |
# File 'lib/bureaucrat/fields.rb', line 233 def 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([:invalid]) end end |