json_fields 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7079d62f287d66cddd4463fd928cb5d2a09baaf8
4
- data.tar.gz: dba147269b49c31a3e48fc17b3d5be977fae4f6b
3
+ metadata.gz: 0a8758cdcd2c0994b7c11a807f8584f0e41e3305
4
+ data.tar.gz: e14f2e6d1d0065014e4fa836c2a7d45c5b95f116
5
5
  SHA512:
6
- metadata.gz: e06ec241e16a7de469b77ee68fea07078b7eff34da9f40a6a215deb8fb0f36963a55f0591d2ae155a2640aafa51ea4cfec0356e54be764ba8d6782eb083e23ba
7
- data.tar.gz: 76437d955afa436fafc02ef0b324706c7ab1c1fcfb2abfd2be0233c9725fab32e6d25bb1af5ea0b6bd93cd423acce596eb8de00f3aa8df572f99521288e9750c
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
- values
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
- @@fields[attribute] = JsonFields::ConfigurationParser.parse!(json_structure)
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
- Hash[values[:key].zip(values[:value])]
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
@@ -1,3 +1,3 @@
1
1
  module JsonFields
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
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.0
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-14 00:00:00.000000000 Z
11
+ date: 2017-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties