class SurveyQuestion Oneline = 0 Multiline = 1 Radio = 2 Check = 3 Select = 4 Radiogroup = 5 DateTime = 6 DoubleLevelOption = 7 include Mongoid::Document include Mongoid::Attributes::Dynamic field :need_update_score, :type => Boolean, :default => false field :weight, :type => Integer, :default => 1 field :title, :localize => true field :description, :localize => true field :is_required, :type => Boolean field :type, :type => Integer field :selectable_question, :type => Boolean, :default => false field :selectable_question_type, :type => Integer, :default => 0 field :datetime_type # allow custom answer option field :custom_option, :type => Boolean field :custom_option_type, :type => Array, :default => [1] field :custom_option_title, :type => String, :localize => true field :option_layout_type, :type => Integer, :default => 0 field :sequence, :type => Integer, :default => 0 field :question_type, :type => Integer, :default => 0 belongs_to :questionnaire_survey embeds_many :survey_question_options embeds_many :survey_question_radiogroups accepts_nested_attributes_for :survey_question_options, :allow_destroy => true accepts_nested_attributes_for :survey_question_radiogroups, :allow_destroy => true # default_scope asc(:sequence) default_scope ->{ asc(:sequence) } before_save do self.custom_option_type = self.custom_option_type.map(&:to_i) if self.weight_changed? || (self.survey_question_options.select{|opt| opt.points_changed? || (opt.level2.select{|sub_opt| sub_opt.points_changed?}.count != 0)}.count != 0) self.need_update_score = true end end def jumpable? case type when SurveyQuestion::Radio, SurveyQuestion::Select true else false end end def can_set_jump? ( !custom_option ) end def get_jump_tos Hash[survey_question_options.select{ |o| !o.jump_to.blank? }.collect{ |o| [o.id.to_s, o.jump_to] }] end def custom_option_new_option self.custom_option && self.custom_option_type.include?(1) end def custom_option_each_option self.custom_option && self.custom_option_type.include?(0) end def custom_option_title tmp = super() tmp.blank? ? I18n.t('survey_question.option_title_default') : tmp end def layout_float self.option_layout_type==1 end def as_json(**xargs) res = super({:except =>:survey_question_options}.merge(xargs)) res.merge({title_translations: self.title_translations, description_translations: self[:description], qid: self[:_id].to_s, survey_question_options: self.survey_question_options.as_json, custom_option_title_translations: self.custom_option_title_translations}) end def is_selectable_radio self.selectable_question && self.selectable_question_type==1 end def is_selectable_check self.selectable_question && self.selectable_question_type==0 end def level1_radio self.question_type==0 end def level1_check self.question_type==1 end #def attributes() # tmp = super() # tmp.merge({title_translations: tmp[:title], # description_translations: tmp[:description], # qid: tmp[:_id].to_s}) #end end