govuk_elements_form_builder 0.0.2 → 0.0.3

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: b788800d15d8ae1a07b12e3582f03a1b5a6f5cba
4
- data.tar.gz: 97aea32625fa53528d7650eb02438e3370855dce
3
+ metadata.gz: 2fd74da74ab30234815107cffece6456ee7aa1c8
4
+ data.tar.gz: 0033ba79bac042304bd9d54d3223300c8299f550
5
5
  SHA512:
6
- metadata.gz: 427bb61c76c85ccdfd06771bd0bd7165591cba1cd3bc5f8d69fb74cf0eb9bf9a7d25793b4fd9f76986625ef66ff55776eb1f446071a529c2713c6df29a3e69ac
7
- data.tar.gz: b7a125879ee2b133bf44b56c236455826b6b81d534964e9f64ed972272d5e126b91afc13192f79ada47218f4a5e3ed5f12d9de804a351c3a9f47f99e548aeba2
6
+ metadata.gz: eefccc8724fe53d9c0d0b18c9b0fc6248510e62fafb91d2c22aae4877381a7d5d27473f04a818f47872d880fdb24e3fdd5866d71f0de619fdae2871f90c6a375
7
+ data.tar.gz: 998622aee024ff43f0777839a41ae4ceab91548acea038436733df4f60f5794d59a853b0fe0fb36dcd57e494fc95a59173af868d348eb7ce642025392863b070
@@ -22,10 +22,13 @@ module GovukElementsErrorsHelper
22
22
  attributes(object).any? { |child| errors_exist?(child) }
23
23
  end
24
24
 
25
- def self.attributes object
25
+ def self.attributes object, parent_object=nil
26
+ return [] if object == parent_object
27
+ parent_object ||= object
28
+
26
29
  child_objects = attribute_objects object
27
- nested_child_objects = child_objects.map { |o| attributes(o) }
28
- (child_objects + nested_child_objects).flatten
30
+ nested_child_objects = child_objects.map { |o| attributes(o, parent_object) }
31
+ (child_objects + nested_child_objects).flatten - [object]
29
32
  end
30
33
 
31
34
  def self.attribute_objects object
@@ -35,21 +38,47 @@ module GovukElementsErrorsHelper
35
38
  compact
36
39
  end
37
40
 
38
- def self.child_to_parent object, parents={}
41
+ def self.array_to_parent list, object, child_to_parents={}, parent_object
42
+ list.each do |child|
43
+ child_to_parents[child] = object
44
+ child_to_parent child, child_to_parents, parent_object
45
+ end
46
+ end
47
+
48
+ def self.child_to_parent object, child_to_parents={}, parent_object=nil
49
+ return child_to_parents if object == parent_object
50
+ parent_object ||= object
51
+
39
52
  attribute_objects(object).each do |child|
40
- parents[child] = object
41
- child_to_parent child, parents
53
+ if child.is_a?(Array)
54
+ array_to_parent(child, object, child_to_parents, parent_object)
55
+ else
56
+ child_to_parents[child] = object
57
+ child_to_parent child, child_to_parents, parent_object
58
+ end
42
59
  end
43
- parents
60
+
61
+ child_to_parents
44
62
  end
45
63
 
46
64
  def self.instance_variable object, var
47
65
  field = var.to_s.sub('@','').to_sym
48
- object.send(field) if object.respond_to?(field)
66
+ if object.respond_to?(field)
67
+ child = object.send(field)
68
+ if respond_to_errors?(child) || child.is_a?(Array)
69
+ child
70
+ else
71
+ nil
72
+ end
73
+ end
74
+ end
75
+
76
+ def self.respond_to_errors? object
77
+ object && object.respond_to?(:errors)
49
78
  end
50
79
 
51
80
  def self.errors_present? object
52
- object && object.respond_to?(:errors) && object.errors.present?
81
+ respond_to_errors?(object) && object.errors.present?
53
82
  end
54
83
 
55
84
  def self.children_with_errors object
@@ -128,8 +157,8 @@ module GovukElementsErrorsHelper
128
157
  def self.parents_list object, child_to_parents
129
158
  if parent = child_to_parents[object]
130
159
  [].tap do |parents|
131
- while parent
132
- parents.unshift parent
160
+ while parent && !parents.include?(parent)
161
+ parents.unshift parent # prepends parent to front of parents
133
162
  parent = child_to_parents[parent]
134
163
  end
135
164
  end
@@ -208,7 +208,9 @@ module GovukElementsFormBuilder
208
208
  end
209
209
 
210
210
  def error_for? attribute
211
- errors.messages.key?(attribute) && !errors.messages[attribute].empty?
211
+ object.respond_to?(:errors) &&
212
+ errors.messages.key?(attribute) &&
213
+ !errors.messages[attribute].empty?
212
214
  end
213
215
 
214
216
  private_class_method def self.object_attribute_for field, object_name
@@ -1,3 +1,3 @@
1
1
  module GovukElementsFormBuilder
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_elements_form_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alistair Laing
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-02-07 00:00:00.000000000 Z
12
+ date: 2017-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails