Class: Bureaucrat::Fields::BigDecimalField

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

Constant Summary

Constants included from Validation::Validators

Validation::Validators::EMAIL_RE

Instance Attribute Summary

Attributes inherited from Field

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

Instance Method Summary collapse

Methods inherited from Field

hidden_widget, inherited, #initialize_copy, set_error, #validating, widget, #widget_attrs

Methods included from Validation::Converters

to_big_decimal, to_bool, to_float, to_integer

Methods included from Validation::Validates

fail_with

Methods included from Validation::Validators

empty_value?, has_max_decimal_places, has_max_digits, has_max_length, has_max_whole_digits, has_min_length, included_in, is_array, is_email, is_not_greater_than, is_not_lesser_than, is_present, is_true, matches_regex, not_empty

Constructor Details

#initialize(options = {}) ⇒ BigDecimalField

Returns a new instance of BigDecimalField.



228
229
230
231
232
233
234
235
# File 'lib/bureaucrat/fields.rb', line 228

def initialize(options={})
  @max_value = options.delete(:max_value)
  @min_value = options.delete(:min_value)
  @max_digits = options.delete(:max_digits)
  @max_decimal_places = options.delete(:max_decimal_places)
  @whole_digits = @max_digits - @decimal_places if @max_digits && @decimal_places
  super(options)
end

Instance Method Details

#clean(value) ⇒ Object



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

def clean(value)
  super(value)
  return nil if !@required && empty_value?(value)

  validating do
      value = to_big_decimal(value.to_s.strip)
      is_not_greater_than(value, @max_value) if @max_value
      is_not_lesser_than(value, @min_value) if @min_value
      has_max_digits(value, @max_digits) if @max_digits
      has_max_decimal_places(value, @max_decimal_places) if @max_decimal_places
      has_max_whole_digits(value, @max_whole_digits) if @max_whole_digits
    end

  value
end