Module: Backend::FormHelper

Defined in:
app/helpers/backend/form_helper.rb

Overview

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2015 Brice Texier

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Instance Method Summary collapse

Instance Method Details

#date_field_tag(name, value = nil, html_options = {}) ⇒ Object

Date field tag override default formize date_field_tag helper to simplify use for now


22
23
24
25
26
27
28
# File 'app/helpers/backend/form_helper.rb', line 22

def date_field_tag(name, value = nil, html_options = {})
  html_options[:value] = value
  html_options[:size] ||= 10
  html_options[:type] = :date
  html_options[:name] = name
  tag(:input, html_options)
end

#datetime_field_tag(name, value = nil, html_options = {}) ⇒ Object

Date field tag override default formize date_field_tag helper to simplify use for now


32
33
34
35
36
37
38
39
# File 'app/helpers/backend/form_helper.rb', line 32

def datetime_field_tag(name, value = nil, html_options = {})
  html_options[:size] ||= 16
  html_options[:lang] ||= 'i18n.iso2'.t
  html_options[:type] = :datetime
  html_options[:name] = name
  html_options[:value] = value || params[name]
  tag(:input, html_options)
end

#field_tag(*args) ⇒ Object


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
70
71
# File 'app/helpers/backend/form_helper.rb', line 41

def field_tag(*args)
  options = args.extract_options!
  name = args.shift || options[:name]
  datatype = args.shift || options[:datatype]
  value = args.shift || options[:value]
  html_options = options[:html_options] || {}
  html_options[:required] = true if options[:required]
  html_options[:placeholder] = options[:placeholder] if options[:placeholder]
  if datatype == :boolean
    hidden_field_tag(name, '0') + check_box_tag(name, '1', value, html_options)
  elsif datatype == :measure
    unless unit = options[:unit] || (value ? value.unit : nil)
      raise StandardError, 'Need unit'
    end
    (:div, class: 'input-append') do
      text_field_tag("#{name}[value]", (value ? value.to_d : nil)) +
        select_tag("#{name}[unit]", options_for_select(Measure.siblings(unit).collect { |u| [Nomen::Unit[u].human_name, u] }, (value ? value.unit : unit)), html_options)
    end
  elsif [:string, :integer, :decimal].include? datatype
    text_field_tag(name, value, html_options)
  elsif datatype == :choice
    choices = options[:choices] || []
    select_tag(name, options_for_select(choices, value), html_options)
  elsif datatype == :accounting_system
    select_tag(name, options_for_select(Nomen::AccountingSystem.selection, value), html_options)
  elsif nomenclature = Nomen[datatype.to_s.pluralize]
    select_tag(name, options_for_select(nomenclature.selection, value), html_options)
  else
    return "[EmptyField #{name.inspect}]"
  end
end

#indicator_field_tag(*args) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/helpers/backend/form_helper.rb', line 73

def indicator_field_tag(*args)
  options = args.extract_options!
  name = args.shift
  indicator = args.shift
  value = args.shift
  datatype = indicator.datatype
  if datatype == :boolean
    hidden_field_tag(name, '0') + check_box_tag(name, '1', value)
  elsif datatype == :measure
    (:div, class: 'input-append') do
      text_field_tag("#{name}[value]", (value ? value.to_d : nil)) +
        select_tag("#{name}[unit]", options_for_select(Measure.siblings(indicator.unit).collect { |u| [Nomen::Unit[u].human_name, u] }, (value ? value.unit : indicator.unit)))
    end
  elsif [:string, :integer, :decimal].include? datatype
    text_field_tag(name, value)
  elsif datatype == :choice
    select_tag(name, options_for_select(indicator.selection(:choices), value))
  else
    return indicator.name.upcase
  end
end