json_fields 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/json_fields/array_simple_structure.rb +6 -2
- data/lib/json_fields/base_structure.rb +6 -0
- data/lib/json_fields/configurable.rb +3 -2
- data/lib/json_fields/configuration_parser.rb +2 -2
- data/lib/json_fields/hash_simple_structure.rb +8 -2
- data/lib/json_fields/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a8758cdcd2c0994b7c11a807f8584f0e41e3305
|
4
|
+
data.tar.gz: e14f2e6d1d0065014e4fa836c2a7d45c5b95f116
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 560180df54686f5c14663742491b1a97c4c3b21d4ca6b317880e16a40c2cab732bde695d5dd6c9105aa89cd279e4fcac6a7653089616f92d2d080288facb333e
|
7
|
+
data.tar.gz: 4bd300120e051dd59f9971188f8268a048708f3f5d6f86c2159a5146ede219fc6f06da4be81af4deedf687f52c2aa6a80e572c04f24f50ac1e0be2419e870998
|
@@ -11,7 +11,7 @@ module JsonFields
|
|
11
11
|
|
12
12
|
content_tag(:div, id: id) do
|
13
13
|
html = content_tag(:a, 'Add Field', href: '#', class: 'btn btn-add add-json-fields', 'data-target' => id)
|
14
|
-
Array(obj.send(method)).collect.with_index { |value, idx|
|
14
|
+
(Array(obj.send(method)) + [""]).collect.with_index { |value, idx|
|
15
15
|
html += content_tag(:div, class: ['template', options.delete(:wrapper_class)].compact.join(' ')) do
|
16
16
|
[text_field_tag("#{object_name}[#{method}][]", nil, value: value, class: 'json-field-control', id: nil, name: "#{object_name}[#{method}][]"),
|
17
17
|
content_tag(:a, '-', href: '#', class: 'btn btn-remove remove-json-fields')
|
@@ -25,7 +25,11 @@ module JsonFields
|
|
25
25
|
|
26
26
|
# Takes in values and returns a normal structure that can be saved
|
27
27
|
def assemble(values)
|
28
|
-
|
28
|
+
if allow_blank
|
29
|
+
values
|
30
|
+
else
|
31
|
+
values.delete_if(&:blank?)
|
32
|
+
end
|
29
33
|
end
|
30
34
|
end
|
31
35
|
end
|
@@ -4,6 +4,12 @@ module JsonFields
|
|
4
4
|
include ActionView::Helpers::FormTagHelper
|
5
5
|
include ActionView::Helpers::TagHelper
|
6
6
|
|
7
|
+
attr_accessor :allow_blank
|
8
|
+
|
9
|
+
def initialize(options)
|
10
|
+
@allow_blank = options[:allow_blank] || false
|
11
|
+
end
|
12
|
+
|
7
13
|
def template(object_name, method, options)
|
8
14
|
raise "template must be defined in the subclass"
|
9
15
|
end
|
@@ -2,8 +2,9 @@ module JsonFields
|
|
2
2
|
module Configurable
|
3
3
|
@@fields = {}
|
4
4
|
|
5
|
-
def json_field(attribute, json_structure)
|
6
|
-
|
5
|
+
def json_field(attribute, json_structure, options = {})
|
6
|
+
options[:allow_blank] ||= false
|
7
|
+
@@fields[attribute] = JsonFields::ConfigurationParser.parse!(json_structure, options)
|
7
8
|
define_method("#{attribute}=") do |values|
|
8
9
|
value = self.class.json_fields[attribute].assemble(values)
|
9
10
|
self.write_attribute(attribute, value)
|
@@ -2,12 +2,12 @@ module JsonFields
|
|
2
2
|
class ConfigurationParser
|
3
3
|
|
4
4
|
# returns a Structure class based on the parsed data
|
5
|
-
def self.parse!(data)
|
5
|
+
def self.parse!(data, options = {})
|
6
6
|
data.match(/(\A[A-Z][A-Za-z]+)\((\w+)\)/)
|
7
7
|
raise JsonFields::ConfigurationError.new("Configuration data is incorrect: #{data}") if $1.nil? || $2.nil?
|
8
8
|
klass = "JsonFields::#{$1}#{$2}Structure".safe_constantize
|
9
9
|
raise JsonFields::ConfigurationError.new("No Structure found") if klass.nil?
|
10
|
-
klass.new
|
10
|
+
klass.new(options)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -7,7 +7,7 @@ module JsonFields
|
|
7
7
|
|
8
8
|
content_tag(:div, id: id) do
|
9
9
|
html = content_tag(:a, 'Add Field', href: '#', class: 'btn btn-add add-json-fields', 'data-target' => id)
|
10
|
-
Hash(obj.send(method)).collect.with_index { |(key, value), idx|
|
10
|
+
Hash(obj.send(method)).merge({""=>""}).collect.with_index { |(key, value), idx|
|
11
11
|
html += content_tag(:div, class: ['template', options.delete(:wrapper_class)].compact.join(' ')) do
|
12
12
|
[
|
13
13
|
text_field_tag("#{object_name}[#{method}][key][]", nil, value: key, class: 'json-field-control', id: nil),
|
@@ -22,7 +22,13 @@ module JsonFields
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def assemble(values)
|
25
|
-
|
25
|
+
if allow_blank
|
26
|
+
Hash[values[:key].zip(values[:value])]
|
27
|
+
else
|
28
|
+
keys = values[:key].delete_if(&:blank?)
|
29
|
+
vals = values[:value].delete_if(&:blank?)
|
30
|
+
Hash[keys.zip(vals)]
|
31
|
+
end
|
26
32
|
end
|
27
33
|
end
|
28
34
|
end
|
data/lib/json_fields/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Woertink
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|