Sha256: 29d1d3b12537d2105fc971cbe3ab21743af49afff0af780e88684b5e6745fabb

Contents?: true

Size: 1.48 KB

Versions: 10

Compression:

Stored size: 1.48 KB

Contents

module ActiveRecordSurvey
	class InstanceNode < ::ActiveRecord::Base
		self.table_name = "active_record_survey_instance_nodes"
		belongs_to :instance, :class_name => "ActiveRecordSurvey::Instance", :foreign_key => :active_record_survey_instance_id
		belongs_to :node, :class_name => "ActiveRecordSurvey::Node", :foreign_key => :active_record_survey_node_id

		validates_presence_of :instance

		validate do |instance_node|
			# No node to begin with!
			if self.node.nil?
				instance_node.errors[:base] << "INVALID_NODE"
			else
				# This instance_node has no valid path to the root node
				if !self.node.instance_node_path_to_root?(self)
					instance_node.errors[:base] << "INVALID_PATH"
				end

				parent_nodes = self.node.survey.node_maps.select { |i| i.node == self.node }.collect { |j| j.parent }

				# Two instance_nodes on the same node for this instance
				if self.instance.instance_nodes.select { |i|
						# We don't care about paths that are going to be deleted
						!i.marked_for_destruction?
					}.select { |i|
						# And the two arrays
						# Two votes share a parent (this means a question has two answers for this instance)
						(i.node.survey.node_maps.select { |j| i.node == j.node }.collect { |j| j.parent } & parent_nodes).length > 0
					}.length > 1
					instance_node.errors[:base] << "DUPLICATE_PATH"
				end

				# Validate against the associated node
				if !self.node.validate_instance_node(self)
					instance_node.errors[:base] << "INVALID"
				end
			end
		end
	end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
active_record_survey-0.1.49 lib/active_record_survey/instance_node.rb
active_record_survey-0.1.48 lib/active_record_survey/instance_node.rb
active_record_survey-0.1.47 lib/active_record_survey/instance_node.rb
active_record_survey-0.1.46 lib/active_record_survey/instance_node.rb
active_record_survey-0.1.45 lib/active_record_survey/instance_node.rb
active_record_survey-0.1.44 lib/active_record_survey/instance_node.rb
active_record_survey-0.1.43 lib/active_record_survey/instance_node.rb
active_record_survey-0.1.42 lib/active_record_survey/instance_node.rb
active_record_survey-0.1.41 lib/active_record_survey/instance_node.rb
active_record_survey-0.1.40 lib/active_record_survey/instance_node.rb