app/models/response_set.rb in surveyor-0.10.0 vs app/models/response_set.rb in surveyor-0.11.0
- old
+ new
@@ -9,10 +9,11 @@
has_many :responses, :dependent => :destroy
# Validations
validates_presence_of :survey_id
validates_associated :responses
+ validates_uniqueness_of :access_code
# Attributes
attr_protected :completed_at
attr_accessor :current_section_id
@@ -26,9 +27,30 @@
end
def default_args
self.started_at ||= Time.now
self.access_code = Surveyor.make_tiny_code
+ end
+
+ def access_code=(val)
+ while ResponseSet.find_by_access_code(val)
+ val = Surveyor.make_tiny_code
+ end
+ super
+ end
+
+ def to_csv
+ qcols = Question.content_columns.map(&:name) - %w(created_at updated_at)
+ acols = Answer.content_columns.map(&:name) - %w(created_at updated_at)
+ rcols = Response.content_columns.map(&:name)
+ require 'fastercsv'
+ FCSV(result = "") do |csv|
+ csv << qcols.map{|qcol| "question.#{qcol}"} + acols.map{|acol| "answer.#{acol}"} + rcols.map{|rcol| "response.#{rcol}"}
+ responses.each do |response|
+ csv << qcols.map{|qcol| response.question.send(qcol)} + acols.map{|acol| response.answer.send(acol)} + rcols.map{|rcol| response.send(rcol)}
+ end
+ end
+ result
end
def response_for(question_id, answer_id, group = nil)
found = responses.detect{|r| r.question_id == question_id && r.answer_id == answer_id && r.response_group.to_s == group.to_s}
found.blank? ? responses.new(:question_id => question_id, :answer_id => answer_id, :response_group => group) : found
\ No newline at end of file