Permalink
Browse files

Initial checkin: question models and basic admin

  • Loading branch information...
0 parents commit 901fcf223bd91d9c56515d097ba01ef5e2adb61c @markkendall markkendall committed Apr 5, 2010
Showing with 1,487 additions and 0 deletions.
  1. +22 −0 .gitignore
  2. +20 −0 LICENSE
  3. +5 −0 README.rdoc
  4. +70 −0 Rakefile
  5. +1 −0 VERSION
  6. +44 −0 app/controllers/census/data_groups_controller.rb
  7. +15 −0 app/helpers/census_helper.rb
  8. +43 −0 app/models/answer.rb
  9. +23 −0 app/models/boolean_question.rb
  10. +9 −0 app/models/choice.rb
  11. +10 −0 app/models/data_group.rb
  12. +19 −0 app/models/number_question.rb
  13. +72 −0 app/models/question.rb
  14. +7 −0 app/models/string_question.rb
  15. +40 −0 app/views/census/_question_fields.html.erb
  16. +10 −0 app/views/census/_user_answers.html.erb
  17. +10 −0 app/views/census/_user_questions.html.erb
  18. +4 −0 app/views/census/data_groups/_choice_fields.html.erb
  19. +11 −0 app/views/census/data_groups/_form.html.erb
  20. +22 −0 app/views/census/data_groups/_question_fields.html.erb
  21. +7 −0 app/views/census/data_groups/edit.html.erb
  22. +10 −0 app/views/census/data_groups/index.html.erb
  23. +7 −0 app/views/census/data_groups/new.html.erb
  24. +73 −0 census.gemspec
  25. +7 −0 config/routes.rb
  26. +1 −0 generators/census/USAGE
  27. +36 −0 generators/census/census_generator.rb
  28. +33 −0 generators/census/lib/insert_commands.rb
  29. +22 −0 generators/census/lib/rake_commands.rb
  30. +28 −0 generators/census/templates/README
  31. +10 −0 generators/census/templates/census.js
  32. +45 −0 generators/census/templates/factories.rb
  33. +60 −0 generators/census/templates/migrations/with_users.rb
  34. +54 −0 generators/census/templates/migrations/without_users.rb
  35. +3 −0 generators/census/templates/user.rb
  36. +1 −0 lib/census.rb
  37. +85 −0 lib/census/user.rb
  38. +7 −0 rails/init.rb
  39. +35 −0 shoulda_macros/census.rb
  40. +165 −0 test/controllers/data_groups_controller_test.rb
  41. +44 −0 test/models/answer_test.rb
  42. +45 −0 test/models/boolean_question_test.rb
  43. +24 −0 test/models/choice_test.rb
  44. +12 −0 test/models/data_group_test.rb
  45. +52 −0 test/models/number_question_test.rb
  46. +92 −0 test/models/question_test.rb
  47. +44 −0 test/models/string_question_test.rb
  48. +9 −0 test/models/user_test.rb
  49. +19 −0 test/test_helper.rb
@@ -0,0 +1,22 @@
+## MAC OS
+.DS_Store
+
+## TEXTMATE
+*.tmproj
+tmtags
+
+## EMACS
+*~
+\#*
+.\#*
+
+## VIM
+*.swp
+
+## PROJECT::GENERAL
+coverage
+rdoc
+pkg
+
+## PROJECT::SPECIFIC
+test/rails_root/*
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Envy Labs LLC
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,5 @@
+= Census
+
+Census is a Rails plugin that collects searchable demographics data for each
+of your application's users. The data to be collected is defined using a simple
+admin interface that Census provides.
@@ -0,0 +1,70 @@
+require 'rubygems'
+require 'rake'
+require 'rake/testtask'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "census"
+ gem.summary = %Q{Rails user demographics collection and searching}
+ gem.description = %Q{Census is a Rails plugin that collects searchable demographics data for each of your application's users.}
+ gem.email = "mark@envylabs.com"
+ gem.homepage = "http://github.com/envylabs/census"
+ gem.authors = ["Mark Kendall"]
+ gem.files = FileList["[A-Z]*", "{app,config,generators,lib,shoulda_macros,rails}/**/*"]
+
+ gem.add_dependency "acts_as_list", ">= 0.1.2"
+ gem.add_dependency "inverse_of", ">= 0.0.1"
+
+ gem.add_development_dependency "shoulda", ">= 0"
+ gem.add_development_dependency "factory_girl", ">= 0"
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
+
+namespace :test do
+ Rake::TestTask.new(:basic => ["check_dependencies",
+ "generator:cleanup",
+ "generator:census"]) do |task|
+ task.libs << "lib"
+ task.libs << "test"
+ task.pattern = "test/{controllers,models}/*_test.rb"
+ task.verbose = false
+ end
+end
+
+task :default => ['test:basic']
+
+generators = %w(census)
+
+namespace :generator do
+ desc "Cleans up the test app before running the generator"
+ task :cleanup do
+ FileList["test/rails_root/db/**/*"].each do |each|
+ FileUtils.rm_rf(each)
+ end
+
+ FileUtils.rm_rf("test/rails_root/vendor/plugins/census")
+ FileUtils.mkdir_p("test/rails_root/vendor/plugins")
+ census_root = File.expand_path(File.dirname(__FILE__))
+ system("ln -s \"#{census_root}\" test/rails_root/vendor/plugins/census")
+ end
+
+ desc "Run the census generator"
+ task :census do
+ system "cd test/rails_root && ./script/generate census -f && rake gems:unpack && rake db:migrate db:test:prepare"
+ end
+
+end
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "Census #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+0.1.0
@@ -0,0 +1,44 @@
+class Census::DataGroupsController < ApplicationController
+
+ def index
+ @data_groups = DataGroup.all(:order => :position)
+ end
+
+ def new
+ @data_group = DataGroup.new(params[:data_group])
+ end
+
+ def create
+ @data_group = DataGroup.new(params[:data_group])
+
+ if @data_group.save
+ flash[:notice] = "Created #{@data_group.name}"
+ redirect_to census_data_groups_path
+ else
+ render :action => 'new', :status => :unprocessable_entity
+ end
+ end
+
+ def edit
+ @data_group = DataGroup.find(params[:id])
+ end
+
+ def update
+ @data_group = DataGroup.find(params[:id])
+
+ if @data_group.update_attributes(params[:data_group])
+ flash[:notice] = "Saved #{@data_group.name}"
+ redirect_to census_data_groups_path
+ else
+ render :action => 'edit', :status => :unprocessable_entity
+ end
+ end
+
+ def destroy
+ @data_group = DataGroup.find(params[:id])
+ @data_group.destroy
+ flash[:notice] = "Deleted #{@data_group.name}"
+ redirect_to census_data_groups_path
+ end
+
+end
@@ -0,0 +1,15 @@
+module CensusHelper
+
+ def link_to_remove_fields(name, form)
+ form.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
+ end
+
+ def link_to_add_fields(name, form, association)
+ new_object = form.object.class.reflect_on_association(association).klass.new
+ fields = form.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
+ render(association.to_s.singularize + "_fields", :form => builder)
+ end
+ link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
+ end
+
+end
@@ -0,0 +1,43 @@
+class Answer < ActiveRecord::Base
+
+ belongs_to :question
+ belongs_to :user
+
+ validates_presence_of :question,
+ :user
+
+ validate :ensure_valid_choice
+ validate :ensure_valid_data_type
+ validate :check_multiple_answers
+
+ named_scope :for_user, lambda { |user| { :conditions => {:user_id => user.id} } }
+
+ def formatted_data
+ question.format_data(self.data)
+ end
+
+ def to_s
+ question.to_s(self.data)
+ end
+
+
+ private
+
+
+ def ensure_valid_choice
+ return if question.blank? || data.blank?
+ errors.add_to_base("Invalid choice for #{question.prompt}") if question.restrict_values? && !question.choices.map(&:value).include?(self.data)
+ end
+
+ def ensure_valid_data_type
+ return if question.blank? || data.blank?
+ message = question.validate_data(data)
+ errors.add_to_base("#{question.prompt} #{message}") if message
+ end
+
+ def check_multiple_answers
+ return if question.blank? || user.blank?
+ errors.add_to_base("Only one answer allowed for #{question.prompt}") if new_record? && question.answers.for_user(user).size > 0 && !question.multiple?
+ end
+
+end
@@ -0,0 +1,23 @@
+class BooleanQuestion < Question
+
+ def self.data_type_description
+ "Yes/No"
+ end
+
+ def sql_transform(column_name = '?')
+ "CAST(#{column_name} AS CHAR)"
+ end
+
+ def format_data(data)
+ %w(1 T t Y y).include?(data) unless data.blank?
+ end
+
+ def to_s(data)
+ case format_data(data)
+ when nil: ""
+ when true: "Yes"
+ else "No"
+ end
+ end
+
+end
@@ -0,0 +1,9 @@
+class Choice < ActiveRecord::Base
+
+ belongs_to :question, :inverse_of => :choices
+ acts_as_list :scope => :question
+
+ validates_presence_of :value,
+ :question
+
+end
@@ -0,0 +1,10 @@
+class DataGroup < ActiveRecord::Base
+
+ acts_as_list
+
+ has_many :questions, :dependent => :destroy, :inverse_of => :data_group
+ accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:prompt].blank? }, :allow_destroy => true
+
+ validates_presence_of :name
+
+end
@@ -0,0 +1,19 @@
+class NumberQuestion < Question
+
+ def self.data_type_description
+ "Number"
+ end
+
+ def sql_transform(column_name = '?')
+ "CAST(#{column_name} AS SIGNED INTEGER)"
+ end
+
+ def format_data(data)
+ data.to_i unless data.blank?
+ end
+
+ def validate_data(data)
+ "must be a number" unless data =~ /^\d*$/
+ end
+
+end
@@ -0,0 +1,72 @@
+class Question < ActiveRecord::Base
+
+ belongs_to :data_group, :inverse_of => :questions
+ acts_as_list :scope => :data_group
+
+ has_many :choices, :dependent => :destroy, :inverse_of => :question
+ accepts_nested_attributes_for :choices, :reject_if => lambda { |a| a[:value].blank? }, :allow_destroy => true
+
+ has_many :answers, :dependent => :destroy
+
+ validates_presence_of :prompt,
+ :data_group
+
+ def self.load_data_type(klass)
+ @@question_types ||= []
+ @@question_types << klass
+ end
+
+ def self.data_types
+ @@question_types ||= [StringQuestion, NumberQuestion, BooleanQuestion]
+ @@question_types.map {|klass| [klass.data_type_description, klass.name]}
+ end
+
+ def self.data_type_description
+ ""
+ end
+
+ def data_type
+ self.class.name
+ end
+
+ def data_type=(type)
+ self[:type] = type
+ end
+
+ def sql_transform(column_name = '?')
+ "#{column_name}"
+ end
+
+ def format_data(data)
+ data
+ end
+
+ def validate_data(data)
+ nil
+ end
+
+ def to_s(data)
+ format_data(data).to_s
+ end
+
+ def find_answers_matching(value)
+ answers.find(:all, :conditions => conditions_for(value), :include => :user)
+ end
+
+ def restrict_values?
+ choices.present? && !other?
+ end
+
+
+ private
+
+
+ def conditions_for(value)
+ if value.kind_of?(Range) || value.kind_of?(Array)
+ ["#{sql_transform('data')} IN (?)", value]
+ else
+ ["#{sql_transform('data')} = #{sql_transform}", value]
+ end
+ end
+
+end
@@ -0,0 +1,7 @@
+class StringQuestion < Question
+
+ def self.data_type_description
+ "String"
+ end
+
+end
Oops, something went wrong.

0 comments on commit 901fcf2

Please sign in to comment.