active_record_survey 0.1.21 → 0.1.22
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +48 -1
- data/active_record_survey.gemspec +2 -0
- data/lib/active_record_survey/node/answer.rb +13 -42
- data/lib/active_record_survey/version.rb +1 -1
- data/spec/active_record_survey/node/answer_spec.rb +34 -1
- data/spec/spec_helper.rb +8 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e0bf7b98d33a965f69f24d715c45718ca9b6071
|
4
|
+
data.tar.gz: 380afa7927dea7f05097dddc47405553487719c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8232d4ab5cf39eaa900fd0e286f973c796b77df8eb8a1fe8c1116e4ee3fe0af7746073888cb9d96a7aa19b5fb7a9b1b335a51b471118a8c1de5f52a799d8af2a
|
7
|
+
data.tar.gz: 41e2702dc82b5e057f2da5ab18d9b2905dbf3b1d5fcb9fdf0846d3ca2217da333da0dab6fa4d9f573ad1148f7ebbb450a5e648dc959c477dc77f28c520d75e71
|
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.22**
|
16
|
+
- answer#remove_link cleaned up so it can be understood
|
17
|
+
|
15
18
|
**0.1.20**
|
16
19
|
- answer#build_link now detects and throws an error when a infinite loop is added
|
17
20
|
|
@@ -54,7 +57,7 @@ rails generate active_record_survey:active_record
|
|
54
57
|
|
55
58
|
See the spec file for more detailed usage.
|
56
59
|
|
57
|
-
|
60
|
+
### Build a basic survey
|
58
61
|
```ruby
|
59
62
|
|
60
63
|
# New method for building surveys
|
@@ -111,6 +114,50 @@ The will build a survey with the following node structure.
|
|
111
114
|
|
112
115
|
![alt tag](https://raw.githubusercontent.com/butchmarshall/active_record_survey/master/bin/Example_1.png)
|
113
116
|
|
117
|
+
### Answer Types
|
118
|
+
|
119
|
+
A number of different answer types are implemented by default.
|
120
|
+
|
121
|
+
- [Default (a.k.a radio)](#answer_default)
|
122
|
+
- [Boolean (a.k.a. checkbox)](#answer_checkbox)
|
123
|
+
- [Rank](#answer_rank)
|
124
|
+
- [Scale](#answer_scale)
|
125
|
+
- [Text](#answer_text)
|
126
|
+
|
127
|
+
#### Default <a id="answer_default"></a>
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
ActiveRecordSurvey::Node::Answer
|
131
|
+
```
|
132
|
+
|
133
|
+
#### Boolean <a id="answer_checkbox"></a>
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
ActiveRecordSurvey::Node::Answer::Boolean
|
137
|
+
```
|
138
|
+
|
139
|
+
#### Rank <a id="answer_rank"></a>
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
ActiveRecordSurvey::Node::Answer::Rank
|
143
|
+
```
|
144
|
+
|
145
|
+
#### Scale <a id="answer_scale"></a>
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
ActiveRecordSurvey::Node::Answer::Scale
|
149
|
+
```
|
150
|
+
|
151
|
+
#### Text <a id="answer_text"></a>
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
ActiveRecordSurvey::Node::Answer::Text
|
155
|
+
```
|
156
|
+
|
157
|
+
### Answer Validation
|
158
|
+
|
159
|
+
|
160
|
+
|
114
161
|
## Development
|
115
162
|
|
116
163
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -26,6 +26,8 @@ Gem::Specification.new do |spec|
|
|
26
26
|
else
|
27
27
|
spec.add_development_dependency "sqlite3", "> 0"
|
28
28
|
end
|
29
|
+
|
30
|
+
spec.add_development_dependency "database_cleaner"
|
29
31
|
spec.add_development_dependency "factory_girl", "> 4.0"
|
30
32
|
spec.add_development_dependency "bundler", "~> 1.7"
|
31
33
|
spec.add_development_dependency "rake", "~> 10.0"
|
@@ -1,18 +1,5 @@
|
|
1
1
|
module ActiveRecordSurvey
|
2
2
|
class Node::Answer < Node
|
3
|
-
attr_accessor :ancestor_marked_for_destruction
|
4
|
-
protected :ancestor_marked_for_destruction
|
5
|
-
|
6
|
-
before_save do |node|
|
7
|
-
# ------------------------ WARNING ------------------------
|
8
|
-
# This code is to support #remove_link which uses mark_for_destruction
|
9
|
-
# This code is necessary to clean everything up.
|
10
|
-
# Calling save on this answer won't automatically go to its next_question -> node_maps and clean everything up
|
11
|
-
(@ancestor_marked_for_destruction || []).each { |i|
|
12
|
-
i.destroy
|
13
|
-
}
|
14
|
-
end
|
15
|
-
|
16
3
|
# Answer nodes are valid if their questions are valid!
|
17
4
|
# Validate this node against an instance
|
18
5
|
def validate_node(instance)
|
@@ -60,34 +47,18 @@ module ActiveRecordSurvey
|
|
60
47
|
# Removes the link
|
61
48
|
# TODO - does this work when saved??
|
62
49
|
def remove_link
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
self.node_maps.each_with_index { |
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
child.node.node_maps.select { |nm|
|
77
|
-
nm.parent && nm.parent.node == self
|
78
|
-
}.each_with_index { |nm,ii|
|
79
|
-
# Cleans up all the excess node_maps from the old linkage
|
80
|
-
if linked_somewhere_else || nm.parent && nm.parent.node == self && ii > 0
|
81
|
-
nm.mark_for_destruction
|
82
|
-
@ancestor_marked_for_destruction << nm
|
83
|
-
node_map_removed = true
|
84
|
-
end
|
85
|
-
}
|
86
|
-
|
87
|
-
# Should not know about parent
|
88
|
-
child.parent = nil if node_map_removed
|
89
|
-
|
90
|
-
!node_map_removed
|
50
|
+
# not linked to a question - nothing to remove!
|
51
|
+
return true if (question = self.next_question).nil?
|
52
|
+
|
53
|
+
self.node_maps.each_with_index { |answer_nm, answer_nm_i|
|
54
|
+
answer_nm.children = answer_nm.children.select { |question_nm|
|
55
|
+
# This node_map links to the question
|
56
|
+
if question_nm.node === question
|
57
|
+
question_nm.parent = nil
|
58
|
+
false
|
59
|
+
else
|
60
|
+
true
|
61
|
+
end
|
91
62
|
}
|
92
63
|
}
|
93
64
|
end
|
@@ -104,7 +75,7 @@ module ActiveRecordSurvey
|
|
104
75
|
if self.node_maps.select { |i|
|
105
76
|
i.children.length === 0
|
106
77
|
}.length === 0
|
107
|
-
raise RuntimeError.new "This answer has already been linked"
|
78
|
+
raise RuntimeError.new "This answer has already been linked"
|
108
79
|
end
|
109
80
|
|
110
81
|
# Attempt to find an unused to_node node_map
|
@@ -7,7 +7,7 @@ describe ActiveRecordSurvey::Node::Answer, :answer_spec => true do
|
|
7
7
|
@survey.save
|
8
8
|
end
|
9
9
|
|
10
|
-
describe '#build_link'
|
10
|
+
describe '#build_link' do
|
11
11
|
it 'should throw error when build_link creates an infinite loop' do
|
12
12
|
survey = ActiveRecordSurvey::Survey.new()
|
13
13
|
|
@@ -30,6 +30,39 @@ describe ActiveRecordSurvey::Node::Answer, :answer_spec => true do
|
|
30
30
|
q2_a1.build_link(q3)
|
31
31
|
expect{q3_a1.build_link(q1)}.to raise_error(RuntimeError) # This should throw exception
|
32
32
|
end
|
33
|
+
|
34
|
+
it 'should be allowed before saving after calling #remove_link' do
|
35
|
+
survey = ActiveRecordSurvey::Survey.new()
|
36
|
+
|
37
|
+
q1 = ActiveRecordSurvey::Node::Question.new(:text => "Q1")
|
38
|
+
survey.build_question(q1)
|
39
|
+
q1_a1 = ActiveRecordSurvey::Node::Answer.new(:text => "Q1 A1")
|
40
|
+
q1.build_answer(q1_a1)
|
41
|
+
|
42
|
+
q2 = ActiveRecordSurvey::Node::Question.new(:text => "Q2")
|
43
|
+
survey.build_question(q2)
|
44
|
+
q3 = ActiveRecordSurvey::Node::Question.new(:text => "Q3")
|
45
|
+
survey.build_question(q3)
|
46
|
+
|
47
|
+
expect(survey.as_map).to eq([{"text"=>"Q1", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 A1", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}]}, {"text"=>"Q2", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}, {"text"=>"Q3", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}])
|
48
|
+
|
49
|
+
q1_a1.build_link(q2)
|
50
|
+
expect(q1_a1.next_question).to eq(q2)
|
51
|
+
expect(survey.as_map).to eq([{"text"=>"Q1", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 A1", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Answer", :children=>[{"text"=>"Q2", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}]}]}, {"text"=>"Q3", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}])
|
52
|
+
|
53
|
+
q1_a1.remove_link
|
54
|
+
expect(q1_a1.next_question).to eq(nil)
|
55
|
+
expect(survey.as_map).to eq([{"text"=>"Q1", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 A1", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Answer", :children=>[]}]}, {"text"=>"Q2", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}, {"text"=>"Q3", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}])
|
56
|
+
|
57
|
+
q1_a1.build_link(q3)
|
58
|
+
expect(q1_a1.next_question).to eq(q3)
|
59
|
+
expect(survey.as_map).to eq([{"text"=>"Q1", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 A1", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Answer", :children=>[{"text"=>"Q3", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}]}]}, {"text"=>"Q2", :id=>nil, :node_id=>nil, :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}])
|
60
|
+
|
61
|
+
survey.save
|
62
|
+
expect(survey.as_map).to eq([{"text"=>"Q1", :id=>190, :node_id=>58, :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 A1", :id=>191, :node_id=>59, :type=>"ActiveRecordSurvey::Node::Answer", :children=>[{"text"=>"Q3", :id=>192, :node_id=>60, :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}]}]}, {"text"=>"Q2", :id=>193, :node_id=>61, :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}])
|
63
|
+
survey.reload
|
64
|
+
expect(survey.as_map).to eq([{"text"=>"Q1", :id=>190, :node_id=>58, :type=>"ActiveRecordSurvey::Node::Question", :children=>[{"text"=>"Q1 A1", :id=>191, :node_id=>59, :type=>"ActiveRecordSurvey::Node::Answer", :children=>[{"text"=>"Q3", :id=>192, :node_id=>60, :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}]}]}, {"text"=>"Q2", :id=>193, :node_id=>61, :type=>"ActiveRecordSurvey::Node::Question", :children=>[]}])
|
65
|
+
end
|
33
66
|
end
|
34
67
|
|
35
68
|
describe '#remove_link' do
|
data/spec/spec_helper.rb
CHANGED
@@ -6,6 +6,8 @@ require 'active_record_survey'
|
|
6
6
|
|
7
7
|
require_relative '../spec/factories/active_record_survey/survey'
|
8
8
|
|
9
|
+
require 'database_cleaner'
|
10
|
+
|
9
11
|
# Trigger AR to initialize
|
10
12
|
ActiveRecord::Base
|
11
13
|
|
@@ -61,6 +63,12 @@ RSpec.configure do |config|
|
|
61
63
|
config.include FactoryGirl::Syntax::Methods
|
62
64
|
config.after(:each) do
|
63
65
|
end
|
66
|
+
|
67
|
+
config.before(:suite) do
|
68
|
+
DatabaseCleaner.strategy = :transaction
|
69
|
+
DatabaseCleaner.clean_with(:truncation)
|
70
|
+
end
|
71
|
+
|
64
72
|
config.expect_with :rspec do |c|
|
65
73
|
c.syntax = :expect
|
66
74
|
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.
|
4
|
+
version: 0.1.22
|
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-12-
|
11
|
+
date: 2015-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -58,6 +58,20 @@ dependencies:
|
|
58
58
|
- - ">"
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: database_cleaner
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
61
75
|
- !ruby/object:Gem::Dependency
|
62
76
|
name: factory_girl
|
63
77
|
requirement: !ruby/object:Gem::Requirement
|