Sha256: 42dc51cf99f3603e69f8df102f5d57765d0744faf1053a7e8338d4b8c05a4f74

Contents?: true

Size: 1.06 KB

Versions: 40

Compression:

Stored size: 1.06 KB

Contents

module ActiveRecordSurvey
	# Ensure the a minimum number of answers are made
	class NodeValidation::MinimumAnswer < NodeValidation
		# Validate the instance_node to ensure a minimum number of answers are made
		def validate_instance_node(instance_node, question_node = nil)
			# Only makes sense for questions to have minimum answers
			if !question_node.class.ancestors.include?(::ActiveRecordSurvey::Node::Question)
				return false 
			end

			instance = instance_node.instance

			# Go through the node_map of this node
			total_answered = question_node.node_maps.collect { |question_node_map|
				# Get all children until a childs node isn't an answer
				question_node_map.children.collect { |i|
					i.children_until_node_not_ancestor_of(::ActiveRecordSurvey::Node::Answer)
				}.flatten.collect { |i|
					i.node.is_answered_for_instance?(instance)
				}
			}.flatten.select { |i| i }.count

			is_valid = (total_answered >= self.value.to_i)

			instance_node.errors[:base] << { :nodes => { question_node.id => ["MINIMUM_ANSWER"] } } if !is_valid

			is_valid
		end
	end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
active_record_survey-0.1.28 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.27 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.26 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.25 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.23 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.22 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.21 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.20 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.19 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.18 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.17 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.16 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.15 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.14 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.13 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.12 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.11 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.10 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.9 lib/active_record_survey/node_validation/minimum_answer.rb
active_record_survey-0.1.8 lib/active_record_survey/node_validation/minimum_answer.rb