active_record_survey 0.1.13 → 0.1.14

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: 83a2266578ea626f30af00337a09c9e3e5d72479
4
- data.tar.gz: 41c7b8258b00f3d622543603758eda0c720f372e
3
+ metadata.gz: c61a24fbd16d0add2429b4bbe70a02c19aec13f2
4
+ data.tar.gz: ba2f206b945844811bb7393dfe573ee5bb023a55
5
5
  SHA512:
6
- metadata.gz: 9b01dbf73ba271e7cc2d7c0b7e980b8d26282917f9e505c7261915158cc8ed38cfb5076f1f00bee24485789782830393695b84f37f243893a97728f5e54b0563
7
- data.tar.gz: 93aaf4fa9c8b98b94b57f1bd73d3f9fa96b8d4ff08db6604ea838fa32ce5fec99c61bed561a51e65371e2918074ef595d3568894dd426c4ec880d843a2a4b6bc
6
+ metadata.gz: b7b6d0b8319ddbb329358af965a05ba740af3d19ab5ec18af17743537c114243b82a1e957588b55851978b6c334bba0d33613e0b10e4ed538799ee5d9ecff150
7
+ data.tar.gz: 40c2e2f618498d56dbb81fd019154d8949f1e416cbc872ffc0117831bbf18bea35787266efc05a28e7db0b81e9d63fdab3814d70e1c358c1bd24c709b97f8d63
data/README.md CHANGED
@@ -12,6 +12,9 @@ The goal is to give a simple interface for creating surveys and validating the a
12
12
  Release Notes
13
13
  ============
14
14
 
15
+ **0.1.14**
16
+ - build_question now only accepts a question
17
+ - Exceptions added
15
18
  **0.1.13**
16
19
  - Added Question#build_answer and Answer#build_link to make survey creation possible without dealing with internal nodes
17
20
 
@@ -19,6 +19,25 @@ module ActiveRecordSurvey
19
19
  end
20
20
  end
21
21
 
22
+ # Rank nodes are different - they must find the final rank node added and add to it
23
+ def build_answer(question_node, survey)
24
+ # No node_maps exist yet from this question
25
+ if question_node.node_maps.length === 0
26
+ # Build our first node-map
27
+ question_node.node_maps.build(:node => question_node, :survey => survey)
28
+ end
29
+
30
+ last_in_chain = question_node.answers.last || question_node
31
+
32
+ # Each instance of this question needs the answer hung from it
33
+ last_in_chain.node_maps.each { |node_map|
34
+ answer_node_map = self.node_maps.build(:node => self, :survey => survey)
35
+ node_map.children << answer_node_map
36
+ }
37
+
38
+ true
39
+ end
40
+
22
41
  protected
23
42
 
24
43
  # Calculate the number of Rank nodes above this one
@@ -17,5 +17,24 @@ module ActiveRecordSurvey
17
17
  false
18
18
  end
19
19
  end
20
+
21
+ # Scale nodes are different - they must find the final scale node added and add to it
22
+ def build_answer(question_node, survey)
23
+ # No node_maps exist yet from this question
24
+ if question_node.node_maps.length === 0
25
+ # Build our first node-map
26
+ question_node.node_maps.build(:node => question_node, :survey => survey)
27
+ end
28
+
29
+ last_in_chain = question_node.answers.last || question_node
30
+
31
+ # Each instance of this question needs the answer hung from it
32
+ last_in_chain.node_maps.each { |node_map|
33
+ answer_node_map = self.node_maps.build(:node => self, :survey => survey)
34
+ node_map.children << answer_node_map
35
+ }
36
+
37
+ true
38
+ end
20
39
  end
21
40
  end
@@ -10,5 +10,24 @@ module ActiveRecordSurvey
10
10
  false
11
11
  end
12
12
  end
13
+
14
+ # Text nodes are different - they must find the final text node added and add to it
15
+ def build_answer(question_node, survey)
16
+ # No node_maps exist yet from this question
17
+ if question_node.node_maps.length === 0
18
+ # Build our first node-map
19
+ question_node.node_maps.build(:node => question_node, :survey => survey)
20
+ end
21
+
22
+ last_in_chain = question_node.answers.last || question_node
23
+
24
+ # Each instance of this question needs the answer hung from it
25
+ last_in_chain.node_maps.each { |node_map|
26
+ answer_node_map = self.node_maps.build(:node => self, :survey => survey)
27
+ node_map.children << answer_node_map
28
+ }
29
+
30
+ true
31
+ end
13
32
  end
14
33
  end
@@ -25,6 +25,11 @@ module ActiveRecordSurvey
25
25
  # Build a link from this node to another node
26
26
  # Building a link actually needs to throw off a whole new clone of all children nodes
27
27
  def build_link(to_node)
28
+ # build_link only accepts a to_node that inherits from Question
29
+ if !to_node.class.ancestors.include?(::ActiveRecordSurvey::Node::Question)
30
+ raise ArgumentError.new "to_node must inherit from ::ActiveRecordSurvey::Node::Question"
31
+ end
32
+
28
33
  # Answer has already got a question - throw error
29
34
  if self.node_maps.select { |i|
30
35
  i.children.length === 0
@@ -8,7 +8,18 @@ module ActiveRecordSurvey
8
8
  end
9
9
 
10
10
  # Build an answer off this node
11
- def build_answer(answer_node, survey)
11
+ def build_answer(answer_node, survey = nil)
12
+ survey = survey || self.node_maps.select { |i|
13
+ !i.survey.nil?
14
+ }.collect { |i|
15
+ i.survey
16
+ }.first
17
+
18
+ # A survey must either be passed or already present in node_maps
19
+ if survey.nil?
20
+ raise ArgumentError.new "A survey must be passed if Question is not yet added to a survey"
21
+ end
22
+
12
23
  # Answers actually define how they're built off the parent node... yep
13
24
  answer_node.build_answer(self, survey)
14
25
  end
@@ -24,22 +24,21 @@ module ActiveRecordSurvey
24
24
  }
25
25
  end
26
26
 
27
- # Build a question with answers for this survey
28
- def build_question(question, answers = [], parent = nil)
29
- node_maps = []
30
- n_question = question.node_maps.build(:node => question, :survey => self)
31
- node_maps << n_question
32
-
33
- answers.each { |answer|
34
- n_answer = answer.node_maps.build(:node => answer, :survey => self)
35
- n_question.children << n_answer
36
- node_maps << n_answer
37
- }
38
-
39
- # If a parent node is passed, add it
40
- parent.children << n_question if !parent.nil?
41
-
42
- node_maps
27
+ # Build a question for this survey
28
+ def build_question(question)
29
+ # build_question only accepts a node that inherits from Question
30
+ if !question.class.ancestors.include?(::ActiveRecordSurvey::Node::Question)
31
+ raise ArgumentError.new "Question must inherit from ::ActiveRecordSurvey::Node::Question"
32
+ end
33
+
34
+ # Already added - shouldn't add twice
35
+ if question.node_maps.select { |node_map|
36
+ node_map.survey === self
37
+ }.length > 0
38
+ raise RuntimeError.new "This question has already been added to the survey"
39
+ end
40
+
41
+ question.node_maps.build(:node => question, :survey => self)
43
42
  end
44
43
  end
45
44
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordSurvey
2
- VERSION = "0.1.13"
2
+ VERSION = "0.1.14"
3
3
  end
@@ -12,11 +12,12 @@ describe ActiveRecordSurvey::Node::Answer::Boolean, :boolean_spec => true do
12
12
  @q1_a4 = ActiveRecordSurvey::Node::Answer::Boolean.new()
13
13
  @q1_a5 = ActiveRecordSurvey::Node::Answer::Boolean.new()
14
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])
15
+ @survey.build_question(@q1)
16
+ @q1.build_answer(@q1_a1, @survey)
17
+ @q1.build_answer(@q1_a2, @survey)
18
+ @q1.build_answer(@q1_a3, @survey)
19
+ @q1.build_answer(@q1_a4, @survey)
20
+ @q1.build_answer(@q1_a5, @survey)
20
21
 
21
22
  @survey.save
22
23
  end
@@ -12,11 +12,12 @@ describe ActiveRecordSurvey::Node::Answer::Rank, :rank_spec => true do
12
12
  @q1_a4 = ActiveRecordSurvey::Node::Answer::Rank.new()
13
13
  @q1_a5 = ActiveRecordSurvey::Node::Answer::Rank.new()
14
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])
15
+ @survey.build_question(@q1)
16
+ @q1.build_answer(@q1_a1, @survey)
17
+ @q1.build_answer(@q1_a2, @survey)
18
+ @q1.build_answer(@q1_a3, @survey)
19
+ @q1.build_answer(@q1_a4, @survey)
20
+ @q1.build_answer(@q1_a5, @survey)
20
21
 
21
22
  @survey.save
22
23
  end
@@ -10,9 +10,10 @@ describe ActiveRecordSurvey::Node::Answer::Scale, :scale_spec => true do
10
10
  @q1_a2 = ActiveRecordSurvey::Node::Answer::Scale.new()
11
11
  @q1_a3 = ActiveRecordSurvey::Node::Answer::Scale.new()
12
12
 
13
- nodes = @survey.build_question(@q1, [@q1_a1])
14
- nodes = @survey.build_question(@q1_a2, [], nodes[1])
15
- nodes = @survey.build_question(@q1_a3, [], nodes[0])
13
+ @survey.build_question(@q1)
14
+ @q1.build_answer(@q1_a1, @survey)
15
+ @q1.build_answer(@q1_a2, @survey)
16
+ @q1.build_answer(@q1_a3, @survey)
16
17
 
17
18
  @survey.save
18
19
  end
@@ -9,8 +9,9 @@ describe ActiveRecordSurvey::Node::Answer::Text, :text_spec => true do
9
9
  @q1_a1 = ActiveRecordSurvey::Node::Answer::Text.new()
10
10
  @q1_a2 = ActiveRecordSurvey::Node::Answer::Text.new()
11
11
 
12
- nodes = @survey.build_question(@q1, [@q1_a1])
13
- nodes = @survey.build_question(@q1_a2, [], nodes[1])
12
+ @survey.build_question(@q1)
13
+ @q1.build_answer(@q1_a1)
14
+ @q1.build_answer(@q1_a2)
14
15
 
15
16
  @survey.save
16
17
  end
@@ -5,49 +5,44 @@ describe ActiveRecordSurvey::Node::Question, :question_spec => true do
5
5
  before(:all) do
6
6
  @survey = ActiveRecordSurvey::Survey.new()
7
7
 
8
- @q1 = ActiveRecordSurvey::Node::Question.new()
9
- @q1_a1 = ActiveRecordSurvey::Node::Answer.new()
10
- @q1_a2 = ActiveRecordSurvey::Node::Answer.new()
11
- @q1_a3 = ActiveRecordSurvey::Node::Answer.new()
12
-
13
- @q2 = ActiveRecordSurvey::Node::Question.new()
14
- @q2_a1 = ActiveRecordSurvey::Node::Answer.new()
15
- @q2_a2 = ActiveRecordSurvey::Node::Answer.new()
16
-
17
- @q3 = ActiveRecordSurvey::Node::Question.new()
18
- @q3_a1 = ActiveRecordSurvey::Node::Answer.new()
19
- @q3_a2 = ActiveRecordSurvey::Node::Answer.new()
20
-
21
- @q4 = ActiveRecordSurvey::Node::Question.new()
22
- @q4_a1 = ActiveRecordSurvey::Node::Answer.new()
23
- @q4_a2 = ActiveRecordSurvey::Node::Answer.new()
24
-
25
- @q5 = ActiveRecordSurvey::Node::Question.new()
26
- @q5_a1 = ActiveRecordSurvey::Node::Answer::Boolean.new()
27
- @q5_a2 = ActiveRecordSurvey::Node::Answer::Boolean.new()
28
- @q5_a3 = ActiveRecordSurvey::Node::Answer::Boolean.new()
29
- @q5_a4 = ActiveRecordSurvey::Node::Answer::Boolean.new()
30
- @q5_a5 = ActiveRecordSurvey::Node::Answer::Boolean.new()
31
-
32
- q1_nodes = @survey.build_question(@q1, [@q1_a1, @q1_a2, @q1_a3])
33
- q2_nodes = @survey.build_question(@q2, [@q2_a1, @q2_a2], q1_nodes[1])
34
- q4_nodes = @survey.build_question(@q4, [@q4_a1, @q4_a2], q2_nodes[1])
35
-
36
- q3_nodes = @survey.build_question(@q3, [@q3_a1, @q3_a2], q2_nodes[2])
37
- q4_nodes = @survey.build_question(@q4, [@q4_a1, @q4_a2], q3_nodes[1])
38
- q4_nodes = @survey.build_question(@q4, [@q4_a1, @q4_a2], q3_nodes[2])
39
-
40
- q3_nodes = @survey.build_question(@q3, [@q3_a1, @q3_a2], q1_nodes[2])
41
- q4_nodes = @survey.build_question(@q4, [@q4_a1, @q4_a2], q3_nodes[1])
42
- q4_nodes = @survey.build_question(@q4, [@q4_a1, @q4_a2], q3_nodes[2])
43
-
44
- q4_nodes = @survey.build_question(@q4, [@q4_a1, @q4_a2], q1_nodes[3])
45
-
46
- q5_nodes = @survey.build_question(@q5, [@q5_a1], q4_nodes[1])
47
- nodes = @survey.build_question(@q5_a2, [], q5_nodes[1])
48
- nodes = @survey.build_question(@q5_a3, [], nodes[0])
49
- nodes = @survey.build_question(@q5_a4, [], nodes[0])
50
- nodes = @survey.build_question(@q5_a5, [], nodes[0])
8
+ @q1 = ActiveRecordSurvey::Node::Question.new(:text => "Question #1")
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, @survey)
13
+ @q1.build_answer(@q1_a2, @survey)
14
+ @q1.build_answer(@q1_a3, @survey)
15
+
16
+ @q2 = ActiveRecordSurvey::Node::Question.new(:text => "Question #2")
17
+ @q2_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q2 Answer #1")
18
+ @q2_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q2 Answer #2")
19
+ @q2.build_answer(@q2_a1, @survey)
20
+ @q2.build_answer(@q2_a2, @survey)
21
+
22
+ @q3 = ActiveRecordSurvey::Node::Question.new(:text => "Question #3")
23
+ @q3_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q3 Answer #1")
24
+ @q3_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q3 Answer #2")
25
+ @q3.build_answer(@q3_a1, @survey)
26
+ @q3.build_answer(@q3_a2, @survey)
27
+
28
+ @q4 = ActiveRecordSurvey::Node::Question.new(:text => "Question #4")
29
+ @q4_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q4 Answer #1")
30
+ @q4_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q4 Answer #2")
31
+ @q4.build_answer(@q4_a1, @survey)
32
+ @q4.build_answer(@q4_a2, @survey)
33
+
34
+ # Link up Q1
35
+ @q1_a1.build_link(@q2)
36
+ @q1_a2.build_link(@q3)
37
+ @q1_a3.build_link(@q4)
38
+
39
+ # Link up Q2
40
+ @q2_a1.build_link(@q4)
41
+ @q2_a2.build_link(@q3)
42
+
43
+ # Link up Q3
44
+ @q3_a1.build_link(@q4)
45
+ @q3_a2.build_link(@q4)
51
46
 
52
47
  @survey.save
53
48
  end
@@ -58,11 +53,11 @@ describe ActiveRecordSurvey::Node::Question, :question_spec => true do
58
53
  question.answers
59
54
  }.flatten
60
55
 
61
- expect(answers.length).to eq(14)
56
+ expect(answers.length).to eq(9)
62
57
  end
63
58
  end
64
59
 
65
- describe 'build_answer', :focus => true do
60
+ describe '#build_answer', :focus => true do
66
61
  it 'should have the right number of node maps' do
67
62
  @survey = ActiveRecordSurvey::Survey.new()
68
63
 
@@ -3,38 +3,46 @@ require 'spec_helper'
3
3
  describe ActiveRecordSurvey::Survey, :survey_spec => true do
4
4
  describe 'a survey' do
5
5
  before(:all) do
6
- @survey = ActiveRecordSurvey::Survey.new
6
+ @survey = ActiveRecordSurvey::Survey.new()
7
7
 
8
- @q1 = ActiveRecordSurvey::Node::Question.new()
9
- @q1_a1 = ActiveRecordSurvey::Node::Answer.new()
10
- @q1_a2 = ActiveRecordSurvey::Node::Answer.new()
11
- @q1_a3 = ActiveRecordSurvey::Node::Answer.new()
8
+ @q1 = ActiveRecordSurvey::Node::Question.new(:text => "Question #1")
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, @survey)
13
+ @q1.build_answer(@q1_a2, @survey)
14
+ @q1.build_answer(@q1_a3, @survey)
12
15
 
13
- @q2 = ActiveRecordSurvey::Node::Question.new()
14
- @q2_a1 = ActiveRecordSurvey::Node::Answer.new()
15
- @q2_a2 = ActiveRecordSurvey::Node::Answer.new()
16
+ @q2 = ActiveRecordSurvey::Node::Question.new(:text => "Question #2")
17
+ @q2_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q2 Answer #1")
18
+ @q2_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q2 Answer #2")
19
+ @q2.build_answer(@q2_a1, @survey)
20
+ @q2.build_answer(@q2_a2, @survey)
16
21
 
17
- @q3 = ActiveRecordSurvey::Node::Question.new()
18
- @q3_a1 = ActiveRecordSurvey::Node::Answer.new()
19
- @q3_a2 = ActiveRecordSurvey::Node::Answer.new()
22
+ @q3 = ActiveRecordSurvey::Node::Question.new(:text => "Question #3")
23
+ @q3_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q3 Answer #1")
24
+ @q3_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q3 Answer #2")
25
+ @q3.build_answer(@q3_a1, @survey)
26
+ @q3.build_answer(@q3_a2, @survey)
20
27
 
21
- @q4 = ActiveRecordSurvey::Node::Question.new()
22
- @q4_a1 = ActiveRecordSurvey::Node::Answer.new()
23
- @q4_a2 = ActiveRecordSurvey::Node::Answer.new()
28
+ @q4 = ActiveRecordSurvey::Node::Question.new(:text => "Question #4")
29
+ @q4_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q4 Answer #1")
30
+ @q4_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q4 Answer #2")
31
+ @q4.build_answer(@q4_a1, @survey)
32
+ @q4.build_answer(@q4_a2, @survey)
24
33
 
25
- q1_nodes = @survey.build_question(@q1, [@q1_a1, @q1_a2, @q1_a3])
26
- q2_nodes = @survey.build_question(@q2, [@q2_a1, @q2_a2], q1_nodes[1])
27
- q4_nodes = @survey.build_question(@q4, [@q4_a1, @q4_a2], q2_nodes[1])
34
+ # Link up Q1
35
+ @q1_a1.build_link(@q2)
36
+ @q1_a2.build_link(@q3)
37
+ @q1_a3.build_link(@q4)
28
38
 
29
- q3_nodes = @survey.build_question(@q3, [@q3_a1, @q3_a2], q2_nodes[2])
30
- q4_nodes = @survey.build_question(@q4, [@q4_a1, @q4_a2], q3_nodes[1])
31
- q4_nodes = @survey.build_question(@q4, [@q4_a1, @q4_a2], q3_nodes[2])
39
+ # Link up Q2
40
+ @q2_a1.build_link(@q4)
41
+ @q2_a2.build_link(@q3)
32
42
 
33
- q3_nodes = @survey.build_question(@q3, [@q3_a1, @q3_a2], q1_nodes[2])
34
- q4_nodes = @survey.build_question(@q4, [@q4_a1, @q4_a2], q3_nodes[1])
35
- q4_nodes = @survey.build_question(@q4, [@q4_a1, @q4_a2], q3_nodes[2])
36
-
37
- q4_nodes = @survey.build_question(@q4, [@q4_a1, @q4_a2], q1_nodes[3])
43
+ # Link up Q3
44
+ @q3_a1.build_link(@q4)
45
+ @q3_a2.build_link(@q4)
38
46
 
39
47
  @survey.save
40
48
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_survey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Butch Marshall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-11 00:00:00.000000000 Z
11
+ date: 2015-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord