Module: Surveyor::Models::SurveySectionMethods

Included in:
SurveySection
Defined in:
lib/surveyor/models/survey_section_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/surveyor/models/survey_section_methods.rb', line 4

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 Method Details

#default_argsObject



32
33
34
# File 'lib/surveyor/models/survey_section_methods.rb', line 32

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

#initialize(*args) ⇒ Object

Instance Methods



27
28
29
30
# File 'lib/surveyor/models/survey_section_methods.rb', line 27

def initialize(*args)
  super(*args)
  default_args
end

#questions_and_groupsObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/surveyor/models/survey_section_methods.rb', line 36

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

#translation(locale) ⇒ Object



49
50
51
52
53
# File 'lib/surveyor/models/survey_section_methods.rb', line 49

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