Sha256: 7846e0bce485bbfa38b1c37ade9d766bbf04d3d82ffb6916ec6957a0e4331696

Contents?: true

Size: 1012 Bytes

Versions: 7

Compression:

Stored size: 1012 Bytes

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]
		
		after_initialize do |i|
			i.survey.node_maps << self if i.new_record?
		end

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

			{
				:type => self.node.class.to_s,
				:text => "#{self.node.text}",
				: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

7 entries across 7 versions & 1 rubygems

Version Path
active_record_survey-0.1.12 lib/active_record_survey/node_map.rb
active_record_survey-0.1.11 lib/active_record_survey/node_map.rb
active_record_survey-0.1.10 lib/active_record_survey/node_map.rb
active_record_survey-0.1.9 lib/active_record_survey/node_map.rb
active_record_survey-0.1.8 lib/active_record_survey/node_map.rb
active_record_survey-0.1.7 lib/active_record_survey/node_map.rb
active_record_survey-0.1.6 lib/active_record_survey/node_map.rb