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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 082ebe76b425818c9a032b73ed6c0c5385744225
4
- data.tar.gz: c0c5f13b437c964afa64e515c91e45a180525942
3
+ metadata.gz: 18f0a2bc583b09e654fbbb966f3e9899a8d67a4b
4
+ data.tar.gz: e41e6c23da5678240d833549fff30c60235bd90c
5
5
  SHA512:
6
- metadata.gz: 5b0880add7fe547eaecc719abd0467f1b4c1e3c652831c4d9c9d347840146c9159c71e24cff4a11141c004311bc1ed9907424d2b31512c507dd4a7de322c053e
7
- data.tar.gz: c3fd2f597ba728044f8556b1a81d57dbd43c0d471baadc3db73086a6ed166f340864a406c08df1020f5aa6f4fbc0af6bc977e7ceabc87bc32f70a2f606fb098d
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,3 +1,3 @@
1
1
  module ActiveRecordSurvey
2
- VERSION = "0.1.31"
2
+ VERSION = "0.1.32"
3
3
  end
@@ -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()
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_survey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.31
4
+ version: 0.1.32
5
5
  platform: ruby
6
6
  authors:
7
7
  - Butch Marshall