Class: BootstrapValidatorRails::Validators::Numericality

Inherits:
Object
  • Object
show all
Defined in:
lib/bootstrap_validator_rails/validators/numericality_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(record, method, validator) ⇒ Numericality

Returns a new instance of Numericality.



4
5
6
# File 'lib/bootstrap_validator_rails/validators/numericality_validator.rb', line 4

def initialize(record, method, validator)
  @record, @method, @validator = record, method, validator
end

Instance Method Details

#generate_dataObject



8
9
10
11
12
13
14
15
# File 'lib/bootstrap_validator_rails/validators/numericality_validator.rb', line 8

def generate_data
  data = {
    :bv_numeric_separator => '',
    :bv_numeric_separator_message => generate_message
  }
  
  data.merge(generate_options)
end

#generate_messageObject



17
18
19
# File 'lib/bootstrap_validator_rails/validators/numericality_validator.rb', line 17

def generate_message
  @record.errors.generate_message(@method, :presence, {default: 'should be a number'})
end

#generate_optionsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bootstrap_validator_rails/validators/numericality_validator.rb', line 21

def generate_options
  options = @validator.options

  data = {}

  if options[:greater_than].present?
    data[:bv_greaterthan_inclusive] = 'false'
    data[:bv_greaterthan_value] = options[:greater_than]
  end

  if options[:greater_than_or_equal_to].present?
    data[:bv_greaterthan_inclusive] = 'true'
    data[:bv_greaterthan_value] = options[:greater_than_or_equal_to]
  end

  if options[:less_than].present?
    data[:bv_lessthan_inclusive] = 'false'
    data[:bv_lessthan_value] = options[:less_than]
  end

  if options[:less_than_or_equal_to].present?
    data[:bv_lessthan_inclusive] = 'true'
    data[:bv_lessthan_value] = options[:less_than_or_equal_to]
  end
  data
end