Sha256: 8fc4ba6df22b8de65799f06e823ef39c764fffbd5ccc07636aa3093ff8332882

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

module Surveyor
  module Models
    module SurveySectionMethods
      def self.included(base)
        # Associations
        base.send :has_many, :questions, :dependent => :destroy
        base.send :belongs_to, :survey

        # Scopes
        base.send :scope, :with_includes, { :include => {:questions => [:answers, :question_group, {:dependency => :dependency_conditions}]}}

        @@validations_already_included ||= nil
        unless @@validations_already_included
          # Validations
          base.send :validates_presence_of, :title, :display_order
          # this causes issues with building and saving
          #, :survey

          @@validations_already_included = true
        end

        # Whitelisting attributes
        base.send :attr_accessible, :survey, :survey_id, :title, :description, :reference_identifier, :data_export_identifier, :common_namespace, :common_identifier, :display_order, :custom_class
      end

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

      def default_args
        self.data_export_identifier ||= Surveyor::Common.normalize(title)
      end

      def questions_and_groups
        qs = []
        questions.each_with_index.map do |q,i|
          if q.part_of_group?
            if (i+1 >= questions.size) or (q.question_group_id != questions[i+1].question_group_id)
              q.question_group
            end
          else
            q
          end
        end.compact
      end

      def translation(locale)
        {:title => self.title, :description => self.description}.with_indifferent_access.merge(
          (self.survey.translation(locale)[:survey_sections] || {})[self.reference_identifier] || {}
        )
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
hssc_surveyor-1.4.3.pre lib/surveyor/models/survey_section_methods.rb
hssc_surveyor-1.4.2.pre lib/surveyor/models/survey_section_methods.rb
hssc_surveyor-1.4.1.pre lib/surveyor/models/survey_section_methods.rb
surveyor-1.4.0 lib/surveyor/models/survey_section_methods.rb