Class: Effective::FormBuilderInputs::EffectivePrice

Inherits:
Effective::FormBuilderInput show all
Defined in:
app/models/effective/form_builder_inputs/effective_price.rb

Instance Method Summary collapse

Methods inherited from Effective::FormBuilderInput

#field_name, #initialize, #js_options, #options

Constructor Details

This class inherits a constructor from Effective::FormBuilderInput

Instance Method Details

#default_input_htmlObject



10
11
12
# File 'app/models/effective/form_builder_inputs/effective_price.rb', line 10

def default_input_html
  { class: 'effective_price numeric', maxlength: 14, autocomplete: 'off' }
end

#default_optionsObject



6
7
8
# File 'app/models/effective/form_builder_inputs/effective_price.rb', line 6

def default_options
  { include_blank: false }
end

#html_optionsObject



47
48
49
50
51
# File 'app/models/effective/form_builder_inputs/effective_price.rb', line 47

def html_options
  super().tap do |html_options|
    html_options['data-include-blank'] = options[:include_blank]
  end
end

#priceObject



40
41
42
43
44
45
# File 'app/models/effective/form_builder_inputs/effective_price.rb', line 40

def price
  return nil if (@value == nil && options[:include_blank])

  val = (@value || 0) # This is 'super'
  val.kind_of?(Integer) ? val : (val * 100.0).to_i
end

#price_fieldObject

These two are for the hidden input



36
37
38
# File 'app/models/effective/form_builder_inputs/effective_price.rb', line 36

def price_field
  "#{field_name.parameterize.gsub('-', '_')}_value_as_integer"
end

#to_htmlObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/effective/form_builder_inputs/effective_price.rb', line 14

def to_html
  if options[:input_group] == false
    return text_field_tag(field_name, number_to_currency(value, unit: ''), tag_options) + hidden_field_tag(field_name, price, id: price_field)
  end

  (:div, class: 'input-group') do
    (:span, '$', class: 'input-group-addon') do
      (:i, '', class: 'glyphicon glyphicon-usd').html_safe
    end +
    text_field_tag(field_name, number_to_currency(value, unit: ''), tag_options) +
    hidden_field_tag(field_name, price, id: price_field)
  end
end

#valueObject



28
29
30
31
32
33
# File 'app/models/effective/form_builder_inputs/effective_price.rb', line 28

def value
  return nil if (@value == nil && options[:include_blank])

  val = (@value || 0) # This is 'super'
  val.kind_of?(Integer) ? ('%.2f' % (val / 100.0)) : ('%.2f' % val)
end