active_record_survey 0.1.0 → 0.1.1

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: 240ce460ae63c7c51f4a360cbe24280df0b5f502
4
- data.tar.gz: 9420ee37dc83a9cc95e8dbfaa245129671b4443b
3
+ metadata.gz: dd8d08ee8e2a5a3187e5c5aa11eee88f01f32768
4
+ data.tar.gz: caffbf9c42a143567168a2d2dae25bc439d06173
5
5
  SHA512:
6
- metadata.gz: 1add51d1fc6436ac5060a9de61af79d0778169dc7432b01939595cb898a9c9b043e7325dccb05c2d3a6b87791713b1228face4ee1b7c9fdf616a88c84b8a5ab9
7
- data.tar.gz: 8e008c6a8f146e3e705d6ddd789945b10460d759a617a71d5f397f7b4638367ecb555aaff1ccc076ba83df3c157e74784056f504f2525a198d58ee44eb20178b
6
+ metadata.gz: fa4a3bd1c105be6a800f57b94c5cd508f2dac3f73a4dcfb4878173627ef91a666900d1271dd9c362d1661b955e283eefb1f100b8555bb4c28b9e882f825c9a23
7
+ data.tar.gz: 9b75335474d05f2b7aac0c9fffd60ca02b48bb30f2dab8e3aff5614747bc11e91dc775e12307ead5ec3b197dc5a4df1eb0e93890f26fca6ffd25a9783f84df8b
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![Gem Version](https://badge.fury.io/rb/active_record_survey.svg)](http://badge.fury.io/rb/active_record_survey)
2
+ [![Build Status](https://travis-ci.org/butchmarshall/active_record_survey.svg?branch=master)](https://travis-ci.org/butchmarshall/active_record_survey)
3
+
1
4
  # ActiveRecordSurvey
2
5
 
3
6
  An attempt at a more versatile data structure for making and taking surveys.
@@ -13,6 +13,8 @@ require "active_record_survey/node"
13
13
  require "active_record_survey/node/question"
14
14
  require "active_record_survey/node/answer"
15
15
  require "active_record_survey/node/answer/text"
16
+ require "active_record_survey/node/answer/rank"
17
+ require "active_record_survey/node/answer/scale"
16
18
  require "active_record_survey/node/answer/boolean"
17
19
 
18
20
  require "active_record_survey/node_map"
@@ -6,7 +6,9 @@ module ActiveRecordSurvey
6
6
 
7
7
  # By default all values are accepted
8
8
  def validate_instance_node(instance_node)
9
- true
9
+ !self.node_validations.collect { |node_validation|
10
+ node_validation.validate_instance_node(instance_node, self)
11
+ }.include?(false)
10
12
  end
11
13
 
12
14
  # Whether there is a valid answer path from this node to the root node for the instance
@@ -3,6 +3,8 @@ module ActiveRecordSurvey
3
3
  class Node::Answer::Boolean < Node::Answer
4
4
  # Only boolean values
5
5
  def validate_instance_node(instance_node)
6
+ # super - all validations on this node pass
7
+ super &&
6
8
  !instance_node.value.to_s.match(/^[0|1]$/).nil?
7
9
  end
8
10
  end
@@ -0,0 +1,12 @@
1
+ module ActiveRecordSurvey
2
+ # Rank in relation to parent/children
3
+ class Node::Answer::Rank < Node::Answer
4
+ # Accept integer or empty values
5
+ def validate_instance_node(instance_node)
6
+ # super - all validations on this node pass
7
+ super &&
8
+ (instance_node.value.to_s.empty? ||
9
+ !instance_node.value.to_s.match(/^\d+$/).nil?)
10
+ end
11
+ end
12
+ end
@@ -1,5 +1,11 @@
1
1
  module ActiveRecordSurvey
2
2
  # Can hold a value on a scale (e.g. from 0-10)
3
3
  class Node::Answer::Scale < Node::Answer
4
+ # Accept integer, float, or empty values
5
+ def validate_instance_node(instance_node)
6
+ # super - all validations on this node pass
7
+ super &&
8
+ (instance_node.value.to_s.empty? || !instance_node.value.to_s.match(/^(\d+(\.\d+)?)$/).nil?)
9
+ end
4
10
  end
5
11
  end
@@ -3,5 +3,10 @@ module ActiveRecordSurvey
3
3
  class NodeValidation < ::ActiveRecord::Base
4
4
  self.table_name = "active_record_survey_node_validations"
5
5
  belongs_to :node, :class_name => "ActiveRecordSurvey::Node", :foreign_key => :active_record_survey_node_id
6
+
7
+ # By default everything is valid! WOOO!
8
+ def validate_instance_node(instance_node, node = nil)
9
+ true
10
+ end
6
11
  end
7
12
  end
@@ -1,4 +1,9 @@
1
1
  module ActiveRecordSurvey
2
- class NodeValidation::MaximumLength < Node
2
+ # Ensure the instance_node has a length less than the maximum
3
+ class NodeValidation::MaximumLength < NodeValidation
4
+ # Validate the instance_node value
5
+ def validate_instance_node(instance_node, node = nil)
6
+ instance_node.value.to_s.length <= self.value.to_i
7
+ end
3
8
  end
4
9
  end
@@ -1,4 +1,10 @@
1
1
  module ActiveRecordSurvey
2
- class NodeValidation::MaximumValue < Node
2
+ # Ensure the instance_node has a value less than the maximum
3
+ class NodeValidation::MaximumValue < NodeValidation
4
+ # Validate the instance_node value is less than the maximum
5
+ def validate_instance_node(instance_node, node = nil)
6
+ !instance_node.value.to_s.empty? &&
7
+ instance_node.value.to_f <= self.value.to_f
8
+ end
3
9
  end
4
10
  end
@@ -1,4 +1,9 @@
1
1
  module ActiveRecordSurvey
2
- class NodeValidation::MinimumLength < Node
2
+ # Ensure the instance_node has a value greater than the minimum
3
+ class NodeValidation::MinimumLength < NodeValidation
4
+ # Validate the instance_node value is greater than the minimum
5
+ def validate_instance_node(instance_node, node = nil)
6
+ instance_node.value.to_s.length >= self.value.to_i
7
+ end
3
8
  end
4
9
  end
@@ -1,4 +1,10 @@
1
1
  module ActiveRecordSurvey
2
- class NodeValidation::MinimumValue < Node
2
+ # Ensure the instance_node has a value greater than the minimum
3
+ class NodeValidation::MinimumValue < NodeValidation
4
+ # Validate the instance_node value is greater than the minimum
5
+ def validate_instance_node(instance_node, node = nil)
6
+ !instance_node.value.to_s.empty? &&
7
+ instance_node.value.to_f >= self.value.to_f
8
+ end
3
9
  end
4
10
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordSurvey
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ActiveRecordSurvey::Node::Answer::Boolean, :focus => true do
3
+ describe ActiveRecordSurvey::Node::Answer::Boolean do
4
4
  describe 'a boolean survey is' do
5
5
  before(:all) do
6
6
  @survey = ActiveRecordSurvey::Survey.new
@@ -0,0 +1,159 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecordSurvey::Node::Answer::Scale, :focus => true do
4
+ describe 'survey is' do
5
+ before(:all) do
6
+ @survey = ActiveRecordSurvey::Survey.new
7
+
8
+ @q1 = ActiveRecordSurvey::Node::Question.new(:text => "Please select one")
9
+ @q1_a1 = ActiveRecordSurvey::Node::Answer::Scale.new(:text => "How much do you like dogs?")
10
+ @q1_a2 = ActiveRecordSurvey::Node::Answer::Scale.new(:text => "How much do you like cats?")
11
+ @q1_a3 = ActiveRecordSurvey::Node::Answer::Scale.new(:text => "How much do you like mice?")
12
+
13
+ nodes = @survey.build_question(@q1, [@q1_a1])
14
+ nodes = @survey.build_question(@q1_a2, [], nodes[1])
15
+ nodes = @survey.build_question(@q1_a3, [], nodes[0])
16
+
17
+ @survey.save
18
+ end
19
+
20
+ describe 'valid when' do
21
+ it 'accepts an integer value' do
22
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
23
+ instance.instance_nodes.build(
24
+ :instance => instance,
25
+ :node => @q1_a1,
26
+ :value => 100
27
+ )
28
+ instance.save
29
+
30
+ expect(instance.valid?).to be(true)
31
+ end
32
+
33
+ it 'accepts a float value' do
34
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
35
+ instance.instance_nodes.build(
36
+ :instance => instance,
37
+ :node => @q1_a1,
38
+ :value => 10.5
39
+ )
40
+ instance.save
41
+
42
+ expect(instance.valid?).to be(true)
43
+ end
44
+
45
+ it 'accepts an empty value' do
46
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
47
+ instance.instance_nodes.build(
48
+ :instance => instance,
49
+ :node => @q1_a1,
50
+ )
51
+ instance.save
52
+
53
+ expect(instance.valid?).to be(true)
54
+ end
55
+
56
+ describe ActiveRecordSurvey::NodeValidation::MinimumValue do
57
+ describe 'valid when' do
58
+ it 'has a value greater than the minimum' do
59
+ @q1_a1.node_validations << ActiveRecordSurvey::NodeValidation::MinimumValue.new(
60
+ :node => @q1_a1,
61
+ :value => 4
62
+ )
63
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
64
+ instance.instance_nodes.build(
65
+ :instance => instance,
66
+ :node => @q1_a1,
67
+ :value => 5,
68
+ )
69
+ instance.save
70
+
71
+ expect(instance.valid?).to be(true)
72
+ end
73
+ end
74
+
75
+ describe 'invalid when' do
76
+ it 'has a value less than the minimum' do
77
+ @q1_a1.node_validations << ActiveRecordSurvey::NodeValidation::MinimumValue.new(
78
+ :node => @q1_a1,
79
+ :value => 6
80
+ )
81
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
82
+ instance.instance_nodes.build(
83
+ :instance => instance,
84
+ :node => @q1_a1,
85
+ :value => 5,
86
+ )
87
+ instance.save
88
+
89
+ expect(instance.valid?).to be(false)
90
+ end
91
+ end
92
+ end
93
+
94
+ describe ActiveRecordSurvey::NodeValidation::MaximumValue do
95
+ describe 'valid when' do
96
+ it 'has a value less than the maximum' do
97
+ @q1_a1.node_validations << ActiveRecordSurvey::NodeValidation::MaximumValue.new(
98
+ :node => @q1_a1,
99
+ :value => 15
100
+ )
101
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
102
+ instance.instance_nodes.build(
103
+ :instance => instance,
104
+ :node => @q1_a1,
105
+ :value => 8,
106
+ )
107
+ instance.save
108
+
109
+ expect(instance.valid?).to be(true)
110
+ end
111
+ end
112
+
113
+ describe 'invalid when' do
114
+ it 'has a value greater than the maximum' do
115
+ @q1_a1.node_validations << ActiveRecordSurvey::NodeValidation::MaximumValue.new(
116
+ :node => @q1_a1,
117
+ :value => 15
118
+ )
119
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
120
+ instance.instance_nodes.build(
121
+ :instance => instance,
122
+ :node => @q1_a1,
123
+ :value => 20,
124
+ )
125
+ instance.save
126
+
127
+ expect(instance.valid?).to be(false)
128
+ end
129
+ end
130
+ end
131
+ end
132
+
133
+ describe 'invalid when' do
134
+ it 'rejects an alphabetical value' do
135
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
136
+ instance.instance_nodes.build(
137
+ :instance => instance,
138
+ :node => @q1_a1,
139
+ :value => "1a2"
140
+ )
141
+ instance.save
142
+
143
+ expect(instance.valid?).to be(false)
144
+ end
145
+
146
+ it 'rejects when not answered in order' do
147
+ instance = ActiveRecordSurvey::Instance.new(:survey => @survey)
148
+ instance.instance_nodes.build(
149
+ :instance => instance,
150
+ :node => @q1_a2,
151
+ :value => 20
152
+ )
153
+ instance.save
154
+
155
+ expect(instance.valid?).to be(false)
156
+ end
157
+ end
158
+ end
159
+ 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.0
4
+ version: 0.1.1
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-23 00:00:00.000000000 Z
11
+ date: 2015-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -126,6 +126,7 @@ files:
126
126
  - lib/active_record_survey/node.rb
127
127
  - lib/active_record_survey/node/answer.rb
128
128
  - lib/active_record_survey/node/answer/boolean.rb
129
+ - lib/active_record_survey/node/answer/rank.rb
129
130
  - lib/active_record_survey/node/answer/scale.rb
130
131
  - lib/active_record_survey/node/answer/text.rb
131
132
  - lib/active_record_survey/node/question.rb
@@ -142,6 +143,7 @@ files:
142
143
  - lib/generators/active_record_survey/next_migration_version.rb
143
144
  - lib/generators/active_record_survey/templates/migration_0.1.0.rb
144
145
  - spec/active_record_survey/node/answer/boolean_spec.rb
146
+ - spec/active_record_survey/node/answer/scale_spec.rb
145
147
  - spec/active_record_survey/survey_spec.rb
146
148
  - spec/active_record_survey_spec.rb
147
149
  - spec/spec_helper.rb
@@ -171,6 +173,7 @@ specification_version: 4
171
173
  summary: Surveys using activerecord
172
174
  test_files:
173
175
  - spec/active_record_survey/node/answer/boolean_spec.rb
176
+ - spec/active_record_survey/node/answer/scale_spec.rb
174
177
  - spec/active_record_survey/survey_spec.rb
175
178
  - spec/active_record_survey_spec.rb
176
179
  - spec/spec_helper.rb