Class: JsonFields::HashSimpleStructure
- Inherits:
-
BaseStructure
- Object
- BaseStructure
- JsonFields::HashSimpleStructure
- Defined in:
- lib/json_fields/hash_simple_structure.rb
Instance Attribute Summary
Attributes inherited from BaseStructure
Instance Method Summary collapse
-
#assemble(values) ⇒ Object
[], value: [] assemble this => 1 passthrough this.
- #template(object_name, method, options) ⇒ Object
Methods inherited from BaseStructure
Constructor Details
This class inherits a constructor from JsonFields::BaseStructure
Instance Method Details
#assemble(values) ⇒ Object
[], value: [] assemble this => 1 passthrough this
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/json_fields/hash_simple_structure.rb', line 26 def assemble(values) if values.is_a?(Hash) && (values.keys & [:key, :value]).any? if allow_blank Hash[values[:key].zip(values[:value])] else keys = values[:key].delete_if(&:blank?) vals = values[:value].delete_if(&:blank?) Hash[keys.zip(vals)] end else values end 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, ) obj = [:object] id = [object_name, method, 'json_field'].join('_') content_tag(:div, id: id) do html = content_tag(:a, 'Add Field', href: '#', class: 'btn btn-add add-json-fields', 'data-target' => id) Hash(obj.send(method)).merge({""=>""}).collect.with_index { |(key, value), idx| html += content_tag(:div, class: ['template', .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), content_tag(:a, '-', href: '#', class: 'btn btn-remove remove-json-fields') ].join.html_safe end } html.html_safe end end |