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

+ (Object) included(base)



6
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
# File 'lib/surveyor/models/survey_methods.rb', line 6

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

  # Scopes
  base.send :named_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, :access_code
    
    @@validations_already_included = true
  end        

  # 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

- (Object) activate!



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

def activate!
  self.active_at = DateTime.now
end

- (Boolean) active?



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

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

- (Boolean) active_as_of?(datetime)



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

def active_as_of?(datetime)
  (self.active_at.nil? or self.active_at < datetime) and (self.inactive_at.nil? or self.inactive_at > datetime)
end

- (Object) active_at=(datetime)



70
71
72
73
# File 'lib/surveyor/models/survey_methods.rb', line 70

def active_at=(datetime)
  self.inactive_at = nil if !datetime.nil? and !self.inactive_at.nil? and self.inactive_at < datetime
  super(datetime)
end

- (Object) deactivate!



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

def deactivate!
  self.inactive_at = DateTime.now
end

- (Object) default_args



39
40
41
42
# File 'lib/surveyor/models/survey_methods.rb', line 39

def default_args
  self.inactive_at ||= DateTime.now
  self.api_id ||= UUID.generate
end

- (Object) inactive_at=(datetime)



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

def inactive_at=(datetime)
  self.active_at = nil if !datetime.nil? and !self.active_at.nil? and self.active_at > datetime
  super(datetime)
end

- (SurveyMethods) initialize(*args)

Instance methods



34
35
36
37
# File 'lib/surveyor/models/survey_methods.rb', line 34

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

- (Object) title=(value)



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/surveyor/models/survey_methods.rb', line 44

def title=(value)
  return if value == self.title
  adjusted_value = value
  while Survey.find_by_access_code(Survey.to_normalized_string(adjusted_value))
    i ||= 0
    i += 1
    adjusted_value = "#{value} #{i.to_s}"
  end
  self.access_code = Survey.to_normalized_string(adjusted_value)
  super(adjusted_value)
  # self.access_code = Survey.to_normalized_string(value)
  # super
end