Sha256: 7e41dd3c8afb8954bc42ca940712cce852870c635cbb28674ceb36f0d3775796

Contents?: true

Size: 1.19 KB

Versions: 10

Compression:

Stored size: 1.19 KB

Contents

module ActiveRecordSurvey
	# Boolean answers can have values 0|1
	class Node::Answer::Boolean < Node::Answer
		# Only boolean values
		def validate_instance_node(instance_node)
			# super - all validations on this node pass
			super &&
			!instance_node.value.to_s.match(/^[0|1]$/).nil?
		end

		# Boolean answers are considered answered if they have a value of "1"
		def is_answered_for_instance?(instance)
			if instance_node = self.instance_node_for_instance(instance)
				# Instance node is answered "1"
				(instance_node.value.to_i === 1)
			end
		end

		# Boolean nodes are different - they must find the final boolean node added and add to it
		def build_answer(question_node, survey)
			# No node_maps exist yet from this question
			if question_node.node_maps.length === 0
				# Build our first node-map
				question_node.node_maps.build(:node => question_node, :survey => survey)
			end

			last_in_chain = question_node.answers.last || question_node

			# Each instance of this question needs the answer hung from it
			last_in_chain.node_maps.each { |node_map|
				answer_node_map = self.node_maps.build(:node => self, :survey => survey)
				node_map.children << answer_node_map
			}

			true
		end
	end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
active_record_survey-0.1.22 lib/active_record_survey/node/answer/boolean.rb
active_record_survey-0.1.21 lib/active_record_survey/node/answer/boolean.rb
active_record_survey-0.1.20 lib/active_record_survey/node/answer/boolean.rb
active_record_survey-0.1.19 lib/active_record_survey/node/answer/boolean.rb
active_record_survey-0.1.18 lib/active_record_survey/node/answer/boolean.rb
active_record_survey-0.1.17 lib/active_record_survey/node/answer/boolean.rb
active_record_survey-0.1.16 lib/active_record_survey/node/answer/boolean.rb
active_record_survey-0.1.15 lib/active_record_survey/node/answer/boolean.rb
active_record_survey-0.1.14 lib/active_record_survey/node/answer/boolean.rb
active_record_survey-0.1.13 lib/active_record_survey/node/answer/boolean.rb