Sha256: 87786d2a153477f02b69920dea97dc1084a05c98d6942d720bdf0b2217876df6

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module ActiveRecordSurvey
	class Survey < ::ActiveRecord::Base
		self.table_name = "active_record_surveys"
		has_many :node_maps, :class_name => "ActiveRecordSurvey::NodeMap", :foreign_key => :active_record_survey_id
		has_many :nodes, -> { distinct }, :through => :node_maps

		def questions
			self.node_maps.includes(:node).select { |i|
				i.node.class.ancestors.include?(::ActiveRecordSurvey::Node::Question)
			}.collect { |i|
				i.node
			}.uniq
		end

		def root_node
			self.node_maps.includes(:node).select { |i| i.depth === 0 }.first
		end

		def as_map
			list = self.node_maps

			list.select { |i| !i.parent }.collect { |i|
				i.as_map(list)
			}
		end

		# Build a question with answers for this survey
		def build_question(question, answers = [], parent = nil)
			node_maps = []
			n_question = question.node_maps.build(:node => question, :survey => self)
			node_maps << n_question

			answers.each { |answer|
				n_answer = answer.node_maps.build(:node => answer, :survey => self)
				n_question.children << n_answer
				node_maps << n_answer
			}

			# If a parent node is passed, add it
			parent.children << n_question if !parent.nil?

			node_maps
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_survey-0.1.13 lib/active_record_survey/survey.rb