Class: BootstrapValidatorRails::Validators::Numericality

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

Instance Method Summary collapse

Methods inherited from Validator

#initialize, #unsupported?, #validator_options

Constructor Details

This class inherits a constructor from BootstrapValidatorRails::Validators::Validator

Instance Method Details

#generate_dataObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/bootstrap_validator_rails/validators/numericality_validator.rb', line 4

def generate_data
  data = {}
  return data if unsupported?
  
  options = validator_options

  data[:bv_numeric] = 'true'
  data[:bv_numeric_separator] = '.'
  
  data.merge(generate_options)
end

#generate_messageObject



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

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

#generate_objectObject



20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bootstrap_validator_rails/validators/numericality_validator.rb', line 20

def generate_object
  options = @validator.options

  data = {}

  if options[:only_integer].present?
    data["integer"] = {}
    data["integer"]['message'] = 'should be a number'
  end

  if options[:greater_than].present?
    data["greaterThan"] = {}
    data["greaterThan"]["inclusive"] = false
    data["greaterThan"]["value"] = options[:greater_than]
  end

  if options[:greater_than_or_equal_to].present?
    data["greaterThan"] = {}
    data["greaterThan"]["inclusive"] = true
    data["greaterThan"]["value"] = options[:greater_than_or_equal_to]
  end

  if options[:less_than].present?
    data["lessThan"] = {}
    data["lessThan"]["inclusive"] = false
    data["lessThan"]["value"] = options[:less_than]
  end

  if options[:less_than_or_equal_to].present?
    data["lessThan"] = {}
    data["lessThan"]["inclusive"] = true
    data["lessThan"]["value"] = options[:less_than_or_equal_to]
  end

  if options[:odd].present?
    data["step"] = {} 
    data["step"]["message"] = 'should be odd'
    data["step"]["base"] = 1 
    data["step"]["step"] = 2
  end

  if options[:even].present?
    data["step"] = {} 
    data["step"]["message"] = 'should be even'
    data["step"]["base"] = 0 
    data["step"]["step"] = 2
  end

  {method_key => {'validators' => data}}
end

#generate_optionsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/bootstrap_validator_rails/validators/numericality_validator.rb', line 71

def generate_options
  options = @validator.options

  data = {}

  if options[:only_integer].present?
    data[:bv_integer] = 'true'
  end

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

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

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

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

  if options[:odd].present?
    data[:bv_step] = 'true'
    data[:bv_step_message] = 'should be odd'
    data[:bv_step_base] = '1'
    data[:bv_step_step] = '2'
  end

  if options[:even].present?
    data[:bv_step] = 'true'
    data[:bv_step_message] = 'should be even'
    data[:bv_step_base] = '0'
    data[:bv_step_step] = '2'
  end
  data
end