Sha256: 869468de65c9d8e06e3272d8d7209539a60dcb5c1f19f36978da05d3dcccee3a

Contents?: true

Size: 1.37 KB

Versions: 8

Compression:

Stored size: 1.37 KB

Contents

module ActiveRecordSurvey
	class NodeMap < ::ActiveRecord::Base
		self.table_name = "active_record_survey_node_maps"
		belongs_to :node, :foreign_key => :active_record_survey_node_id
		belongs_to :survey, :class_name => "ActiveRecordSurvey::Survey", :foreign_key => :active_record_survey_id
		acts_as_nested_set :scope => [:active_record_survey_id]

		validates_presence_of :survey

		after_initialize do |i|
			# Required for all functions to work without creating
			i.survey.node_maps << self if i.new_record? && i.survey
		end

		# Recursively creates a copy of this entire node_map
		def recursive_clone
			node_map = self.node.node_maps.build(:survey => self.survey, :node => self.node)
			self.children.each { |child_node|
				node_map.children << child_node.recursive_clone
			}
			node_map
		end

		def as_map(node_maps = nil)
			children = (node_maps.nil?)? self.children : node_maps.select { |i|
				i.parent == self
			}

			{
				:id => self.id,
				:node_id => self.node.id,
				:type => self.node.class.to_s,
				:children => children.collect { |i|
					i.as_map(node_maps)
				}
			}
		end

		# Gets all the child nodes until one is not an ancestor of klass
		def children_until_node_not_ancestor_of(klass)
			if !self.node.class.ancestors.include?(klass)
				return []
			end

			[self] + self.children.collect { |i|
				i.children_until_node_not_ancestor_of(klass)
			}
		end
	end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
active_record_survey-0.1.20 lib/active_record_survey/node_map.rb
active_record_survey-0.1.19 lib/active_record_survey/node_map.rb
active_record_survey-0.1.18 lib/active_record_survey/node_map.rb
active_record_survey-0.1.17 lib/active_record_survey/node_map.rb
active_record_survey-0.1.16 lib/active_record_survey/node_map.rb
active_record_survey-0.1.15 lib/active_record_survey/node_map.rb
active_record_survey-0.1.14 lib/active_record_survey/node_map.rb
active_record_survey-0.1.13 lib/active_record_survey/node_map.rb