Sha256: 3e6c4a4c688a7516a8b4b8811e88c3ce7a806dba4da2260eac44c5a7d6f23332

Contents?: true

Size: 724 Bytes

Versions: 6

Compression:

Stored size: 724 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
	end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
active_record_survey-0.1.5 lib/active_record_survey/node_map.rb
active_record_survey-0.1.4 lib/active_record_survey/node_map.rb
active_record_survey-0.1.3 lib/active_record_survey/node_map.rb
active_record_survey-0.1.2 lib/active_record_survey/node_map.rb
active_record_survey-0.1.1 lib/active_record_survey/node_map.rb
active_record_survey-0.1.0 lib/active_record_survey/node_map.rb