Module: Surveyor::Models::SurveyMethods

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::ForbiddenAttributesProtection, ActiveModel::Validations
Included in:
Survey
Defined in:
lib/surveyor/models/survey_methods.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#activate!Object



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

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

#active?Boolean

Returns:

  • (Boolean)


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

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

#active_as_of?(date) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#as_json(options = nil) ⇒ Object



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

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



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

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

#default_access_codeObject



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

def default_access_code
  self.class.to_normalized_string(title)
end

#default_argsObject



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

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.



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

def filtered_for_json
  self
end

#generate_access_codeObject



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

def generate_access_code
  self.access_code ||= default_access_code
end

#increment_versionObject



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

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



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

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

#translation(locale_symbol) ⇒ Object



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

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