active_record_survey-node_map_group 0.0.2 → 0.0.4

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: 6be44a566609f039efa07311cc7500bde87b4f57
4
- data.tar.gz: 1db16debb15aff55240809e22967025dcb4d0e40
3
+ metadata.gz: 11e86c11a387a583868a8390049c0bcded8eda47
4
+ data.tar.gz: 6592788ffb17ebf285872edd8f970f328d727d75
5
5
  SHA512:
6
- metadata.gz: 89d3f6d16b496a04f85561210fe40fb47e0300b31f0694a4bfeae461e80452a54bb83a27ab9963207259648becda9f361a9351ee5f8222778d0808959245ae23
7
- data.tar.gz: cb1322b43bc6f585a755fd0df7b5c8455dd57f90de69c01778cf4b145430d3dee2e9aa266400c239e1602559267921b030873e4ab29c2461d91fbe662ae585bf
6
+ metadata.gz: 57db7f700994af3fedfb929a9aeffb31776155f51872acf1e5ac13a195fda496ca8eca5710a77b8ae563d6fb9d979929d036084ccfec45893b0a4ea0da163f2a
7
+ data.tar.gz: 77125d8171294a2fb73b22f5546f2662f68273e946dd0a3fa8c5424f86b69c8b7616cc343dfdfd26754a00c89721a659fb2498ebb77f93d503adf88954ffb9a5
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in active_record_survey-node_map_group.gemspec
4
4
  gemspec
5
+
6
+ #gem 'active_record_survey', :path => "../active_record_survey"
@@ -0,0 +1,9 @@
1
+ module ActiveRecordSurvey
2
+ class NodeMap < ActiveRecord::Base
3
+ module ClassMethods
4
+ def self.extended(base)
5
+ base.belongs_to :node_map_group, :class_name => "ActiveRecordSurvey::NodeMapGroup", :foreign_key => :active_record_survey_api_node_map_group_id
6
+ end
7
+ end
8
+ end
9
+ end
@@ -8,10 +8,14 @@ require "active_record_survey/node_map_group/version"
8
8
  require "active_record_survey/node_map_group/class_methods"
9
9
  require "active_record_survey/node_map_group/instance_methods"
10
10
 
11
+ require "active_record_survey/node_map/class_methods"
12
+
11
13
  module ActiveRecordSurvey
12
14
  class NodeMapGroup < ActiveRecord::Base
13
15
  end
14
16
  end
15
17
 
16
18
  ActiveRecordSurvey::NodeMapGroup.send :include, ActiveRecordSurvey::NodeMapGroup::InstanceMethods
17
- ActiveRecordSurvey::NodeMapGroup.send :extend, ActiveRecordSurvey::NodeMapGroup::ClassMethods
19
+ ActiveRecordSurvey::NodeMapGroup.send :extend, ActiveRecordSurvey::NodeMapGroup::ClassMethods
20
+
21
+ ActiveRecordSurvey::NodeMap.send :extend, ActiveRecordSurvey::NodeMap::ClassMethods
@@ -25,6 +25,83 @@ module ActiveRecordSurvey
25
25
  (results.length > 0 && !results.include?(false))
26
26
  end
27
27
 
28
+ # Builds questions
29
+ #
30
+ # From an array of questions (and question/answer key value pairs) builds all the node_map_groups to be associated with this node_map_group
31
+ #
32
+ # * *Args* :
33
+ # - +array+ -> The array of questions
34
+ # * *Returns* :
35
+ # - +boolean+ -> Whether successul or not
36
+ # * *Raises* :
37
+ # - +StandardError+ -> Survey has not yet been associated
38
+ # - +ArgumentError+ -> Question or Answers do not exist
39
+ #
40
+ def build_questions(questions)
41
+ # Must be associated with a survey first
42
+ raise StandardError, "SURVEY_MISSING" if self.survey.nil?
43
+
44
+ questions.each { |datum|
45
+ # Find question by inputs
46
+ question = case datum.class.to_s
47
+ when 'Fixnum' then ActiveRecordSurvey::Node::Question.where(:id => datum, :survey => self.survey).first
48
+ when 'Hash' then ActiveRecordSurvey::Node::Question.where(:id => datum[:question_id], :survey => self.survey).first
49
+ else nil
50
+ end
51
+
52
+ # Don't continue if question does not exist
53
+ raise ArgumentError, "INVALID_QUESTION" if question.nil?
54
+
55
+ # Find answer by inputs
56
+ previous_node = case datum.class.to_s
57
+ when 'Hash' then ActiveRecordSurvey::Node.where(:id => datum[:previous_id], :survey => self.survey).first
58
+ else nil
59
+ end
60
+
61
+ # Via Survey object, find all the node_maps that match the criteria passed
62
+ question_node_maps = self.survey.node_maps.select { |node_map|
63
+ node_map.node == question
64
+ }
65
+
66
+ # Filter list by the previous node
67
+ question_node_maps.select! { |node_map| previous_node.node_maps.select { |i| i.children.include?(node_map) }.length > 0 } unless previous_node.nil?
68
+
69
+ # Filter by existing node maps
70
+ filtered = question_node_maps.select { |new_node_map|
71
+ # Only keep new node_maps if there is an existing node_map that is an ancestor of it
72
+ self.node_maps.select { |existing_node_map|
73
+ # This is the only path we want - throw out the others
74
+ new_node_map.is_decendant_of?(existing_node_map)
75
+ }.length > 0
76
+ }
77
+
78
+ # At least one existing node_map was an ancestor of the one we're adding
79
+ question_node_maps = filtered if filtered.length != 0
80
+
81
+ # Now prune existing node maps
82
+ # We are removing node_maps that are apart of paths that don't logically make sense anymore
83
+ self.node_maps = self.node_maps.select { |existing_node_map|
84
+ keep = true
85
+ question_node_maps.select { |new_node_map|
86
+ if existing_node_map.node != new_node_map.node && !existing_node_map.is_decendant_of?(new_node_map) && ! new_node_map.is_decendant_of?(existing_node_map)
87
+ existing_node_map.node_map_group = nil
88
+ keep = false
89
+ end
90
+ }
91
+ keep
92
+ }
93
+
94
+ # After our investigation - no nodes found!
95
+ raise ArgumentError, "QUESTION_NOT_FOUND" if question_node_maps.length === 0
96
+
97
+ # Add the remaining nodes
98
+ question_node_maps.each { |node_map|
99
+ self.node_maps << node_map
100
+ }
101
+ }
102
+ true
103
+ end
104
+
28
105
  private
29
106
  def validate_node_maps
30
107
  self.node_maps.each { |node_map|
@@ -39,6 +116,15 @@ module ActiveRecordSurvey
39
116
  # Must inherit from ActiveRecordSurvey::Node::Question
40
117
  return false if !potential_node_map.node.class.ancestors.include?(::ActiveRecordSurvey::Node::Question)
41
118
 
119
+ # A question also linked to this node_group leads to this node
120
+ # BUT - that previous question actually points to multiple questions
121
+ return false if self.node_maps.select { |node_map|
122
+ # This node map is the question before the one we're validating
123
+ node_map.node.next_questions.include?(potential_node_map.node)
124
+ }.collect { |node_map|
125
+ node_map.node.next_questions.length === 1
126
+ }.include?(false)
127
+
42
128
  # Nothing else added - so yep - valid!
43
129
  return true if self.node_maps.length === 0
44
130
 
@@ -2,6 +2,6 @@ require "active_record"
2
2
 
3
3
  module ActiveRecordSurvey
4
4
  class NodeMapGroup < ActiveRecord::Base
5
- VERSION = "0.0.2"
5
+ VERSION = "0.0.4"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_survey-node_map_group
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Butch Marshall
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-08 00:00:00.000000000 Z
11
+ date: 2016-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_record_survey
@@ -124,6 +124,7 @@ files:
124
124
  - active_record_survey-node_map_group.gemspec
125
125
  - bin/console
126
126
  - bin/setup
127
+ - lib/active_record_survey/node_map/class_methods.rb
127
128
  - lib/active_record_survey/node_map_group.rb
128
129
  - lib/active_record_survey/node_map_group/class_methods.rb
129
130
  - lib/active_record_survey/node_map_group/compatibility.rb