Sha256: 35ff7da4660418a40c72e165a1c01de86b5eb4fe53ffc1c6681083463769a73c

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

module Surveyor
  module Models
    module AnswerMethods
      def self.included(base)
        # Associations
        base.send :belongs_to, :question
        base.send :has_many, :responses
        base.send :has_many, :validations, :dependent => :destroy
        
        # Scopes
        base.send :default_scope, :order => "display_order ASC"
        
        @@validations_already_included ||= nil
        unless @@validations_already_included
          # Validations
          base.send :validates_presence_of, :text
          # this causes issues with building and saving
          # base.send :validates_numericality_of, :question_id, :allow_nil => false, :only_integer => true
          @@validations_already_included = true
        end
      end

      # Instance Methods
      def initialize(*args)
        super(*args)
        default_args
      end

      def default_args
        self.display_order ||= self.question ? self.question.answers.count : 0
        self.is_exclusive ||= false
        self.hide_label ||= false
        self.response_class ||= "answer"
        self.short_text ||= text
        self.data_export_identifier ||= Surveyor::Common.normalize(text)
      end
      
      def css_class
        [(is_exclusive ? "exclusive" : nil), custom_class].compact.join(" ")
      end
      
      def split_or_hidden_text(part = nil)
        return "" if hide_label.to_s == "true"
        part == :pre ? text.split("|",2)[0] : (part == :post ? text.split("|",2)[1] : text)
      end
      
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
surveyor-0.19.3 lib/surveyor/models/answer_methods.rb
surveyor-0.19.2 lib/surveyor/models/answer_methods.rb
surveyor-0.19.1 lib/surveyor/models/answer_methods.rb
surveyor-0.19.0 lib/surveyor/models/answer_methods.rb