active_record_survey 0.1.30 → 0.1.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 600d9499d8cde5f959faf9af9775f35c84f965c2
4
- data.tar.gz: 54c5136528e2ea561f2e0bb1fcfc875d21683a9d
3
+ metadata.gz: 082ebe76b425818c9a032b73ed6c0c5385744225
4
+ data.tar.gz: c0c5f13b437c964afa64e515c91e45a180525942
5
5
  SHA512:
6
- metadata.gz: 830b35f1da8d9af17142c1a77061b62ec2dadcd104afbd4033f6ef277d58ee946acc91a704025b01bc0691f3c9988be9976bf1c81bcbbfed98c1633fdfe8b764
7
- data.tar.gz: 82f5041bf2c60cc07c674e0f6217092465da5dfed2dfd3bcaa261d2106d21a52e00fae8a4fd4ab9b1a09ef00bc976560daa662d812e645c05fccdba3a43d6ff4
6
+ metadata.gz: 5b0880add7fe547eaecc719abd0467f1b4c1e3c652831c4d9c9d347840146c9159c71e24cff4a11141c004311bc1ed9907424d2b31512c507dd4a7de322c053e
7
+ data.tar.gz: c3fd2f597ba728044f8556b1a81d57dbd43c0d471baadc3db73086a6ed166f340864a406c08df1020f5aa6f4fbc0af6bc977e7ceabc87bc32f70a2f606fb098d
data/README.md CHANGED
@@ -11,6 +11,9 @@ The goal is to give a simple interface for creating surveys and validating the a
11
11
 
12
12
  Release Notes
13
13
  ============
14
+ **0.1.31**
15
+ - `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
+ 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.
14
17
 
15
18
  **0.1.30**
16
19
  - `ActiveRecordSurvey::Node::Question` now throws ArgumentError if answers of different types are added to it
@@ -32,6 +32,76 @@ module ActiveRecordSurvey
32
32
  true
33
33
  end
34
34
 
35
+ # Moves answer down relative to other answers by swapping parent and children
36
+ def move_up
37
+ # Ensure each parent node to this node (the goal here is to hit a question node) is valid
38
+ !self.survey.node_maps.select { |i|
39
+ i.node == self
40
+ }.collect { |node_map|
41
+ # Parent must be an answer - cannot move into the position of a Question!
42
+ if !node_map.parent.nil? && node_map.parent.node.class.ancestors.include?(::ActiveRecordSurvey::Node::Answer)
43
+ # I know this looks overly complicated, but we need to always work with the survey.node_maps - never children/parent of the relation
44
+ parent_node = self.survey.node_maps.select { |j|
45
+ node_map.parent == j
46
+ }.first
47
+
48
+ parent_parent = self.survey.node_maps.select { |j|
49
+ node_map.parent.parent == j
50
+ }.first
51
+
52
+ node_map.parent = parent_parent
53
+ parent_parent.children << node_map
54
+
55
+ self.survey.node_maps.select { |j|
56
+ node_map.children.include?(j)
57
+ }.each { |c|
58
+ c.parent = parent_node
59
+ parent_node.children << c
60
+ }
61
+
62
+ parent_node.parent = node_map
63
+ node_map.children << parent_node
64
+ end
65
+ }
66
+ end
67
+
68
+ # Moves answer down relative to other answers by swapping parent and children
69
+ def move_down
70
+ # Ensure each parent node to this node (the goal here is to hit a question node) is valid
71
+ !self.survey.node_maps.select { |i|
72
+ i.node == self
73
+ }.collect { |node_map|
74
+ # Must have children to move lower!
75
+ # And the children are also answers!
76
+ if node_map.children.length > 0 && !node_map.children.select { |j| j.node.class.ancestors.include?(::ActiveRecordSurvey::Node::Answer) }.empty?
77
+ # I know this looks overly complicated, but we need to always work with the survey.node_maps - never children/parent of the relation
78
+ parent_node = self.survey.node_maps.select { |j|
79
+ node_map.parent == j
80
+ }.first
81
+
82
+ children = self.survey.node_maps.select { |j|
83
+ node_map.children.include?(j)
84
+ }
85
+
86
+ children_children = self.survey.node_maps.select { |j|
87
+ children.collect { |k| k.children }.flatten.include?(j)
88
+ }
89
+
90
+ children.each { |c|
91
+ parent_node.children << c
92
+ }
93
+
94
+ children.each { |c|
95
+ c.children << node_map
96
+ }
97
+
98
+ children_children.each { |i|
99
+ node_map.children << i
100
+ }
101
+ end
102
+ }
103
+ end
104
+
35
105
  private
36
106
  # Before a node is destroyed, will re-build the node_map links from parent to child if they exist
37
107
  def before_destroy_rebuild_node_map
@@ -135,6 +135,30 @@ module ActiveRecordSurvey
135
135
  }
136
136
  end
137
137
 
138
+ # Moves answer up relative to other answers
139
+ def move_up
140
+ !self.survey.node_maps.select { |i|
141
+ i.node == self
142
+ }.collect { |node_map|
143
+ begin
144
+ node_map.move_left
145
+ rescue
146
+ end
147
+ }
148
+ end
149
+
150
+ # Moves answer down relative to other answers
151
+ def move_down
152
+ !self.survey.node_maps.select { |i|
153
+ i.node == self
154
+ }.collect { |node_map|
155
+ begin
156
+ node_map.move_right
157
+ rescue
158
+ end
159
+ }
160
+ end
161
+
138
162
  private
139
163
  # By default - answers build off the original question node
140
164
  #
@@ -22,7 +22,7 @@ module ActiveRecordSurvey
22
22
 
23
23
  c = (node_maps.nil?)? self.children : node_maps.select { |i|
24
24
  i.parent == self && !i.marked_for_destruction?
25
- }
25
+ }.sort { |a,b| a.left<=>b.left }
26
26
 
27
27
  result = {}
28
28
  result.merge!({ :id => self.id, :node_id => ((self.node.respond_to?(:id))? self.node.id : "") }) if !options[:no_ids] && !self.node.nil?
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordSurvey
2
- VERSION = "0.1.30"
2
+ VERSION = "0.1.31"
3
3
  end
@@ -1,6 +1,66 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ActiveRecordSurvey::Node::Answer::Boolean, :boolean_spec => true do
4
+ describe 'move operations' 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
+ describe '#move_up' do
18
+ it 'should go higher of possible' do
19
+ @q1_a2.move_up
20
+
21
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 Answer #2", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[{"text"=>"Q1 Answer #1", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[{"text"=>"Q1 Answer #3", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[]}]}]}]}])
22
+
23
+ @q1_a3.move_up
24
+
25
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 Answer #2", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[{"text"=>"Q1 Answer #3", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[{"text"=>"Q1 Answer #1", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[]}]}]}]}])
26
+
27
+ @survey.save
28
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 Answer #2", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[{"text"=>"Q1 Answer #3", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[{"text"=>"Q1 Answer #1", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[]}]}]}]}])
29
+
30
+ @survey.reload
31
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 Answer #2", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[{"text"=>"Q1 Answer #3", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[{"text"=>"Q1 Answer #1", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[]}]}]}]}])
32
+ end
33
+
34
+ it 'should not change the position of the first question' do
35
+ @q1_a1.move_up
36
+
37
+ 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"=>"Q1 Answer #3", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[]}]}]}]}])
38
+
39
+ # Save should not affect it
40
+ @survey.save
41
+ 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"=>"Q1 Answer #3", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[]}]}]}]}])
42
+
43
+ # Reload should not affect it
44
+ @survey.reload
45
+ 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"=>"Q1 Answer #3", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[]}]}]}]}])
46
+ end
47
+ end
48
+
49
+ describe '#move_down' do
50
+ it 'should go lower of possible' do
51
+ @q1_a2.move_down
52
+
53
+ 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 #3", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[{"text"=>"Q1 Answer #2", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[]}]}]}]}])
54
+ end
55
+
56
+ it 'should not change the position of the last question' do
57
+ @q1_a3.move_down
58
+
59
+ 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"=>"Q1 Answer #3", :type=>"ActiveRecordSurvey::Node::Answer::Boolean", :children=>[]}]}]}]}])
60
+ end
61
+ end
62
+ end
63
+
4
64
  describe '#destroy' do
5
65
  it 'should re-link broken chains' do
6
66
  survey = ActiveRecordSurvey::Survey.new()
@@ -1,6 +1,52 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ActiveRecordSurvey::Node::Answer, :answer_spec => true do
4
+ describe 'move operations' 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.new(:text => "Q1 Answer #1")
9
+ @q1_a2 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 Answer #2")
10
+ @q1_a3 = ActiveRecordSurvey::Node::Answer.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
+ describe '#move_up' do
18
+ it 'should go higher of possible' do
19
+ @q1_a2.move_up
20
+
21
+ @survey.reload
22
+ expect(@survey.as_map(no_ids: true)).to eq([{"text"=>"Question #1", :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 Answer #2", :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}, {"text"=>"Q1 Answer #1", :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}, {"text"=>"Q1 Answer #3", :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}]}])
23
+ end
24
+
25
+ it 'should not change the position of the first question' do
26
+ @q1_a1.move_up
27
+
28
+ @survey.reload
29
+ 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"=>"Q1 Answer #2", :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}, {"text"=>"Q1 Answer #3", :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}]}])
30
+ end
31
+ end
32
+
33
+ describe '#move_down' do
34
+ it 'should go lower of possible' do
35
+ @q1_a2.move_down
36
+
37
+ @survey.reload
38
+ 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"=>"Q1 Answer #3", :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}, {"text"=>"Q1 Answer #2", :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}]}])
39
+ end
40
+
41
+ it 'should not change the position of the last question' do
42
+ @q1_a3.move_down
43
+
44
+ @survey.reload
45
+ 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"=>"Q1 Answer #2", :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}, {"text"=>"Q1 Answer #3", :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}]}])
46
+ end
47
+ end
48
+ end
49
+
4
50
  # When answer nodes are deleted should:
5
51
  # - Clean upp node_maps
6
52
  # - If chained, build a chain from parent -> child after removing self
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.30
4
+ version: 0.1.31
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-25 00:00:00.000000000 Z
11
+ date: 2016-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord