active_record_survey 0.1.3 → 0.1.4
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e14e2ac1a5afccd6174d0c4a6a6d63eaf299648
|
4
|
+
data.tar.gz: 851b819031bbf4121f31003892eed93168ba0bb6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96cd0a3783d52e6a472229deea9782fb11b46fa8e71a24739eefab6d2be7c6fc8080531fdc01c4532600094da451b05d9e57c5f6d00dee308dc3b2a554eb6140
|
7
|
+
data.tar.gz: 61fa51b61fe2e0de0e7b6fe9c0fa5f0de3fa9e1348f65d78a69e0458ee49f99c0a5f96f153bdc1916c32931826a4c35b8cc9bc9177ac8a5c3edd993a1b9e798d
|
@@ -1,12 +1,47 @@
|
|
1
1
|
module ActiveRecordSurvey
|
2
|
-
# Rank in relation to parent/children
|
2
|
+
# Rank in relation to parent/children of ActiveRecordSurvey::Node::Answer::Rank
|
3
3
|
class Node::Answer::Rank < Node::Answer
|
4
4
|
# Accept integer or empty values
|
5
|
+
# Must be within range of the number of ranking nodes
|
5
6
|
def validate_instance_node(instance_node)
|
6
7
|
# super - all validations on this node pass
|
7
8
|
super &&
|
8
|
-
(instance_node.value.to_s.empty? ||
|
9
|
-
|
9
|
+
(instance_node.value.to_s.empty? || !instance_node.value.to_s.match(/^\d+$/).nil?) &&
|
10
|
+
(instance_node.value.to_s.empty? || instance_node.value.to_i >= 1) &&
|
11
|
+
instance_node.value.to_i <= self.max_rank
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
|
16
|
+
# Calculate the number of Rank nodes above this one
|
17
|
+
def num_above
|
18
|
+
count = 0
|
19
|
+
self.node_maps.each { |i|
|
20
|
+
# Parent is one of us as well - include it and check its parents
|
21
|
+
if i.parent.node.class.ancestors.include?(self.class)
|
22
|
+
count = count + 1 + i.parent.node.num_above
|
23
|
+
end
|
24
|
+
}
|
25
|
+
count
|
26
|
+
end
|
27
|
+
|
28
|
+
# Calculate the number of Rank nodes below this one
|
29
|
+
def num_below
|
30
|
+
count = 0
|
31
|
+
self.node_maps.each { |node_map|
|
32
|
+
node_map.children.each { |child|
|
33
|
+
# Child is one of us as well - include it and check its children
|
34
|
+
if child.node.class.ancestors.include?(self.class)
|
35
|
+
count = count + 1 + child.node.num_below
|
36
|
+
end
|
37
|
+
}
|
38
|
+
}
|
39
|
+
count
|
40
|
+
end
|
41
|
+
|
42
|
+
# Calculate the maximum rank value that is accepted
|
43
|
+
def max_rank
|
44
|
+
self.num_above + self.num_below + 1
|
10
45
|
end
|
11
46
|
end
|
12
47
|
end
|
@@ -0,0 +1,221 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveRecordSurvey::Node::Answer::Rank, :focus => true do
|
4
|
+
describe 'survey is' do
|
5
|
+
before(:all) do
|
6
|
+
@survey = ActiveRecordSurvey::Survey.new
|
7
|
+
|
8
|
+
@q1 = ActiveRecordSurvey::Node::Question.new(:text => "Order by preference")
|
9
|
+
@q1_a1 = ActiveRecordSurvey::Node::Answer::Rank.new(:text => "Dogs")
|
10
|
+
@q1_a2 = ActiveRecordSurvey::Node::Answer::Rank.new(:text => "Cats")
|
11
|
+
@q1_a3 = ActiveRecordSurvey::Node::Answer::Rank.new(:text => "Mice")
|
12
|
+
@q1_a4 = ActiveRecordSurvey::Node::Answer::Rank.new(:text => "Ferrets")
|
13
|
+
@q1_a5 = ActiveRecordSurvey::Node::Answer::Rank.new(:text => "Rats")
|
14
|
+
|
15
|
+
nodes = @survey.build_question(@q1, [@q1_a1])
|
16
|
+
nodes = @survey.build_question(@q1_a2, [], nodes[1])
|
17
|
+
nodes = @survey.build_question(@q1_a3, [], nodes[0])
|
18
|
+
nodes = @survey.build_question(@q1_a4, [], nodes[0])
|
19
|
+
nodes = @survey.build_question(@q1_a5, [], nodes[0])
|
20
|
+
|
21
|
+
@survey.save
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'valid when' do
|
25
|
+
it 'accepts an integer value' do
|
26
|
+
instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
|
27
|
+
instance.instance_nodes.build(
|
28
|
+
:instance => instance,
|
29
|
+
:node => @q1_a1,
|
30
|
+
:value => 1
|
31
|
+
)
|
32
|
+
instance.save
|
33
|
+
|
34
|
+
expect(instance.valid?).to be(true)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'accepts an empty value (no rank given)' do
|
38
|
+
instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
|
39
|
+
instance.instance_nodes.build(
|
40
|
+
:instance => instance,
|
41
|
+
:node => @q1_a1,
|
42
|
+
)
|
43
|
+
instance.save
|
44
|
+
|
45
|
+
expect(instance.valid?).to be(true)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'ranked at than maximum number of answers' do
|
49
|
+
instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
|
50
|
+
instance.instance_nodes.build(
|
51
|
+
:instance => instance,
|
52
|
+
:node => @q1_a1,
|
53
|
+
:value => 5
|
54
|
+
)
|
55
|
+
instance.save
|
56
|
+
|
57
|
+
expect(instance.valid?).to be(true)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'all answers are ranked in the correct order' do
|
61
|
+
instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
|
62
|
+
instance.instance_nodes.build(
|
63
|
+
:instance => instance,
|
64
|
+
:node => @q1_a1,
|
65
|
+
:value => 1
|
66
|
+
)
|
67
|
+
instance.instance_nodes.build(
|
68
|
+
:instance => instance,
|
69
|
+
:node => @q1_a2,
|
70
|
+
:value => 3
|
71
|
+
)
|
72
|
+
instance.instance_nodes.build(
|
73
|
+
:instance => instance,
|
74
|
+
:node => @q1_a3,
|
75
|
+
:value => 2
|
76
|
+
)
|
77
|
+
instance.instance_nodes.build(
|
78
|
+
:instance => instance,
|
79
|
+
:node => @q1_a4,
|
80
|
+
:value => 5
|
81
|
+
)
|
82
|
+
instance.instance_nodes.build(
|
83
|
+
:instance => instance,
|
84
|
+
:node => @q1_a5,
|
85
|
+
:value => 4
|
86
|
+
)
|
87
|
+
instance.save
|
88
|
+
|
89
|
+
expect(instance.valid?).to be(true)
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'one answer is not ranked' do
|
93
|
+
instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
|
94
|
+
instance.instance_nodes.build(
|
95
|
+
:instance => instance,
|
96
|
+
:node => @q1_a1,
|
97
|
+
:value => 1
|
98
|
+
)
|
99
|
+
instance.instance_nodes.build(
|
100
|
+
:instance => instance,
|
101
|
+
:node => @q1_a2,
|
102
|
+
:value => 3
|
103
|
+
)
|
104
|
+
# Refusal to rank a3
|
105
|
+
instance.instance_nodes.build(
|
106
|
+
:instance => instance,
|
107
|
+
:node => @q1_a3
|
108
|
+
)
|
109
|
+
instance.instance_nodes.build(
|
110
|
+
:instance => instance,
|
111
|
+
:node => @q1_a4,
|
112
|
+
:value => 2
|
113
|
+
)
|
114
|
+
# Refusal to rank a5
|
115
|
+
instance.instance_nodes.build(
|
116
|
+
:instance => instance,
|
117
|
+
:node => @q1_a5
|
118
|
+
)
|
119
|
+
instance.save
|
120
|
+
|
121
|
+
expect(instance.valid?).to be(true)
|
122
|
+
end
|
123
|
+
|
124
|
+
# TODO - additional min and max answer required validation nodes...
|
125
|
+
end
|
126
|
+
|
127
|
+
describe 'invalid when' do
|
128
|
+
it 'rejects a float value' do
|
129
|
+
instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
|
130
|
+
instance.instance_nodes.build(
|
131
|
+
:instance => instance,
|
132
|
+
:node => @q1_a1,
|
133
|
+
:value => 1.5
|
134
|
+
)
|
135
|
+
instance.save
|
136
|
+
|
137
|
+
expect(instance.valid?).to be(false)
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'rejects an alphabetical value' do
|
141
|
+
instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
|
142
|
+
instance.instance_nodes.build(
|
143
|
+
:instance => instance,
|
144
|
+
:node => @q1_a1,
|
145
|
+
:value => "1a2"
|
146
|
+
)
|
147
|
+
instance.save
|
148
|
+
|
149
|
+
expect(instance.valid?).to be(false)
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'ranked 0' do
|
153
|
+
instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
|
154
|
+
instance.instance_nodes.build(
|
155
|
+
:instance => instance,
|
156
|
+
:node => @q1_a1,
|
157
|
+
:value => 0
|
158
|
+
)
|
159
|
+
instance.save
|
160
|
+
|
161
|
+
expect(instance.valid?).to be(false)
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'ranked higher than maximum number of answers' do
|
165
|
+
instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
|
166
|
+
instance.instance_nodes.build(
|
167
|
+
:instance => instance,
|
168
|
+
:node => @q1_a1,
|
169
|
+
:value => 6
|
170
|
+
)
|
171
|
+
instance.save
|
172
|
+
|
173
|
+
expect(instance.valid?).to be(false)
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'rejects when not answered in order' do
|
177
|
+
instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
|
178
|
+
instance.instance_nodes.build(
|
179
|
+
:instance => instance,
|
180
|
+
:node => @q1_a2,
|
181
|
+
:value => 1
|
182
|
+
)
|
183
|
+
instance.save
|
184
|
+
|
185
|
+
expect(instance.valid?).to be(false)
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'all one answer is ranked in correctly' do
|
189
|
+
instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
|
190
|
+
instance.instance_nodes.build(
|
191
|
+
:instance => instance,
|
192
|
+
:node => @q1_a1,
|
193
|
+
:value => 1
|
194
|
+
)
|
195
|
+
instance.instance_nodes.build(
|
196
|
+
:instance => instance,
|
197
|
+
:node => @q1_a2,
|
198
|
+
:value => 3
|
199
|
+
)
|
200
|
+
instance.instance_nodes.build(
|
201
|
+
:instance => instance,
|
202
|
+
:node => @q1_a3,
|
203
|
+
:value => 12
|
204
|
+
)
|
205
|
+
instance.instance_nodes.build(
|
206
|
+
:instance => instance,
|
207
|
+
:node => @q1_a4,
|
208
|
+
:value => 5
|
209
|
+
)
|
210
|
+
instance.instance_nodes.build(
|
211
|
+
:instance => instance,
|
212
|
+
:node => @q1_a5,
|
213
|
+
:value => 4
|
214
|
+
)
|
215
|
+
instance.save
|
216
|
+
|
217
|
+
expect(instance.valid?).to be(false)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
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.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Butch Marshall
|
@@ -144,6 +144,7 @@ files:
|
|
144
144
|
- lib/generators/active_record_survey/templates/migration_0.1.0.rb
|
145
145
|
- rspec_rvm
|
146
146
|
- spec/active_record_survey/node/answer/boolean_spec.rb
|
147
|
+
- spec/active_record_survey/node/answer/rank_spec.rb
|
147
148
|
- spec/active_record_survey/node/answer/scale_spec.rb
|
148
149
|
- spec/active_record_survey/survey_spec.rb
|
149
150
|
- spec/active_record_survey_spec.rb
|
@@ -174,6 +175,7 @@ specification_version: 4
|
|
174
175
|
summary: Surveys using activerecord
|
175
176
|
test_files:
|
176
177
|
- spec/active_record_survey/node/answer/boolean_spec.rb
|
178
|
+
- spec/active_record_survey/node/answer/rank_spec.rb
|
177
179
|
- spec/active_record_survey/node/answer/scale_spec.rb
|
178
180
|
- spec/active_record_survey/survey_spec.rb
|
179
181
|
- spec/active_record_survey_spec.rb
|