Sha256: 67ada7d7844cdc799079681438e13208063e95eaeb92b7b1b51c3ddfe7bcf7af

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

class Survey < ActiveRecord::Base

  # Associations
  has_many :sections, :class_name => "SurveySection", :order => 'display_order'
  has_many :sections_with_questions, :include => :questions, :class_name => "SurveySection", :order => 'display_order'
  has_many :response_sets
  
  # Scopes
  named_scope :with_sections, {:include => :sections}
  
  # Validations
  validates_presence_of :title
  validates_uniqueness_of :access_code
  
  # Class methods
  def self.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
  
  # Instance methods
  def initialize(*args)
    super(*args)
    default_args
  end
  
  def default_args
    self.inactive_at ||= DateTime.now
  end
  
  def title=(value)
    self.access_code = Survey.to_normalized_string(value)
    super
  end
  
  def active?
    self.active_as_of?(DateTime.now)
  end
  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  
  def activate!
    self.active_at = DateTime.now
  end
  def deactivate!
    self.inactive_at = DateTime.now
  end
  def active_at=(datetime)
    self.inactive_at = nil if !datetime.nil? and !self.inactive_at.nil? and self.inactive_at < datetime
    super(datetime)
  end
  def inactive_at=(datetime)
    self.active_at = nil if !datetime.nil? and !self.active_at.nil? and self.active_at > datetime
    super(datetime)
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
surveyor-0.14.2 app/models/survey.rb
surveyor-0.14.1 app/models/survey.rb
surveyor-0.14.0 app/models/survey.rb
surveyor-0.13.0 app/models/survey.rb