active_record_survey 0.1.12 → 0.1.13

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: ea8378c022e6489a03056cfd20fcb7dac00863c5
4
- data.tar.gz: d779425ffc38a345a61c391ef713c3b006f0d20b
3
+ metadata.gz: 83a2266578ea626f30af00337a09c9e3e5d72479
4
+ data.tar.gz: 41c7b8258b00f3d622543603758eda0c720f372e
5
5
  SHA512:
6
- metadata.gz: 5f55ed3b2a5035039c103d6ce3fe3be7681bc9e4e8d555ecaa753712e5efb6f442a01c9a5b4a1447032568af99dde0da14e6bd6daf4304a7c2eb67a5620aab45
7
- data.tar.gz: 00b171258168e584b04d828f20c68610a5a57af3abb4d7f8ff60b0a355432bc047a1de3905d3ff5d39df90c8197de5819811e624ae82919217807b020cab2cb7
6
+ metadata.gz: 9b01dbf73ba271e7cc2d7c0b7e980b8d26282917f9e505c7261915158cc8ed38cfb5076f1f00bee24485789782830393695b84f37f243893a97728f5e54b0563
7
+ data.tar.gz: 93aaf4fa9c8b98b94b57f1bd73d3f9fa96b8d4ff08db6604ea838fa32ce5fec99c61bed561a51e65371e2918074ef595d3568894dd426c4ec880d843a2a4b6bc
data/README.md CHANGED
@@ -9,6 +9,12 @@ This gem tries to be as unopinionated as possible on the peripheral details on h
9
9
 
10
10
  The goal is to give a simple interface for creating surveys and validating the answers given to them.
11
11
 
12
+ Release Notes
13
+ ============
14
+
15
+ **0.1.13**
16
+ - Added Question#build_answer and Answer#build_link to make survey creation possible without dealing with internal nodes
17
+
12
18
  ## Installation
13
19
 
14
20
  Add this line to your application's Gemfile:
@@ -38,6 +44,49 @@ See the spec file for more detailed usage.
38
44
  **Build a basic survey**
39
45
  ```ruby
40
46
 
47
+ # New method for building surveys
48
+
49
+ @q1 = ActiveRecordSurvey::Node::Question.new(:text => "Question #1")
50
+ @q1_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 Answer #1")
51
+ @q1_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 Answer #2")
52
+ @q1_a3 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 Answer #3")
53
+ @q1.build_answer(@q1_a1, @survey)
54
+ @q1.build_answer(@q1_a2, @survey)
55
+ @q1.build_answer(@q1_a3, @survey)
56
+
57
+ @q2 = ActiveRecordSurvey::Node::Question.new(:text => "Question #2")
58
+ @q2_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q2 Answer #1")
59
+ @q2_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q2 Answer #2")
60
+ @q2.build_answer(@q2_a1, @survey)
61
+ @q2.build_answer(@q2_a2, @survey)
62
+
63
+ @q3 = ActiveRecordSurvey::Node::Question.new(:text => "Question #3")
64
+ @q3_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q3 Answer #1")
65
+ @q3_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q3 Answer #2")
66
+ @q3.build_answer(@q3_a1, @survey)
67
+ @q3.build_answer(@q3_a2, @survey)
68
+
69
+ @q4 = ActiveRecordSurvey::Node::Question.new(:text => "Question #4")
70
+ @q4_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q4 Answer #1")
71
+ @q4_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q4 Answer #2")
72
+ @q4.build_answer(@q4_a1, @survey)
73
+ @q4.build_answer(@q4_a2, @survey)
74
+
75
+ # Link up Q1
76
+ @q1_a1.build_link(@q2)
77
+ @q1_a2.build_link(@q3)
78
+ @q1_a3.build_link(@q4)
79
+
80
+ # Link up Q2
81
+ @q2_a1.build_link(@q4)
82
+ @q2_a2.build_link(@q3)
83
+
84
+ # Link up Q3
85
+ @q3_a1.build_link(@q4)
86
+ @q3_a2.build_link(@q4)
87
+
88
+ # Depricated method for building surveys
89
+
41
90
  q1 = ActiveRecordSurvey::Node::Question.new() # Q1
42
91
  q1_a1 = ActiveRecordSurvey::Node::Answer.new() # Q1 A1
43
92
  q1_a2 = ActiveRecordSurvey::Node::Answer.new() # Q1 A2
data/bin/Example_1.png CHANGED
File without changes
data/bin/Example_1.svg CHANGED
File without changes
data/bin/Example_1.xml CHANGED
File without changes
@@ -15,5 +15,24 @@ module ActiveRecordSurvey
15
15
  (instance_node.value.to_i === 1)
16
16
  end
17
17
  end
18
+
19
+ # Boolean nodes are different - they must find the final boolean node added and add to it
20
+ def build_answer(question_node, survey)
21
+ # No node_maps exist yet from this question
22
+ if question_node.node_maps.length === 0
23
+ # Build our first node-map
24
+ question_node.node_maps.build(:node => question_node, :survey => survey)
25
+ end
26
+
27
+ last_in_chain = question_node.answers.last || question_node
28
+
29
+ # Each instance of this question needs the answer hung from it
30
+ last_in_chain.node_maps.each { |node_map|
31
+ answer_node_map = self.node_maps.build(:node => self, :survey => survey)
32
+ node_map.children << answer_node_map
33
+ }
34
+
35
+ true
36
+ end
18
37
  end
19
38
  end
@@ -8,5 +8,70 @@ module ActiveRecordSurvey
8
8
  node_map.parent.node.validate_node(instance)
9
9
  }.include?(false)
10
10
  end
11
+
12
+ # Removes the link
13
+ # TODO - does this work when saved??
14
+ def remove_link
15
+ self.node_maps.each { |answer_node_map|
16
+ answer_node_map.children.each { |child|
17
+ # Should not know about parent
18
+ child.parent = nil
19
+ }
20
+ # Should not know about children
21
+ answer_node_map.children = []
22
+ }
23
+ end
24
+
25
+ # Build a link from this node to another node
26
+ # Building a link actually needs to throw off a whole new clone of all children nodes
27
+ def build_link(to_node)
28
+ # Answer has already got a question - throw error
29
+ if self.node_maps.select { |i|
30
+ i.children.length === 0
31
+ }.length === 0
32
+ raise RuntimeError.new "This answer has already been linked to a question"
33
+ end
34
+
35
+ # Attempt to find an unused to_node node_map
36
+ if !(to_without_parent = to_node.node_maps.select { |i| i.parent.nil? }.first)
37
+ # no unused path exists
38
+ # we need to recursively clone the existing path
39
+
40
+ to_node_map = to_node.node_maps.first || to_node.node_maps.build(:node => to_node, :survey => self.node_maps.first.survey)
41
+
42
+ # Add it to all the from_node_maps
43
+ self.node_maps.each { |from_node_map|
44
+ from_node_map.children << to_node_map.recursive_clone
45
+ }
46
+ else
47
+ # Find the node map from this node that has no children
48
+ from_with_no_children = self.node_maps.select { |i|
49
+ i.children.length == 0
50
+ }.first
51
+
52
+ # Set the child node of this node to a node
53
+ from_with_no_children.children << to_without_parent
54
+ end
55
+ end
56
+
57
+ # By default - answers build off the original question node
58
+ #
59
+ # This allows us to easily override the answer building behaviour for different answer types
60
+ #
61
+ def build_answer(question_node, survey)
62
+ # No node_maps exist yet from this question
63
+ if question_node.node_maps.length === 0
64
+ # Build our first node-map
65
+ question_node.node_maps.build(:node => question_node, :survey => survey)
66
+ end
67
+
68
+ # Each instance of this question needs the answer hung from it
69
+ question_node.node_maps.each { |question_node_map|
70
+ answer_node_map = self.node_maps.build(:node => self, :survey => survey)
71
+ question_node_map.children << answer_node_map
72
+ }
73
+
74
+ true
75
+ end
11
76
  end
12
77
  end
@@ -6,5 +6,11 @@ module ActiveRecordSurvey
6
6
  node_validation.validate_instance_node(instance_node, self)
7
7
  }.include?(false)
8
8
  end
9
+
10
+ # Build an answer off this node
11
+ def build_answer(answer_node, survey)
12
+ # Answers actually define how they're built off the parent node... yep
13
+ answer_node.build_answer(self, survey)
14
+ end
9
15
  end
10
16
  end
@@ -5,6 +5,22 @@ module ActiveRecordSurvey
5
5
  has_many :node_validations, :class_name => "ActiveRecordSurvey::NodeValidation", :foreign_key => :active_record_survey_node_id, autosave: true
6
6
  has_many :instance_nodes, :class_name => "ActiveRecordSurvey::InstanceNode", :foreign_key => :active_record_survey_node_id
7
7
 
8
+ # All the answer nodes that follow from this node
9
+ def answers
10
+ self.node_maps.collect { |i|
11
+ # Get all the children from this node
12
+ i.children
13
+ }.flatten.collect { |i|
14
+ # Get the nodes
15
+ i.node
16
+ }.select { |i|
17
+ # Only the nodes that are answers
18
+ i.class.ancestors.include?(::ActiveRecordSurvey::Node::Answer)
19
+ }.uniq.collect { |i|
20
+ [i] + i.answers
21
+ }.flatten.uniq
22
+ end
23
+
8
24
  # The instance_node recorded for the passed instance for this node
9
25
  def instance_node_for_instance(instance)
10
26
  instance.instance_nodes.select { |instance_node|
@@ -4,9 +4,21 @@ module ActiveRecordSurvey
4
4
  belongs_to :node, :foreign_key => :active_record_survey_node_id
5
5
  belongs_to :survey, :class_name => "ActiveRecordSurvey::Survey", :foreign_key => :active_record_survey_id
6
6
  acts_as_nested_set :scope => [:active_record_survey_id]
7
-
7
+
8
+ validates_presence_of :survey
9
+
8
10
  after_initialize do |i|
9
- i.survey.node_maps << self if i.new_record?
11
+ # Required for all functions to work without creating
12
+ i.survey.node_maps << self if i.new_record? && i.survey
13
+ end
14
+
15
+ # Recursively creates a copy of this entire node_map
16
+ def recursive_clone
17
+ node_map = self.node.node_maps.build(:survey => self.survey, :node => self.node)
18
+ self.children.each { |child_node|
19
+ node_map.children << child_node.recursive_clone
20
+ }
21
+ node_map
10
22
  end
11
23
 
12
24
  def as_map(node_maps = nil)
@@ -15,8 +27,9 @@ module ActiveRecordSurvey
15
27
  }
16
28
 
17
29
  {
30
+ :id => self.id,
31
+ :node_id => self.node.id,
18
32
  :type => self.node.class.to_s,
19
- :text => "#{self.node.text}",
20
33
  :children => children.collect { |i|
21
34
  i.as_map(node_maps)
22
35
  }
@@ -2,14 +2,26 @@ module ActiveRecordSurvey
2
2
  class Survey < ::ActiveRecord::Base
3
3
  self.table_name = "active_record_surveys"
4
4
  has_many :node_maps, :class_name => "ActiveRecordSurvey::NodeMap", :foreign_key => :active_record_survey_id
5
- has_many :nodes, :through => :node_maps
5
+ has_many :nodes, -> { distinct }, :through => :node_maps
6
+
7
+ def questions
8
+ self.node_maps.includes(:node).select { |i|
9
+ i.node.class.ancestors.include?(::ActiveRecordSurvey::Node::Question)
10
+ }.collect { |i|
11
+ i.node
12
+ }.uniq
13
+ end
6
14
 
7
15
  def root_node
8
16
  self.node_maps.includes(:node).select { |i| i.depth === 0 }.first
9
17
  end
10
18
 
11
19
  def as_map
12
- self.root_node.as_map(self.node_maps.includes(:node))
20
+ list = self.node_maps
21
+
22
+ list.select { |i| !i.parent }.collect { |i|
23
+ i.as_map(list)
24
+ }
13
25
  end
14
26
 
15
27
  # Build a question with answers for this survey
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordSurvey
2
- VERSION = "0.1.12"
2
+ VERSION = "0.1.13"
3
3
  end
@@ -0,0 +1,159 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecordSurvey::Node::Question, :question_spec => true do
4
+ describe 'a survey' do
5
+ before(:all) do
6
+ @survey = ActiveRecordSurvey::Survey.new()
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])
51
+
52
+ @survey.save
53
+ end
54
+
55
+ describe '#answers' do
56
+ it 'should return all the answers' do
57
+ answers = @survey.questions.collect { |question|
58
+ question.answers
59
+ }.flatten
60
+
61
+ expect(answers.length).to eq(14)
62
+ end
63
+ end
64
+
65
+ describe 'build_answer', :focus => true do
66
+ it 'should have the right number of node maps' do
67
+ @survey = ActiveRecordSurvey::Survey.new()
68
+
69
+ @q1 = ActiveRecordSurvey::Node::Question.new(:text => "Question #1")
70
+ @q1_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 Answer #1")
71
+ @q1_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 Answer #2")
72
+ @q1_a3 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 Answer #3")
73
+ @q1.build_answer(@q1_a1, @survey)
74
+ @q1.build_answer(@q1_a2, @survey)
75
+ @q1.build_answer(@q1_a3, @survey)
76
+
77
+ @q2 = ActiveRecordSurvey::Node::Question.new(:text => "Question #2")
78
+ @q2_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q2 Answer #1")
79
+ @q2_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q2 Answer #2")
80
+ @q2.build_answer(@q2_a1, @survey)
81
+ @q2.build_answer(@q2_a2, @survey)
82
+
83
+ @q3 = ActiveRecordSurvey::Node::Question.new(:text => "Question #3")
84
+ @q3_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q3 Answer #1")
85
+ @q3_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q3 Answer #2")
86
+ @q3.build_answer(@q3_a1, @survey)
87
+ @q3.build_answer(@q3_a2, @survey)
88
+
89
+ @q4 = ActiveRecordSurvey::Node::Question.new(:text => "Question #4")
90
+ @q4_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q4 Answer #1")
91
+ @q4_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q4 Answer #2")
92
+ @q4.build_answer(@q4_a1, @survey)
93
+ @q4.build_answer(@q4_a2, @survey)
94
+
95
+ expect(@survey.node_maps.length).to eq(13)
96
+
97
+ # Link up Q1
98
+ @q1_a1.build_link(@q2)
99
+ @q1_a2.build_link(@q3)
100
+ @q1_a3.build_link(@q4)
101
+
102
+ expect(@survey.node_maps.length).to eq(13)
103
+
104
+ # Link up Q2
105
+ @q2_a1.build_link(@q4)
106
+ @q2_a2.build_link(@q3)
107
+
108
+ expect(@survey.node_maps.length).to eq(19)
109
+
110
+ # Link up Q3
111
+ @q3_a1.build_link(@q4)
112
+ @q3_a2.build_link(@q4)
113
+
114
+ expect(@survey.node_maps.length).to eq(31)
115
+
116
+ @q5 = ActiveRecordSurvey::Node::Question.new(:text => "Question #5")
117
+ @q5_a1 = ActiveRecordSurvey::Node::Answer::Boolean.new(:text => "Q5 Answer #1")
118
+ @q5_a2 = ActiveRecordSurvey::Node::Answer::Boolean.new(:text => "Q5 Answer #2")
119
+ @q5_a3 = ActiveRecordSurvey::Node::Answer::Boolean.new(:text => "Q5 Answer #3")
120
+ @q5_a4 = ActiveRecordSurvey::Node::Answer::Boolean.new(:text => "Q5 Answer #4")
121
+ @q5_a5 = ActiveRecordSurvey::Node::Answer::Boolean.new(:text => "Q5 Answer #5")
122
+ # Boolean builds the links differently! - it chains them so there is no branching choice
123
+ @q5.build_answer(@q5_a1, @survey)
124
+ @q5.build_answer(@q5_a2, @survey)
125
+ @q5.build_answer(@q5_a3, @survey)
126
+ @q5.build_answer(@q5_a4, @survey)
127
+ @q5.build_answer(@q5_a5, @survey)
128
+
129
+ @q4_a1.build_link(@q5)
130
+
131
+ expect(@survey.node_maps.length).to eq(37)
132
+
133
+ # Now - add an extra answer to Q1
134
+ @q1_a4 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 Answer #4")
135
+ @q1.build_answer(@q1_a4, @survey)
136
+
137
+ expect(@survey.node_maps.length).to eq(38)
138
+
139
+ @q1_a4.build_link(@q5)
140
+
141
+ expect(@survey.node_maps.length).to eq(44)
142
+
143
+ expect(@survey.as_map.as_json).to eq([{"text"=>"Question #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q1 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q2 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}, {"text"=>"Q2 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q3 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}, {"text"=>"Q3 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}]}]}]}]}, {"text"=>"Q1 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q3 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}, {"text"=>"Q3 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}]}]}, {"text"=>"Q1 Answer #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #5", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q5 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #5", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[]}]}]}]}]}]}]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}, {"text"=>"Q1 Answer #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #5", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q5 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #5", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[]}]}]}]}]}]}]}]}])
144
+
145
+ # Cannot build a link when a link already exists
146
+ expect { @q1_a2.build_link(@q2) }.to raise_error RuntimeError
147
+
148
+ # Remove the link
149
+ @q1_a2.remove_link
150
+
151
+ expect(@survey.as_map.as_json).to eq([{"text"=>"Question #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q1 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q2 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}, {"text"=>"Q2 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q3 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}, {"text"=>"Q3 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}]}]}]}]}, {"text"=>"Q1 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q1 Answer #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #5", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q5 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #5", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[]}]}]}]}]}]}]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}, {"text"=>"Q1 Answer #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #5", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q5 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #5", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[]}]}]}]}]}]}]}]}, {"text"=>"Question #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q3 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}, {"text"=>"Q3 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}]}])
152
+
153
+ @q1_a2.build_link(@q3)
154
+
155
+ expect(@survey.as_map.as_json).to eq([{"text"=>"Question #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q1 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q2 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}, {"text"=>"Q2 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q3 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}, {"text"=>"Q3 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}]}]}]}]}, {"text"=>"Q1 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q3 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}, {"text"=>"Q3 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}]}]}, {"text"=>"Q1 Answer #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q4 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #5", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q5 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #5", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[]}]}]}]}]}]}]}, {"text"=>"Q4 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[]}]}]}, {"text"=>"Q1 Answer #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer", "children"=>[{"text"=>"Question #5", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Question", "children"=>[{"text"=>"Q5 Answer #1", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #2", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #3", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #4", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[{"text"=>"Q5 Answer #5", "id"=>nil, "node_id"=>nil, "type"=>"ActiveRecordSurvey::Node::Answer::Boolean", "children"=>[]}]}]}]}]}]}]}]}])
156
+ end
157
+ end
158
+ end
159
+ end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ActiveRecordSurvey::Survey do
3
+ describe ActiveRecordSurvey::Survey, :survey_spec => true do
4
4
  describe 'a survey' do
5
5
  before(:all) do
6
6
  @survey = ActiveRecordSurvey::Survey.new
@@ -39,6 +39,12 @@ describe ActiveRecordSurvey::Survey do
39
39
  @survey.save
40
40
  end
41
41
 
42
+ describe '#questions' do
43
+ it 'should return all the questions' do
44
+ expect(@survey.questions.length).to be (4)
45
+ end
46
+ end
47
+
42
48
  describe ActiveRecordSurvey::Instance do
43
49
  it 'should allow valid instance_nodes to save' do
44
50
  instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
data/spec/spec_helper.rb CHANGED
@@ -29,7 +29,31 @@ require "generators/active_record_survey/templates/migration_0.1.0"
29
29
 
30
30
  ActiveRecord::Schema.define do
31
31
  AddActiveRecordSurvey.up
32
+
33
+ # Make it easier when you can put text on things
34
+ add_column :active_record_survey_nodes, :text, :string
35
+ end
36
+
37
+ module ActiveRecordSurveyNodeMap
38
+ def self.extended(base)
39
+ base.instance_eval do
40
+ include InstanceMethods
41
+ alias_method_chain :as_map, :text
42
+ end
43
+ end
44
+
45
+ module InstanceMethods
46
+ def as_map_with_text(node_maps = nil)
47
+ result = {
48
+ "text" => self.node.text
49
+ }
50
+ result = result.merge(as_map_without_text(node_maps))
51
+
52
+ result
53
+ end
54
+ end
32
55
  end
56
+ ActiveRecordSurvey::NodeMap.send(:extend, ActiveRecordSurveyNodeMap)
33
57
 
34
58
  RSpec.configure do |config|
35
59
  config.include FactoryGirl::Syntax::Methods
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.12
4
+ version: 0.1.13
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-09-30 00:00:00.000000000 Z
11
+ date: 2015-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -155,6 +155,7 @@ files:
155
155
  - spec/active_record_survey/node/answer/rank_spec.rb
156
156
  - spec/active_record_survey/node/answer/scale_spec.rb
157
157
  - spec/active_record_survey/node/answer/text_spec.rb
158
+ - spec/active_record_survey/node/question_spec.rb
158
159
  - spec/active_record_survey/survey_spec.rb
159
160
  - spec/active_record_survey_spec.rb
160
161
  - spec/spec_helper.rb
@@ -187,6 +188,7 @@ test_files:
187
188
  - spec/active_record_survey/node/answer/rank_spec.rb
188
189
  - spec/active_record_survey/node/answer/scale_spec.rb
189
190
  - spec/active_record_survey/node/answer/text_spec.rb
191
+ - spec/active_record_survey/node/question_spec.rb
190
192
  - spec/active_record_survey/survey_spec.rb
191
193
  - spec/active_record_survey_spec.rb
192
194
  - spec/spec_helper.rb