Skip to content

Commit

Permalink
add api ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Yoon committed May 9, 2011
1 parent 6dc21b3 commit 1f0c476
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 6 deletions.
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -94,7 +94,14 @@ and read surveys/EXTENDING\_SURVEYOR

# Requirements

Surveyor depends on Ruby (1.8.7 - 1.9.1), Rails 3 and HAML/SASS http://haml.hamptoncatlin.com/. It also depends on fastercsv for csv exports.
Surveyor depends on:

* Ruby (1.8.7 - 1.9.1)
* Rails 2.3
* HAML/SASS
* formtastic
* fastercsv for csv exports
* UUID

# Contributing, testing

Expand Down
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -12,6 +12,7 @@ begin
gem.add_dependency 'haml'
gem.add_dependency 'fastercsv'
gem.add_dependency 'formtastic'
gem.add_dependency 'uuid'
gem.add_development_dependency "yard", ">= 0"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/surveyor/install_generator.rb
Expand Up @@ -12,7 +12,7 @@ def migrations
unless options[:skip_migrations]
# because all migration timestamps end up the same, causing a collision when running rake db:migrate
# copied functionality from RAILS_GEM_PATH/lib/rails_generator/commands.rb
%w(create_surveys create_survey_sections create_questions create_question_groups create_answers create_response_sets create_responses create_dependencies create_dependency_conditions create_validations create_validation_conditions add_display_order_to_surveys add_correct_answer_id_to_questions add_index_to_response_sets add_index_to_surveys add_unique_indicies add_section_id_to_responses add_default_value_to_answers).each_with_index do |model, i|
%w(create_surveys create_survey_sections create_questions create_question_groups create_answers create_response_sets create_responses create_dependencies create_dependency_conditions create_validations create_validation_conditions add_display_order_to_surveys add_correct_answer_id_to_questions add_index_to_response_sets add_index_to_surveys add_unique_indicies add_section_id_to_responses add_default_value_to_answers add_api_ids).each_with_index do |model, i|
unless (prev_migrations = Dir.glob("db/migrate/[0-9]*_*.rb").grep(/[0-9]+_#{model}.rb$/)).empty?
prev_migration_timestamp = prev_migrations[0].match(/([0-9]+)_#{model}.rb$/)[1]
end
Expand Down
13 changes: 13 additions & 0 deletions lib/generators/surveyor/templates/db/migrate/add_api_ids.rb
@@ -0,0 +1,13 @@
class AddApiIds < ActiveRecord::Migration
def self.up
add_column :surveys, :api_id, :string
add_column :questions, :api_id, :string
add_column :answers, :api_id, :string
end

def self.down
remove_column :surveys, :api_id
remove_column :questions, :api_id
remove_column :answers, :api_id
end
end
1 change: 1 addition & 0 deletions lib/surveyor/models/answer_methods.rb
Expand Up @@ -33,6 +33,7 @@ def default_args
self.response_class ||= "answer"
self.short_text ||= text
self.data_export_identifier ||= Surveyor::Common.normalize(text)
self.api_id ||= UUID.generate
end

def css_class
Expand Down
1 change: 1 addition & 0 deletions lib/surveyor/models/question_methods.rb
Expand Up @@ -37,6 +37,7 @@ def default_args
self.display_order ||= self.survey_section ? self.survey_section.questions.count : 0
self.data_export_identifier ||= Surveyor::Common.normalize(text)
self.short_text ||= text
self.api_id ||= UUID.generate
end

def pick=(val)
Expand Down
1 change: 1 addition & 0 deletions lib/surveyor/models/survey_methods.rb
Expand Up @@ -36,6 +36,7 @@ def initialize(*args)

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

def title=(value)
Expand Down
6 changes: 3 additions & 3 deletions lib/surveyor/unparser.rb
Expand Up @@ -14,7 +14,7 @@ class Survey < ActiveRecord::Base
# block

def unparse(dsl)
attrs = (self.attributes.diff Survey.new(:title => title).attributes).delete_if{|k,v| %w(created_at updated_at inactive_at id title access_code).include? k}.symbolize_keys!
attrs = (self.attributes.diff Survey.new(:title => title).attributes).delete_if{|k,v| %w(created_at updated_at inactive_at id title access_code api_id).include? k}.symbolize_keys!
dsl << "survey \"#{title}\""
dsl << (attrs.blank? ? " do\n" : ", #{attrs.inspect.gsub(/\{|\}/, "")} do\n")
sections.each{|section| section.unparse(dsl)}
Expand Down Expand Up @@ -62,7 +62,7 @@ class Question < ActiveRecord::Base
# nonblock

def unparse(dsl)
attrs = (self.attributes.diff Question.new(:text => text).attributes).delete_if{|k,v| %w(created_at updated_at reference_identifier id survey_section_id question_group_id).include?(k) or (k == "display_type" && v == "label")}.symbolize_keys!
attrs = (self.attributes.diff Question.new(:text => text).attributes).delete_if{|k,v| %w(created_at updated_at reference_identifier id survey_section_id question_group_id api_id).include?(k) or (k == "display_type" && v == "label")}.symbolize_keys!
dsl << (solo? ? "\n" : " ")
if display_type == "label"
dsl << " label"
Expand Down Expand Up @@ -105,7 +105,7 @@ class Answer < ActiveRecord::Base
# nonblock

def unparse(dsl)
attrs = (self.attributes.diff Answer.new(:text => text).attributes).delete_if{|k,v| %w(created_at updated_at reference_identifier response_class id question_id).include? k}.symbolize_keys!
attrs = (self.attributes.diff Answer.new(:text => text).attributes).delete_if{|k,v| %w(created_at updated_at reference_identifier response_class id question_id api_id).include? k}.symbolize_keys!
attrs.delete(:is_exclusive) if text == "Omit" && is_exclusive == true
attrs.merge!({:is_exclusive => false}) if text == "Omit" && is_exclusive == false
dsl << " " if question.part_of_group?
Expand Down
4 changes: 4 additions & 0 deletions spec/models/answer_spec.rb
Expand Up @@ -37,4 +37,8 @@
@answer.destroy
Validation.find_by_id(v_id).should be_nil
end

it "should have an api_id" do
@answer.api_id.length.should == 36
end
end
4 changes: 4 additions & 0 deletions spec/models/question_spec.rb
Expand Up @@ -38,6 +38,10 @@
@question.split_text(:pre).should == "before"
@question.split_text(:post).should == "after|extra"
end

it "should have an api_id" do
@question.api_id.length.should == 36
end
end

describe Question, "that has answers" do
Expand Down
4 changes: 4 additions & 0 deletions spec/models/survey_spec.rb
Expand Up @@ -21,6 +21,10 @@
bandwagoneer.save.should be_true
bandwagoneer.title.should == "Foo 2"
end

it "should have an api_id" do
@survey.api_id.length.should == 36
end
end

# Associations
Expand Down
3 changes: 2 additions & 1 deletion testbed/Gemfile
Expand Up @@ -39,4 +39,5 @@ end
gem 'formtastic'
gem 'surveyor', :path => ".."
gem 'haml'
gem 'fastercsv'
gem 'fastercsv'
gem 'uuid'

0 comments on commit 1f0c476

Please sign in to comment.