Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Survey access_code not guaranteed to be unique #45

Closed
jakewendt opened this issue Mar 24, 2010 · 4 comments
Closed

Survey access_code not guaranteed to be unique #45

jakewendt opened this issue Mar 24, 2010 · 4 comments

Comments

@jakewendt
Copy link

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[:survey_code])

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.

Of course, now you have to different surveys that probably have the same name. But at least you can take them both!

@yoon
Copy link
Member

yoon commented Apr 5, 2010

Since one may generate surveys (and corresponding fixtures) at a very different time than importing them, looking up surveys in the database won't help the parser generate a unique access_code. Additionally, there aren't many use cases for having surveys with the same title, as users would be unable to differentiate them. If a use case is found, we'll rely on the developer to assign unique access codes with the survey method hash in the dsl.

We'll add unique index on access_code in the surveys table, and a validator to the Rails model should someone create Surveys through an admin index.

@yoon
Copy link
Member

yoon commented Apr 5, 2010

contrain surveys to have unique access_codes. closed by 572faf219240aeebcd15082bc97031f18da0117d, #42

@jakewendt
Copy link
Author

Thank you for making the mods. I can clean up some of my code now. However, the indexes added for access_codes in surveys and response_sets is missing the ":unique => true". Without it, the db won't enforce uniqueness, although the model will.

@yoon
Copy link
Member

yoon commented Apr 6, 2010

add unique indicies. closed by 660dc3f

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants