active_record_survey 0.1.36 → 0.1.37

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: be07fe6de1d3387ccd71f4c56ea9e33535baf95d
4
- data.tar.gz: 8a24d9f1c4459a1f2131b28782dbc035ccbace2a
3
+ metadata.gz: 1e48bc1f74ce11bb6285537bb34e00fb614b1e33
4
+ data.tar.gz: 1528b2c91f64d66e4cf543b37f2b933e356efa5d
5
5
  SHA512:
6
- metadata.gz: 7a533e0173ba238d96430fe78348414cb76249abfd568cc4c6f920d745d920ddc3ea3b12462bb2d4577def1b4d8efefed0ccd2b702c49f9e4c3a822a8f4fa779
7
- data.tar.gz: 13cd48140d0a0050e487e5a615afa3040b19763268a926db3d93e7758c726f261866f14aa21b3970893508aefadb429e9c47460476c5400fa6c6107ea54aa56d
6
+ metadata.gz: d5ed3484bfcac51667d8243b31e187b9b1a372faab368e85d23477ec675b1cfa68ec75e147d3fde56acb57e6a04ca03ce5a740ecf843bde15034ec30959d16bf
7
+ data.tar.gz: 5ff5d9269c791274f92c6538d0f5559080367b1866ef4929b2363d29dbe244fd877fea29283747624ec480a6df0de940b3e848a882f54d20657da9a6a8c25339
@@ -50,6 +50,35 @@ module ActiveRecordSurvey
50
50
  return nil
51
51
  end
52
52
 
53
+ # Removes the node_map from this answer to its next question
54
+ def remove_link
55
+ # not linked to a question - nothing to remove!
56
+ return true if (question = self.next_question).nil?
57
+
58
+ count = 0
59
+ to_remove = []
60
+ self.survey.node_maps.each { |node_map|
61
+ if node_map.node == question
62
+ if count > 0
63
+ to_remove.concat(node_map.self_and_descendants)
64
+ else
65
+ node_map.parent = nil
66
+ end
67
+ count = count + 1
68
+ end
69
+
70
+ if node_map.node == self
71
+ node_map.children = []
72
+ end
73
+ }
74
+ self.survey.node_maps.each { |node_map|
75
+ if to_remove.include?(node_map)
76
+ node_map.parent = nil
77
+ node_map.mark_for_destruction
78
+ end
79
+ }
80
+ end
81
+
53
82
  def build_link(to_node)
54
83
  if self.question.nil?
55
84
  raise ArgumentError.new "A question is required before calling #build_link"
@@ -23,7 +23,47 @@ module ActiveRecordSurvey
23
23
  end
24
24
 
25
25
  # Answers actually define how they're built off the parent node
26
- answer_node.send(:build_answer, self)
26
+ if answer_node.send(:build_answer, self)
27
+
28
+ # If any questions existed directly following this question, insert after this answer
29
+ self.survey.node_maps.select { |i|
30
+ i.node == answer_node
31
+ }.each { |answer_node_map|
32
+ self.survey.node_maps.select { |j|
33
+ # Same parent
34
+ # Is a question
35
+ j.parent == answer_node_map.parent && j.node.class.ancestors.include?(::ActiveRecordSurvey::Node::Question)
36
+ }.each { |j|
37
+ answer_node_map.children << j
38
+ }
39
+ }
40
+
41
+ true
42
+ end
43
+ end
44
+
45
+ # Removes the node_map link from this question all of its next questions
46
+ def remove_link
47
+ return true if (questions = self.next_questions).length === 0
48
+
49
+ # Remove the link to any direct questions
50
+ self.survey.node_maps.select { |i|
51
+ i.node == self
52
+ }.each { |node_map|
53
+ self.survey.node_maps.select { |j|
54
+ node_map.children.include?(j)
55
+ }.each { |child|
56
+ if child.node.class.ancestors.include?(::ActiveRecordSurvey::Node::Question)
57
+ child.parent = nil
58
+ child.send((child.new_record?)? :destroy : :mark_for_destruction )
59
+ end
60
+ }
61
+ }
62
+
63
+ # remove link any answeres that have questions
64
+ self.answers.collect { |i|
65
+ i.remove_link
66
+ }
27
67
  end
28
68
 
29
69
  # Returns the questions that follows this question (either directly or via its answers)
@@ -116,35 +116,6 @@ module ActiveRecordSurvey
116
116
  paths.include?(true)
117
117
  end
118
118
 
119
- # Removes the node_map link
120
- def remove_link
121
- # not linked to a question - nothing to remove!
122
- return true if (question = self.next_question).nil?
123
-
124
- count = 0
125
- to_remove = []
126
- self.survey.node_maps.each { |node_map|
127
- if node_map.node == question
128
- if count > 0
129
- to_remove.concat(node_map.self_and_descendants)
130
- else
131
- node_map.parent = nil
132
- end
133
- count = count + 1
134
- end
135
-
136
- if node_map.node == self
137
- node_map.children = []
138
- end
139
- }
140
- self.survey.node_maps.each { |node_map|
141
- if to_remove.include?(node_map)
142
- node_map.parent = nil
143
- node_map.mark_for_destruction
144
- end
145
- }
146
- end
147
-
148
119
  # Build a link from this node to another node
149
120
  # Building a link actually needs to throw off a whole new clone of all children nodes
150
121
  def build_link(to_node)
@@ -161,8 +132,8 @@ module ActiveRecordSurvey
161
132
 
162
133
  # Answer has already got a question - throw error
163
134
  if from_node_maps.select { |i|
164
- i.children.length === 0
165
- }.length === 0
135
+ i.children.length > 0
136
+ }.length > 0
166
137
  raise RuntimeError.new "This node has already been linked"
167
138
  end
168
139
 
@@ -9,6 +9,21 @@ module ActiveRecordSurvey
9
9
  self.node_maps.includes(:node).select { |i| i.depth === 0 }.first
10
10
  end
11
11
 
12
+ # Builds first question
13
+ def build_first_question(question_node)
14
+ if !question_node.class.ancestors.include?(::ActiveRecordSurvey::Node::Question)
15
+ raise ArgumentError.new "must inherit from ::ActiveRecordSurvey::Node::Question"
16
+ end
17
+
18
+ question_node_maps = self.node_maps.select { |i| i.node == question_node && !i.marked_for_destruction? }
19
+
20
+ # No node_maps exist yet from this question
21
+ if question_node_maps.length === 0
22
+ # Build our first node-map
23
+ question_node_maps << self.node_maps.build(:node => question_node, :survey => self)
24
+ end
25
+ end
26
+
12
27
  def as_map(*args)
13
28
  options = args.extract_options!
14
29
  options[:node_maps] ||= self.node_maps
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordSurvey
2
- VERSION = "0.1.36"
2
+ VERSION = "0.1.37"
3
3
  end
@@ -1,6 +1,84 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ActiveRecordSurvey::Node::Question, :question_spec => true do
4
+ describe "#remove_link" do
5
+ it 'should remove the link between the question and child questions or answers child questions', :focus => true do
6
+ @survey = ActiveRecordSurvey::Survey.new()
7
+ @survey.save
8
+
9
+ @q1 = ActiveRecordSurvey::Node::Question.new(:text => "Question #1", :survey => @survey)
10
+ @survey.build_first_question(@q1)
11
+ @q2 = ActiveRecordSurvey::Node::Question.new(:text => "Question #2", :survey => @survey)
12
+ @q1.build_link(@q2)
13
+
14
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Question #2", :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}]}])
15
+
16
+ @q1.remove_link
17
+
18
+ @survey.save
19
+ @survey.reload
20
+
21
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}])
22
+
23
+ @q1.build_link(@q2)
24
+
25
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Question #2", :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}]}])
26
+
27
+ @q1_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 Answer #1")
28
+ @q1.build_answer(@q1_a1)
29
+
30
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 Answer #1", :type=>"ActiveRecordSurvey::Node::Answer", :children=>[{"text"=>"Question #2", :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}]}]}])
31
+
32
+ @q1.remove_link
33
+
34
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 Answer #1", :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}]}])
35
+
36
+ @survey.save
37
+ @survey.reload
38
+
39
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 Answer #1", :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}]}])
40
+ end
41
+ end
42
+
43
+ describe "#build_link" do
44
+ it 'should build a link between two questions' do
45
+ @survey = ActiveRecordSurvey::Survey.new()
46
+
47
+ @q1 = ActiveRecordSurvey::Node::Question.new(:text => "Question #1", :survey => @survey)
48
+ @survey.build_first_question(@q1)
49
+
50
+ @q2 = ActiveRecordSurvey::Node::Question.new(:text => "Question #2", :survey => @survey)
51
+
52
+ @q1.build_link(@q2)
53
+
54
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Question #2", :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}]}])
55
+ end
56
+
57
+ it 'should still allow answers to be inserted' do
58
+ @survey = ActiveRecordSurvey::Survey.new()
59
+
60
+ @q1 = ActiveRecordSurvey::Node::Question.new(:text => "Question #1", :survey => @survey)
61
+ @survey.build_first_question(@q1)
62
+
63
+ @q2 = ActiveRecordSurvey::Node::Question.new(:text => "Question #2", :survey => @survey)
64
+
65
+ @q1.build_link(@q2)
66
+
67
+ @q1_a1 = ActiveRecordSurvey::Node::Answer::Boolean.new(:text => "Q1 Answer #1")
68
+ @q1.build_answer(@q1_a1)
69
+
70
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 Answer #1", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[{"text"=>"Question #2", :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}]}]}])
71
+
72
+ @q1_a2 = ActiveRecordSurvey::Node::Answer::Boolean.new(:text => "Q1 Answer #2")
73
+ @q1.build_answer(@q1_a2)
74
+
75
+ @survey.save
76
+ @survey.reload
77
+
78
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 Answer #1", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[{"text"=>"Q1 Answer #2", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[{"text"=>"Question #2", :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}]}]}]}])
79
+ end
80
+ end
81
+
4
82
  describe "#next_questions" do
5
83
  it "should return an array of all questions following a question, whether they have answers or not" do
6
84
  @survey = ActiveRecordSurvey::Survey.new()
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.36
4
+ version: 0.1.37
5
5
  platform: ruby
6
6
  authors:
7
7
  - Butch Marshall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-27 00:00:00.000000000 Z
11
+ date: 2016-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord