Sha256: 9c8cd617fb66c08f9a1a66b7512807a92e57bfc620086abb4064fa79cb7f45ac

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

module Helena
  class Question
    include Helena::Concerns::ApplicationModel
    include Mongoid::Orderable

    TYPES = [
      Helena::Questions::ShortText,
      Helena::Questions::LongText,
      Helena::Questions::StaticText,
      Helena::Questions::RadioGroup,
      Helena::Questions::CheckboxGroup,
      Helena::Questions::RadioMatrix,
      Helena::Questions::BipolarRadioMatrix
    ]

    belongs_to :question_group, inverse_of: :questions

    embeds_many :labels, class_name: 'Helena::Label', cascade_callbacks: true
    embeds_many :sub_questions, class_name: 'Helena::SubQuestion', cascade_callbacks: true

    accepts_nested_attributes_for :labels, allow_destroy: true, reject_if: :reject_labels
    accepts_nested_attributes_for :sub_questions, allow_destroy: true, reject_if: :reject_sub_questions

    field :code,          type: String
    field :question_text, type: String

    orderable scope: :question_group

    validates :code, presence: true
    validate :uniqueness_of_code

    def includes_labels?
      false
    end

    def includes_subquestions?
      false
    end

    private

    def uniqueness_of_code
      return unless question_group
      question_code_occurences = question_group.version.question_code_occurences
      errors.add :code, :taken if question_code_occurences[code] > 1
    end

    def reject_labels(attributed)
      attributed['text'].blank? &&
        attributed['value'].blank?
    end

    def reject_sub_questions(attributed)
      attributed['code'].blank? &&
        attributed['default_value'].blank? &&
        attributed['question_text'].blank?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
helena-1.1.0 app/models/helena/question.rb
helena-1.0.3 app/models/helena/question.rb
helena-1.0.2 app/models/helena/question.rb