Sha256: 15e7cf79c7c607afd0aa763f6d395bd30703ed6e14ff414b17232f179f9883a4

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 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
        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
      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 renderer(q = question)  
        r = [q.pick.to_s, self.response_class].compact.map(&:downcase).join("_")
        r.blank? ? :default : r.to_sym
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
surveyor-0.18.0 lib/surveyor/models/answer_methods.rb
surveyor-0.17.0 lib/surveyor/models/answer_methods.rb
surveyor-0.16.1 lib/surveyor/models/answer_methods.rb
surveyor-0.16.0 lib/surveyor/models/answer_methods.rb