survey/app/models/survey_answer.rb

44 lines
1.4 KiB
Ruby
Raw Normal View History

2014-10-06 05:55:25 +00:00
class SurveyAnswer
include Mongoid::Document
include Mongoid::Timestamps
2021-07-08 13:31:27 +00:00
include Mongoid::Attributes::Dynamic
field :user, type: BSON::ObjectId
field :scored_points, type: Integer, :default => 0
2016-10-29 11:33:54 +00:00
field :individual_total, type: Array, :default => []
2021-07-08 13:31:27 +00:00
field :select_question, type: Array, :default => []
2014-10-06 05:55:25 +00:00
belongs_to :questionnaire_survey
after_create do
if self.questionnaire_survey
2021-09-12 12:56:09 +00:00
info = {:questionnaire_survey_id=>self.questionnaire_survey.id,:user=>self.user}
if self.user.blank?
info[:survey_answer_ids] = [self.id]
end
answer_group = SurveyAnswerGroup.where(info).first
if answer_group.nil?
2021-09-12 12:56:09 +00:00
answer_group = SurveyAnswerGroup.new(info)
end
answer_group.last_modified = self.updated_at
answer_group.survey_answer_ids << self.id
answer_group.save
end
end
after_save do
answer_group = SurveyAnswerGroup.where(:questionnaire_survey_id=>self.questionnaire_survey.id,:user=>user).first
if answer_group
answer_group.last_modified = self.updated_at
answer_group.save
end
end
before_destroy do
answer_group = SurveyAnswerGroup.where(:user=>user).first
if answer_group
answer_group.survey_answer_ids.delete(self.id)
if answer_group.survey_answer_ids.count == 0
answer_group.destroy
else
answer_group.save
end
end
end
2014-10-06 05:55:25 +00:00
end