Class: CckForms::ParameterTypeClass::Float

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/cck_forms/parameter_type_class/float.rb

Overview

Represents a floating point value.

Instance Method Summary collapse

Instance Method Details

#build_form(form_builder, options) ⇒ Object

HTML input



44
45
46
47
# File 'lib/cck_forms/parameter_type_class/float.rb', line 44

def build_form(form_builder, options)
  set_value_in_hash options
  form_builder.number_field :value, {step: 'any', class: 'form-control input-small'}.merge(options)
end

#mongoizeObject



6
7
8
# File 'lib/cck_forms/parameter_type_class/float.rb', line 6

def mongoize
  value.to_f
end

#search(selectable, field, query) ⇒ Object

‘123’ -> 123 ‘100/’ -> $gte: 100 ‘100/150’ -> $gte: 100, $lte: 150

'/150' ->            $lte: 150

l: 100 -> $gte: 100 l: 100, h: 150 -> $gte: 100, $lte: 150

h: 150  ->            $lte: 150


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cck_forms/parameter_type_class/float.rb', line 25

def search(selectable, field, query)
  if query.is_a?(Hash) || query.to_s.include?('/')
    low, high = query.is_a?(Hash) ? [query[:l] || query['l'], query[:h] || query['h']] : query.to_s.split('/')

    if low.to_f > 0
      selectable = selectable.gte(field => low.to_f)
    end

    if high.to_f > 0
      selectable = selectable.lte(field => high.to_f)
    end

    selectable
  else
    selectable.where(field => query.to_f)
  end
end

#to_s(_options = nil) ⇒ Object



10
11
12
13
14
15
# File 'lib/cck_forms/parameter_type_class/float.rb', line 10

def to_s(_options = nil)
  options ||= {}
  trailing_zeros = options.fetch(:trailing_zeros, true)
  value.to_f != 0.0 ? value.to_f : ''
  trailing_zeros && value.to_i == value ? value.to_i  : value
end