Class: ActiveRecordSurvey::Survey

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/active_record_survey/survey.rb

Instance Method Summary collapse

Instance Method Details

#as_map(*args) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/active_record_survey/survey.rb', line 39

def as_map(*args)
	options = args.extract_options!
	options[:node_maps] ||= self.node_maps

	self.node_maps.select { |i| !i.parent && !i.marked_for_destruction? }.collect { |i|
		i.as_map(options)
	}
end

#build_first_question(question_node) ⇒ Object

Builds first question



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/active_record_survey/survey.rb', line 13

def build_first_question(question_node)
	if !question_node.class.ancestors.include?(::ActiveRecordSurvey::Node::Question)
		raise ArgumentError.new "must inherit from ::ActiveRecordSurvey::Node::Question"
	end

	question_node_maps = self.node_maps.select { |i| i.node == question_node && !i.marked_for_destruction? }

	# No node_maps exist yet from this question
	if question_node_maps.length === 0
		# Build our first node-map
		question_node_maps << self.node_maps.build(:node => question_node, :survey => self)
	end
end

#edgesObject

All the connective edges



28
29
30
31
32
33
34
35
36
37
# File 'lib/active_record_survey/survey.rb', line 28

def edges
	self.node_maps.select { |i| !i.marked_for_destruction? }.select { |i|
		i.node && i.parent
	}.collect { |i|
		{
			:source => i.parent.node.id,
			:target => i.node.id,
		}
	}.uniq
end

#root_nodeObject



8
9
10
# File 'lib/active_record_survey/survey.rb', line 8

def root_node
	self.node_maps.includes(:node).select { |i| i.depth === 0 }.first
end