active_record_survey 0.1.7 → 0.1.8

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: 07f6faab384d06f3ef40dd8786143e54b36bc195
4
- data.tar.gz: 47f1f3bc3f2a8319d3978cc94388ebbfdde81f82
3
+ metadata.gz: d54c3e737a268c6e9aa3f60f02a62433131007f3
4
+ data.tar.gz: 56c1e7fa286ef30e2735fc1f6f118cae6facc6f3
5
5
  SHA512:
6
- metadata.gz: 502f9da2618cd7e84441668135c5d1678f49fc382e729e04e8fd1eeba374a8049049b6ec2a012205d69cff2352865d217e890a3da85fcb26f8bb82c74c4e4ac7
7
- data.tar.gz: 552fc36dfacb68835174cc6e8f53151b2def44c6f0a789befee1aa49225f5bb3409084e8f88363632620a4849cee5a81c08b25428ec57df85f956f85ca658914
6
+ metadata.gz: 4618ddd55a8dda8bc1b6589ba8241cb9afc73bfdf502439990fb449441c4334d1a3b3d1306843e8b00f9c040b331787d265dc594c616dc32e953847e0745b05d
7
+ data.tar.gz: 1ce77748739de54e4189e94a40951bf22e877d69bd636401ca9097fee94d6211e56c7bf7b8627b7740a356244441acdd80545031984d14446f7efb746a9c915c
@@ -9,7 +9,7 @@ module ActiveRecordSurvey
9
9
  validate do |instance_node|
10
10
  # This instance_node has no valid path to the root node
11
11
  if !self.node.instance_node_path_to_root?(self)
12
- instance_node.errors[:base] << "NO_PATH"
12
+ instance_node.errors[:base] << "INVALID_PATH"
13
13
  end
14
14
 
15
15
  parent_nodes = self.node.node_maps.collect { |j| j.parent }
@@ -30,22 +30,42 @@ module ActiveRecordSurvey
30
30
  self.has_instance_node_for_instance?(instance)
31
31
  end
32
32
 
33
+ # Default behaviour is to recurse up the chain (goal is to hit a question node)
34
+ def validate_parent_instance_node(instance_node, child_node)
35
+ !self.node_maps.collect { |node_map|
36
+ if node_map.parent
37
+ node_map.parent.node.validate_parent_instance_node(instance_node, self)
38
+ # Hit top node
39
+ else
40
+ true
41
+ end
42
+ }.include?(false)
43
+ end
44
+
33
45
  # Run all validations applied to this node
34
46
  def validate_instance_node(instance_node)
35
47
  # Basically this cache is messed up? Why? TODO.
36
48
  # Reloading in the spec seems to fix this... but... this could be a booby trap for others
37
49
  #self.node_validations(true)
38
50
 
39
- !self.node_validations.collect { |node_validation|
51
+ # Check the validations on this node against the instance_node
52
+ validations_passed = !self.node_validations.collect { |node_validation|
40
53
  node_validation.validate_instance_node(instance_node, self)
41
- }.include?(false) && !self.node_maps.collect { |node_map|
54
+ }.include?(false)
55
+
56
+ # More complex....
57
+ # Recureses to the parent node to check
58
+ # This is to validate Node::Question since they don't have instance_nodes directly to validate them
59
+ parent_validations_passed = !self.node_maps.collect { |node_map|
42
60
  if node_map.parent
43
- node_map.parent.node.validate_instance_node(instance_node)
61
+ node_map.parent.node.validate_parent_instance_node(instance_node, self)
44
62
  # Hit top node
45
63
  else
46
64
  true
47
65
  end
48
66
  }.include?(false)
67
+
68
+ validations_passed && parent_validations_passed
49
69
  end
50
70
 
51
71
  # Whether there is a valid answer path from this node to the root node for the instance
@@ -6,6 +6,8 @@ module ActiveRecordSurvey
6
6
  if instance_node = self.instance_node_for_instance(instance)
7
7
  # Answered if has text
8
8
  instance_node.value.to_s.strip.length > 0
9
+ else
10
+ false
9
11
  end
10
12
  end
11
13
  end
@@ -1,4 +1,10 @@
1
1
  module ActiveRecordSurvey
2
2
  class Node::Question < Node
3
+ # Stop validating at the Question node
4
+ def validate_parent_instance_node(instance_node, child_node)
5
+ !self.node_validations.collect { |node_validation|
6
+ node_validation.validate_instance_node(instance_node, self)
7
+ }.include?(false)
8
+ end
3
9
  end
4
10
  end
@@ -5,7 +5,7 @@ module ActiveRecordSurvey
5
5
  def validate_instance_node(instance_node, question_node = nil)
6
6
  # Only makes sense for questions to have maximum answers
7
7
  if !question_node.class.ancestors.include?(::ActiveRecordSurvey::Node::Question)
8
- return false
8
+ return false
9
9
  end
10
10
 
11
11
  instance = instance_node.instance
@@ -20,7 +20,11 @@ module ActiveRecordSurvey
20
20
  }
21
21
  }.flatten.select { |i| i }.count
22
22
 
23
- total_answered <= self.value.to_i
23
+ is_valid = (total_answered <= self.value.to_i)
24
+
25
+ instance_node.errors[:base] << { :nodes => { question_node.id => ["MAXIMUM_ANSWER"] } } if !is_valid
26
+
27
+ is_valid
24
28
  end
25
29
  end
26
30
  end
@@ -2,8 +2,12 @@ module ActiveRecordSurvey
2
2
  # Ensure the instance_node has a length less than the maximum
3
3
  class NodeValidation::MaximumLength < NodeValidation
4
4
  # Validate the instance_node value
5
- def validate_instance_node(instance_node, node = nil)
6
- (self.value.to_i >= instance_node.value.to_s.length.to_i)
5
+ def validate_instance_node(instance_node, answer_node = nil)
6
+ is_valid = (self.value.to_i >= instance_node.value.to_s.length.to_i)
7
+
8
+ instance_node.errors[:base] << { :nodes => { answer_node.id => ["MAXIMUM_LENGTH"] } } if !is_valid
9
+
10
+ is_valid
7
11
  end
8
12
  end
9
13
  end
@@ -2,9 +2,12 @@ module ActiveRecordSurvey
2
2
  # Ensure the instance_node has a value less than the maximum
3
3
  class NodeValidation::MaximumValue < NodeValidation
4
4
  # Validate the instance_node value is less than the maximum
5
- def validate_instance_node(instance_node, node = nil)
6
- !instance_node.value.to_s.empty? &&
7
- instance_node.value.to_f <= self.value.to_f
5
+ def validate_instance_node(instance_node, answer_node = nil)
6
+ is_valid = (!instance_node.value.to_s.empty? && instance_node.value.to_f <= self.value.to_f)
7
+
8
+ instance_node.errors[:base] << { :nodes => { answer_node.id => ["MAXIMUM_VALUE"] } } if !is_valid
9
+
10
+ is_valid
8
11
  end
9
12
  end
10
13
  end
@@ -20,7 +20,11 @@ module ActiveRecordSurvey
20
20
  }
21
21
  }.flatten.select { |i| i }.count
22
22
 
23
- total_answered >= self.value.to_i
23
+ is_valid = (total_answered >= self.value.to_i)
24
+
25
+ instance_node.errors[:base] << { :nodes => { question_node.id => ["MINIMUM_ANSWER"] } } if !is_valid
26
+
27
+ is_valid
24
28
  end
25
29
  end
26
30
  end
@@ -2,8 +2,12 @@ module ActiveRecordSurvey
2
2
  # Ensure the instance_node has a value greater than the minimum
3
3
  class NodeValidation::MinimumLength < NodeValidation
4
4
  # Validate the instance_node value is greater than the minimum
5
- def validate_instance_node(instance_node, node = nil)
6
- instance_node.value.to_s.length >= self.value.to_i
5
+ def validate_instance_node(instance_node, answer_node = nil)
6
+ is_valid = (instance_node.value.to_s.length >= self.value.to_i)
7
+
8
+ instance_node.errors[:base] << { :nodes => { answer_node.id => ["MINIMUM_LENGTH"] } } if !is_valid
9
+
10
+ is_valid
7
11
  end
8
12
  end
9
13
  end
@@ -2,9 +2,12 @@ module ActiveRecordSurvey
2
2
  # Ensure the instance_node has a value greater than the minimum
3
3
  class NodeValidation::MinimumValue < NodeValidation
4
4
  # Validate the instance_node value is greater than the minimum
5
- def validate_instance_node(instance_node, node = nil)
6
- !instance_node.value.to_s.empty? &&
7
- instance_node.value.to_f >= self.value.to_f
5
+ def validate_instance_node(instance_node, answer_node = nil)
6
+ is_valid = (!instance_node.value.to_s.empty? && instance_node.value.to_f >= self.value.to_f)
7
+
8
+ instance_node.errors[:base] << { :nodes => { answer_node.id => ["MINIMUM_VALUE"] } } if !is_valid
9
+
10
+ is_valid
8
11
  end
9
12
  end
10
13
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordSurvey
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -87,7 +87,7 @@ describe ActiveRecordSurvey::Node::Answer::Boolean, :boolean_spec => true do
87
87
  expect(instance.valid?).to be(false)
88
88
  end
89
89
 
90
- it 'not answered in order' do
90
+ it 'when not answered in order' do
91
91
  instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
92
92
  instance.instance_nodes.build(
93
93
  :instance => instance,
@@ -1,14 +1,16 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ActiveRecordSurvey::Node::Answer::Text do
3
+ describe ActiveRecordSurvey::Node::Answer::Text, :text_spec => true do
4
4
  describe 'a text survey is' do
5
5
  before(:all) do
6
6
  @survey = ActiveRecordSurvey::Survey.new
7
7
 
8
8
  @q1 = ActiveRecordSurvey::Node::Question.new(:text => "What... what do you think?")
9
- @q1_a1 = ActiveRecordSurvey::Node::Answer::Text.new()
9
+ @q1_a1 = ActiveRecordSurvey::Node::Answer::Text.new(:text => "Text #1")
10
+ @q1_a2 = ActiveRecordSurvey::Node::Answer::Text.new(:text => "Text #2")
10
11
 
11
- @survey.build_question(@q1, [@q1_a1])
12
+ nodes = @survey.build_question(@q1, [@q1_a1])
13
+ nodes = @survey.build_question(@q1_a2, [], nodes[1])
12
14
 
13
15
  @survey.save
14
16
  end
@@ -50,78 +52,180 @@ describe ActiveRecordSurvey::Node::Answer::Text do
50
52
  end
51
53
  end
52
54
 
53
- describe ActiveRecordSurvey::NodeValidation::MinimumLength do
54
- describe 'valid when' do
55
- it 'has a value greater than the minimum' do
56
- @q1_a1.node_validations << ActiveRecordSurvey::NodeValidation::MinimumLength.new(
57
- :node => @q1_a1,
58
- :value => 3
59
- )
60
- instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
61
- instance.instance_nodes.build(
62
- :instance => instance,
55
+ describe ActiveRecordSurvey::NodeValidation do
56
+ describe ActiveRecordSurvey::NodeValidation::MaximumLength do
57
+ before(:all) do
58
+ @q1_a1.node_validations << ActiveRecordSurvey::NodeValidation::MaximumLength.new(
63
59
  :node => @q1_a1,
64
- :value => "this is greater than minimum length",
60
+ :value => 15
65
61
  )
66
- instance.save
67
-
68
- expect(instance.valid?).to be(true)
62
+
63
+ # Weird caching is happening
64
+ @q1_a1.reload
65
+ end
66
+
67
+ describe 'invalid when' do
68
+ it 'has a value greater than the maximum' do
69
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
70
+ instance.instance_nodes.build(
71
+ :instance => instance,
72
+ :node => @q1_a1,
73
+ :value => "123456789111213141516",
74
+ )
75
+ instance.save
76
+
77
+ expect(instance.valid?).to be(false)
78
+ end
79
+ end
80
+
81
+ describe 'valid when' do
82
+ it 'has a value less than the maximum' do
83
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
84
+ instance.instance_nodes.build(
85
+ :instance => instance,
86
+ :node => @q1_a1,
87
+ :value => "test",
88
+ )
89
+ instance.save
90
+
91
+ expect(instance.valid?).to be(true)
92
+ end
69
93
  end
70
94
  end
71
95
 
72
- describe 'invalid when' do
73
- it 'has a value less than the minimum' do
96
+ describe ActiveRecordSurvey::NodeValidation::MinimumLength do
97
+ before(:all) do
74
98
  @q1_a1.node_validations << ActiveRecordSurvey::NodeValidation::MinimumLength.new(
75
99
  :node => @q1_a1,
76
- :value => 3
77
- )
78
- instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
79
- instance.instance_nodes.build(
80
- :instance => instance,
81
- :node => @q1_a1,
82
- :value => "12",
100
+ :value => 5
83
101
  )
84
- instance.save
85
102
 
86
- expect(instance.valid?).to be(false)
103
+ # Weird caching is happening
104
+ @q1_a1.reload
105
+ end
106
+
107
+ describe 'invalid when' do
108
+ it 'has a value less than the minimum' do
109
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
110
+ instance.instance_nodes.build(
111
+ :instance => instance,
112
+ :node => @q1_a1,
113
+ :value => "1234",
114
+ )
115
+ instance.save
116
+
117
+ expect(instance.valid?).to be(false)
118
+ end
119
+ end
120
+
121
+ describe 'valid when' do
122
+ it 'has a value greater than the minimum' do
123
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
124
+ instance.instance_nodes.build(
125
+ :instance => instance,
126
+ :node => @q1_a1,
127
+ :value => "12345",
128
+ )
129
+ instance.save
130
+
131
+ expect(instance.valid?).to be(true)
132
+ end
87
133
  end
88
134
  end
89
- end
90
135
 
91
- describe ActiveRecordSurvey::NodeValidation::MaximumLength do
92
- describe 'invalid when' do
93
- it 'has a value greater than the maximum' do
94
- @q1_a1.node_validations << ActiveRecordSurvey::NodeValidation::MaximumLength.new(
95
- :node => @q1_a1,
96
- :value => 15
136
+ describe ActiveRecordSurvey::NodeValidation::MaximumAnswer do
137
+ before(:all) do
138
+ @q1.node_validations << ActiveRecordSurvey::NodeValidation::MaximumAnswer.new(
139
+ :node => @q1,
140
+ :value => 1
97
141
  )
98
- instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
99
- instance.instance_nodes.build(
100
- :instance => instance,
101
- :node => @q1_a1,
102
- :value => "123456789111213141516",
103
- )
104
- instance.save
105
142
 
106
- expect(instance.valid?).to be(false)
143
+ # Weird caching is happening
144
+ @q1_a1.reload
145
+ end
146
+
147
+ describe 'invalid when' do
148
+ it 'has more answers than the maximum' do
149
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
150
+ instance.instance_nodes.build(
151
+ :instance => instance,
152
+ :node => @q1_a1,
153
+ :value => "123456",
154
+ )
155
+ instance.instance_nodes.build(
156
+ :instance => instance,
157
+ :node => @q1_a1,
158
+ :value => "abcdefg",
159
+ )
160
+ instance.save
161
+
162
+ expect(instance.valid?).to be(false)
163
+ end
164
+ end
165
+
166
+ describe 'valid when' do
167
+ it 'has less answers than the maximum' do
168
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
169
+ instance.instance_nodes.build(
170
+ :instance => instance,
171
+ :node => @q1_a1,
172
+ :value => "123456",
173
+ )
174
+ instance.instance_nodes.build(
175
+ :instance => instance,
176
+ :node => @q1_a2,
177
+ )
178
+ instance.save
179
+ puts instance.errors.inspect
180
+ expect(instance.valid?).to be(true)
181
+ end
107
182
  end
108
183
  end
109
184
 
110
- describe 'valid when' do
111
- it 'has a value less than the maximum' do
112
- @q1_a1.node_validations << ActiveRecordSurvey::NodeValidation::MaximumLength.new(
113
- :node => @q1_a1,
114
- :value => 15
115
- )
116
- instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
117
- instance.instance_nodes.build(
118
- :instance => instance,
119
- :node => @q1_a1,
120
- :value => "test",
185
+ describe ActiveRecordSurvey::NodeValidation::MinimumAnswer do
186
+ before(:all) do
187
+ @q1.node_validations << ActiveRecordSurvey::NodeValidation::MinimumAnswer.new(
188
+ :node => @q1,
189
+ :value => 1
121
190
  )
122
- instance.save
123
191
 
124
- expect(instance.valid?).to be(true)
192
+ # Weird caching is happening
193
+ @q1_a1.reload
194
+ end
195
+
196
+ describe 'invalid when' do
197
+ it 'has less than the minimum number of answers' do
198
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
199
+ instance.instance_nodes.build(
200
+ :instance => instance,
201
+ :node => @q1_a1,
202
+ )
203
+ instance.instance_nodes.build(
204
+ :instance => instance,
205
+ :node => @q1_a2,
206
+ )
207
+ instance.save
208
+
209
+ expect(instance.valid?).to be(false)
210
+ end
211
+ end
212
+
213
+ describe 'valid when' do
214
+ it 'has greater than the minimum number of answers' do
215
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
216
+ instance.instance_nodes.build(
217
+ :instance => instance,
218
+ :node => @q1_a1,
219
+ :value => "12345",
220
+ )
221
+ instance.instance_nodes.build(
222
+ :instance => instance,
223
+ :node => @q1_a2,
224
+ )
225
+ instance.save
226
+ puts instance.errors.inspect
227
+ expect(instance.valid?).to be(true)
228
+ end
125
229
  end
126
230
  end
127
231
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_survey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Butch Marshall