Class: Amrita2View::FormHelper::FormFieldHelper

Inherits:
Object
  • Object
show all
Includes:
Amrita2::DictionaryData
Defined in:
lib/amrita2/rails_bridge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Amrita2::DictionaryData

===, #amrita_value

Constructor Details

#initialize(record, form, &block) ⇒ FormFieldHelper

Returns a new instance of FormFieldHelper.



21
22
23
24
25
26
# File 'lib/amrita2/rails_bridge.rb', line 21

def initialize(record, form, &block)
  @record = record
  @form = form
  @data = {}
  block.call(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, name, *args, &block) ⇒ Object



36
37
38
39
# File 'lib/amrita2/rails_bridge.rb', line 36

def method_missing(meth, name, *args, &block)
  value = @form.send(meth, name, *args, &block)
  add_field_element(name, value)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



19
20
21
# File 'lib/amrita2/rails_bridge.rb', line 19

def data
  @data
end

#formObject (readonly)

Returns the value of attribute form.



19
20
21
# File 'lib/amrita2/rails_bridge.rb', line 19

def form
  @form
end

Instance Method Details

#add_field(name, value) ⇒ Object



28
29
30
# File 'lib/amrita2/rails_bridge.rb', line 28

def add_field(name, value)
  @data[name] = value
end

#add_field_element(name, element) ⇒ Object



32
33
34
# File 'lib/amrita2/rails_bridge.rb', line 32

def add_field_element(name, element)
  add_field(name, Amrita2::SanitizedString[element])
end

#as_hashObject



41
42
43
# File 'lib/amrita2/rails_bridge.rb', line 41

def as_hash
  @data
end

#as_label_field_hashObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/amrita2/rails_bridge.rb', line 45

def as_label_field_hash
  @data.inject(Hash.new) do |h, data|
    k, v = *data
    h.merge ({
               k => {
                 :label => {
                   :for=>"#{@record}_#{k}",
                   :text=>k.to_s
                 },
                 :field => v
               }
             })
  end
end