Class: Survey
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Survey
- Defined in:
- app/models/survey.rb
Class Method Summary collapse
-
.to_normalized_string(value) ⇒ Object
Class methods.
Instance Method Summary collapse
-
#access_code=(value) ⇒ Object
Survey access_code not guaranteed to be unique.
- #activate! ⇒ Object
- #active? ⇒ Boolean
- #active_as_of?(datetime) ⇒ Boolean
- #active_at=(datetime) ⇒ Object
- #deactivate! ⇒ Object
- #default_args ⇒ Object
- #inactive_at=(datetime) ⇒ Object
-
#initialize(*args) ⇒ Survey
constructor
Instance methods.
- #title=(value) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Survey
Instance methods
26 27 28 29 |
# File 'app/models/survey.rb', line 26 def initialize(*args) super(*args) default_args end |
Class Method Details
.to_normalized_string(value) ⇒ Object
Class methods
20 21 22 23 |
# File 'app/models/survey.rb', line 20 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 Method Details
#access_code=(value) ⇒ Object
Survey access_code not guaranteed to be unique
As there is no model validation or unique index in the database, there is no guarantee that your survey access_code will be unique. It is just a normalized version of your survey title.
Running …
rake surveyor FILE=surveys/kitchen_sink_survey.rb APPEND=true rake surveyor FILE=surveys/kitchen_sink_survey.rb APPEND=true script/console Survey.all.collect(&:access_code)
should show this to be true.
This will show both surveys as able to be taken, but will only allow the first one when the attempt is made as the survey is found by
@survey = Survey.find_by_access_code(params)
In addition, adding a unique index to the db and model will make a mess when running
rake surveyor FILE=surveys/kitchen_sink_survey.rb APPEND=true rake surveyor FILE=surveys/kitchen_sink_survey.rb APPEND=true
as the access_code is not unique and the survey fixtures are loaded last, leaving the associations with an invalid survey_id.
To avoid this, the survey fixture should probably be attempted first.
Update…
Adding something like …
def access_code=(value) counter = 2 original_value = value while( ( survey = Survey.find_by_access_code(value) ) && ( self.id != survey.id ) ) value = [original_value,“_”,counter].join counter += 1 end super end
… to Survey seems to provide a “fix” for this “problem”, although I don’t know how kosher it is.
It works in my test environment, but does not actually work in the “rake surveyor” task as the survey is built from a different Survey model completely and then loaded into the database and not created though the application.
My final “fix” was to add
require ‘lib/surveyor/survey_extensions’
to my Rakefile and create the file
> cat lib/surveyor/survey_extensions.rb if surveyor_gem = Gem.searcher.find(‘surveyor’) require surveyor_gem.full_gem_path + ‘/script/surveyor/parser’ require surveyor_gem.full_gem_path + ‘/script/surveyor/survey’ end
module SurveyParser module SurveyExtensions def self.included(base) base.class_eval do def initialize_with_unique_access_code(obj, args, opts) initialize_without_unique_access_code(obj, args, opts) counter = 2 ac = self.access_code original_ac = self.access_code while( survey = ::Survey.find_by_access_code(ac) ) ac = [original_ac,“_”,counter].join counter += 1 end self.access_code = ac end alias_method_chain :initialize, :unique_access_code end end end end SurveyParser::Survey.send(:include, SurveyParser::SurveyExtensions)
It is not as clean as I would’ve liked, but works. Override the setting of the access_code to ensure its uniqueness
While uniqueness was added after 0.10.0, this method was not deemed important or likely to be needed enough. It is true that this really isn’t NEEDed.
161 162 163 164 165 166 167 168 169 170 |
# File 'app/models/survey.rb', line 161 def access_code=(value) counter = 2 original_value = value while( ( survey = Survey.find_by_access_code(value) ) && ( self.id != survey.id ) ) value = [original_value,"_",counter].join counter += 1 end super #(value) end |
#activate! ⇒ Object
46 47 48 |
# File 'app/models/survey.rb', line 46 def activate! self.active_at = DateTime.now end |
#active? ⇒ Boolean
40 41 42 |
# File 'app/models/survey.rb', line 40 def active? self.active_as_of?(DateTime.now) end |
#active_as_of?(datetime) ⇒ Boolean
43 44 45 |
# File 'app/models/survey.rb', line 43 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 |
#active_at=(datetime) ⇒ Object
52 53 54 55 |
# File 'app/models/survey.rb', line 52 def active_at=(datetime) self.inactive_at = nil if !datetime.nil? and !self.inactive_at.nil? and self.inactive_at < datetime super(datetime) end |
#deactivate! ⇒ Object
49 50 51 |
# File 'app/models/survey.rb', line 49 def deactivate! self.inactive_at = DateTime.now end |
#default_args ⇒ Object
31 32 33 |
# File 'app/models/survey.rb', line 31 def default_args self.inactive_at ||= DateTime.now end |
#inactive_at=(datetime) ⇒ Object
56 57 58 59 |
# File 'app/models/survey.rb', line 56 def inactive_at=(datetime) self.active_at = nil if !datetime.nil? and !self.active_at.nil? and self.active_at > datetime super(datetime) end |
#title=(value) ⇒ Object
35 36 37 38 |
# File 'app/models/survey.rb', line 35 def title=(value) self.access_code = Survey.to_normalized_string(value) super end |