Module: Surveyor::Models::SurveyMethods

Included in:
Survey
Defined in:
lib/surveyor/models/survey_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/surveyor/models/survey_methods.rb', line 7

def self.included(base)
  # Associations
  base.send :has_many, :sections, :class_name => "SurveySection", :order => "#{SurveySection.quoted_table_name}.display_order", :dependent => :destroy
  base.send :has_many, :sections_with_questions, :include => :questions, :class_name => "SurveySection", :order => "#{SurveySection.quoted_table_name}.display_order"
  base.send :has_many, :response_sets
  base.send :has_many, :translations, :class_name => "SurveyTranslation"

  # Scopes
  base.send :scope, :with_sections, {:include => :sections}

  @@validations_already_included ||= nil
  unless @@validations_already_included
    # Validations
    base.send :validates_presence_of, :title
    base.send :validates_uniqueness_of, :survey_version, :scope => :access_code, :message => "survey with matching access code and version already exists"

    @@validations_already_included = true
  end

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

  # Derived attributes
  base.send :before_save, :generate_access_code
  base.send :before_save, :increment_version

  # Class methods
  base.instance_eval do
    def to_normalized_string(value)
      # replace non-alphanumeric with "-". remove repeat "-"s. don't start or end with "-"
      value.to_s.downcase.gsub(/[^a-z0-9]/,"-").gsub(/-+/,"-").gsub(/-$|^-/,"")
    end
  end
end

Instance Method Details

#activate!Object



59
60
61
62
# File 'lib/surveyor/models/survey_methods.rb', line 59

def activate!
  self.active_at = DateTime.now
  self.inactive_at = nil
end

#active?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/surveyor/models/survey_methods.rb', line 53

def active?
  self.active_as_of?(DateTime.now)
end

#active_as_of?(date) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/surveyor/models/survey_methods.rb', line 56

def active_as_of?(date)
  (active_at && active_at < date && (!inactive_at || inactive_at > date)) ? true : false
end

#as_json(options = nil) ⇒ Object



68
69
70
71
# File 'lib/surveyor/models/survey_methods.rb', line 68

def as_json(options = nil)
  template_paths = ActionController::Base.view_paths.collect(&:to_path)
  Rabl.render(filtered_for_json, 'surveyor/export.json', :view_path => template_paths, :format => "hash")
end

#deactivate!Object



63
64
65
66
# File 'lib/surveyor/models/survey_methods.rb', line 63

def deactivate!
  self.inactive_at = DateTime.now
  self.active_at = nil
end

#default_access_codeObject



80
81
82
# File 'lib/surveyor/models/survey_methods.rb', line 80

def default_access_code
  self.class.to_normalized_string(title)
end

#default_argsObject



48
49
50
51
# File 'lib/surveyor/models/survey_methods.rb', line 48

def default_args
  self.api_id ||= Surveyor::Common.generate_api_id
  self.display_order ||= Survey.count
end

#filtered_for_jsonObject

A hook that allows the survey object to be modified before it is serialized by the #as_json method.



76
77
78
# File 'lib/surveyor/models/survey_methods.rb', line 76

def filtered_for_json
  self
end

#generate_access_codeObject



84
85
86
# File 'lib/surveyor/models/survey_methods.rb', line 84

def generate_access_code
  self.access_code ||= default_access_code
end

#increment_versionObject



88
89
90
91
92
93
# File 'lib/surveyor/models/survey_methods.rb', line 88

def increment_version
  surveys = self.class.select(:survey_version).where(:access_code => access_code).order("survey_version DESC")
  next_version = surveys.any? ? surveys.first.survey_version.to_i + 1 : 0

  self.survey_version = next_version
end

#initialize(*args) ⇒ Object

Instance methods



43
44
45
46
# File 'lib/surveyor/models/survey_methods.rb', line 43

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

#translation(locale_symbol) ⇒ Object



95
96
97
98
99
100
# File 'lib/surveyor/models/survey_methods.rb', line 95

def translation(locale_symbol)
  t = self.translations.where(:locale => locale_symbol.to_s).first
  {:title => self.title, :description => self.description}.with_indifferent_access.merge(
    t ? YAML.load(t.translation || "{}").with_indifferent_access : {}
  )
end