active_record_survey 0.1.31 → 0.1.32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/active_record_survey/node/answer/chained.rb +11 -0
- data/lib/active_record_survey/node/answer.rb +16 -0
- data/lib/active_record_survey/node_map.rb +9 -0
- data/lib/active_record_survey/version.rb +1 -1
- data/spec/active_record_survey/node/answer/boolean_spec.rb +20 -0
- data/spec/active_record_survey/node/answer_spec.rb +21 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18f0a2bc583b09e654fbbb966f3e9899a8d67a4b
|
4
|
+
data.tar.gz: e41e6c23da5678240d833549fff30c60235bd90c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47469259ac340357ac77f839042871b284e0c64b98d7036697edf5eec114dc802e179e038ce72dca1d7917ffaa5b495fc6aced94ca11a004066181898f0ca3f4
|
7
|
+
data.tar.gz: 2ebcef691288d4a91228da1bdcc31f41565fbdc24b103069532fae7a2cba7fb05c5edf9b956dc211703993ffbb9a003c983e3dd69f5fb7c51105f724a2e86656
|
data/README.md
CHANGED
@@ -11,6 +11,10 @@ The goal is to give a simple interface for creating surveys and validating the a
|
|
11
11
|
|
12
12
|
Release Notes
|
13
13
|
============
|
14
|
+
|
15
|
+
**0.1.32**
|
16
|
+
- `ActiveRecordSurvey::Node::Answer#sibling_index` for regular answers and chained answers
|
17
|
+
|
14
18
|
**0.1.31**
|
15
19
|
- `ActiveRecordSurvey::Node::Answer#move_up` and `ActiveRecordSurvey::Node::Answer#move_down` implemented so you can change the position of answers relative to one another i both branching and chained types.
|
16
20
|
I am not happy yet with this. [AwesomeNestedSet](https://github.com/collectiveidea/awesome_nested_set) seems to require nodes exist before moving them which is a limitation I'd like to not have.
|
@@ -8,6 +8,17 @@ module ActiveRecordSurvey
|
|
8
8
|
end
|
9
9
|
|
10
10
|
module InstanceMethods
|
11
|
+
# Gets index relative to other chained answers
|
12
|
+
def sibling_index
|
13
|
+
if node_map = self.survey.node_maps.select { |i|
|
14
|
+
i.node == self
|
15
|
+
}.first
|
16
|
+
return node_map.ancestors_until_node_not_ancestor_of(::ActiveRecordSurvey::Node::Answer).length-1
|
17
|
+
end
|
18
|
+
|
19
|
+
return 0
|
20
|
+
end
|
21
|
+
|
11
22
|
# Chain nodes are different - they must find the final answer node added and add to it
|
12
23
|
def build_answer(question_node)
|
13
24
|
self.survey = question_node.survey
|
@@ -11,6 +11,22 @@ module ActiveRecordSurvey
|
|
11
11
|
}.include?(false)
|
12
12
|
end
|
13
13
|
|
14
|
+
# Gets index in sibling relationship
|
15
|
+
def sibling_index
|
16
|
+
if node_map = self.survey.node_maps.select { |i|
|
17
|
+
i.node == self
|
18
|
+
}.first
|
19
|
+
|
20
|
+
node_map.parent.children.each_with_index { |nm, i|
|
21
|
+
if nm == node_map
|
22
|
+
return i
|
23
|
+
end
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
return 0
|
28
|
+
end
|
29
|
+
|
14
30
|
# Returns the question that preceeds this answer
|
15
31
|
def question
|
16
32
|
self.survey.node_maps.select { |i|
|
@@ -37,6 +37,15 @@ module ActiveRecordSurvey
|
|
37
37
|
result
|
38
38
|
end
|
39
39
|
|
40
|
+
# Gets all the ancestor nodes until one is not an ancestor of klass
|
41
|
+
def ancestors_until_node_not_ancestor_of(klass)
|
42
|
+
if !self.parent || !self.node.class.ancestors.include?(klass)
|
43
|
+
return []
|
44
|
+
end
|
45
|
+
|
46
|
+
[self] + self.parent.ancestors_until_node_not_ancestor_of(klass)
|
47
|
+
end
|
48
|
+
|
40
49
|
# Gets all the child nodes until one is not an ancestor of klass
|
41
50
|
def children_until_node_not_ancestor_of(klass)
|
42
51
|
if !self.node.class.ancestors.include?(klass)
|
@@ -1,6 +1,26 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ActiveRecordSurvey::Node::Answer::Boolean, :boolean_spec => true do
|
4
|
+
describe '#sibling_index', :focus => true do
|
5
|
+
before(:each) do
|
6
|
+
@survey = ActiveRecordSurvey::Survey.new()
|
7
|
+
@q1 = ActiveRecordSurvey::Node::Question.new(:text => "Question #1", :survey => @survey)
|
8
|
+
@q1_a1 = ActiveRecordSurvey::Node::Answer::Boolean.new(:text => "Q1 Answer #1")
|
9
|
+
@q1_a2 = ActiveRecordSurvey::Node::Answer::Boolean.new(:text => "Q1 Answer #2")
|
10
|
+
@q1_a3 = ActiveRecordSurvey::Node::Answer::Boolean.new(:text => "Q1 Answer #3")
|
11
|
+
@q1.build_answer(@q1_a1)
|
12
|
+
@q1.build_answer(@q1_a2)
|
13
|
+
@q1.build_answer(@q1_a3)
|
14
|
+
@survey.save
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should give the answers position relative to its sublings' do
|
18
|
+
expect(@q1_a1.sibling_index).to eq(0)
|
19
|
+
expect(@q1_a2.sibling_index).to eq(1)
|
20
|
+
expect(@q1_a3.sibling_index).to eq(2)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
4
24
|
describe 'move operations' do
|
5
25
|
before(:each) do
|
6
26
|
@survey = ActiveRecordSurvey::Survey.new()
|
@@ -1,6 +1,27 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ActiveRecordSurvey::Node::Answer, :answer_spec => true do
|
4
|
+
|
5
|
+
describe '#sibling_index', :focus => true do
|
6
|
+
before(:each) do
|
7
|
+
@survey = ActiveRecordSurvey::Survey.new()
|
8
|
+
@q1 = ActiveRecordSurvey::Node::Question.new(:text => "Question #1", :survey => @survey)
|
9
|
+
@q1_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 Answer #1")
|
10
|
+
@q1_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 Answer #2")
|
11
|
+
@q1_a3 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 Answer #3")
|
12
|
+
@q1.build_answer(@q1_a1)
|
13
|
+
@q1.build_answer(@q1_a2)
|
14
|
+
@q1.build_answer(@q1_a3)
|
15
|
+
@survey.save
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should give the answers position relative to its sublings' do
|
19
|
+
expect(@q1_a1.sibling_index).to eq(0)
|
20
|
+
expect(@q1_a2.sibling_index).to eq(1)
|
21
|
+
expect(@q1_a3.sibling_index).to eq(2)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
4
25
|
describe 'move operations' do
|
5
26
|
before(:each) do
|
6
27
|
@survey = ActiveRecordSurvey::Survey.new()
|