Sha256: 8289886f026936ae06091ac412fac271be0e883e3e513f2d8ef62bfc6c42fe40

Contents?: true

Size: 1.25 KB

Versions: 11

Compression:

Stored size: 1.25 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 for this survey
		def build_question(question)
			# build_question only accepts a node that inherits from Question
			if !question.class.ancestors.include?(::ActiveRecordSurvey::Node::Question)
				raise ArgumentError.new "Question must inherit from ::ActiveRecordSurvey::Node::Question"
			end

			# Already added - shouldn't add twice
			if question.node_maps.select { |node_map|
				node_map.survey === self
			}.length > 0
				raise RuntimeError.new "This question has already been added to the survey"
			end

			question.node_maps.build(:node => question, :survey => self)
		end
	end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
active_record_survey-0.1.25 lib/active_record_survey/survey.rb
active_record_survey-0.1.23 lib/active_record_survey/survey.rb
active_record_survey-0.1.22 lib/active_record_survey/survey.rb
active_record_survey-0.1.21 lib/active_record_survey/survey.rb
active_record_survey-0.1.20 lib/active_record_survey/survey.rb
active_record_survey-0.1.19 lib/active_record_survey/survey.rb
active_record_survey-0.1.18 lib/active_record_survey/survey.rb
active_record_survey-0.1.17 lib/active_record_survey/survey.rb
active_record_survey-0.1.16 lib/active_record_survey/survey.rb
active_record_survey-0.1.15 lib/active_record_survey/survey.rb
active_record_survey-0.1.14 lib/active_record_survey/survey.rb