Class: JsonFields::HashSimpleStructure

Inherits:
BaseStructure show all
Defined in:
lib/json_fields/hash_simple_structure.rb

Instance Method Summary collapse

Instance Method Details

#assemble(values) ⇒ Object



24
25
26
# File 'lib/json_fields/hash_simple_structure.rb', line 24

def assemble(values)
  Hash[values[:key].zip(values[:value])]
end

#template(object_name, method, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/json_fields/hash_simple_structure.rb', line 4

def template(object_name, method, options)
  obj = options[:object]
  id = [object_name, method, 'json_field'].join('_')

  (:div, id: id) do
    html = (:a, 'Add Field', href: '#', class: 'btn btn-add add-json-fields', 'data-target' => id)
    Hash(obj.send(method)).collect.with_index { |(key, value), idx|
      html += (:div, class: ['template', options.delete(:wrapper_class)].compact.join(' ')) do
        [
          text_field_tag("#{object_name}[#{method}][key][]", nil, value: key, class: 'json-field-control', id: nil),
          text_field_tag("#{object_name}[#{method}][value][]", nil, value: value, class: 'json-field-control', id: nil),
         (:a, '-', href: '#', class: 'btn btn-remove remove-json-fields')
        ].join.html_safe
      end
    }
    html.html_safe
  end

end