Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial checkin: question models and basic admin

  • Loading branch information...
commit 901fcf223bd91d9c56515d097ba01ef5e2adb61c 0 parents
@markkendall markkendall authored
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
22 .gitignore
@@ -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.
5 README.rdoc
@@ -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.
70 Rakefile
@@ -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
1  VERSION
@@ -0,0 +1 @@
+0.1.0
44 app/controllers/census/data_groups_controller.rb
@@ -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
15 app/helpers/census_helper.rb
@@ -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
43 app/models/answer.rb
@@ -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
23 app/models/boolean_question.rb
@@ -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
9 app/models/choice.rb
@@ -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
10 app/models/data_group.rb
@@ -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
19 app/models/number_question.rb
@@ -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
72 app/models/question.rb
@@ -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
7 app/models/string_question.rb
@@ -0,0 +1,7 @@
+class StringQuestion < Question
+
+ def self.data_type_description
+ "String"
+ end
+
+end
40 app/views/census/_question_fields.html.erb
@@ -0,0 +1,40 @@
+<% if question.choices.empty? -%>
+ <% answer = user.answer_for(question) -%>
+ <% fields_for "user[answers_attributes][#{user.answers.index(answer)}]", answer do |builder| -%>
+ <%= builder.hidden_field :id %>
+ <%= builder.hidden_field :question_id %>
+ <div class="text_field">
+ <%= builder.label :data, question.prompt %>
+ <%= builder.text_field :data %>
+ </div>
+ <% end -%>
+
+<% elsif question.multiple? -%>
+ <div class="checkboxes">
+ <%= label_tag question.prompt %>
+ <ul>
+ <% question.choices.each do |choice| -%>
+ <% answer = user.answer_for_choice(choice) -%>
+ <% fields_for "user[answers_attributes][#{user.answers.index(answer)}]", answer do |builder| -%>
+ <%= builder.hidden_field :id %>
+ <%= builder.hidden_field :question_id %>
+ <li>
+ <%= builder.check_box :data, {}, choice.value, '' %>
+ <%= builder.label :data, choice.value %>
+ </li>
+ <% end -%>
+ <% end -%>
+ </ul>
+ </div>
+
+<% else -%>
+ <% answer = user.answer_for(question) -%>
+ <% fields_for "user[answers_attributes][#{user.answers.index(answer)}]", answer do |builder| -%>
+ <%= builder.hidden_field :id %>
+ <%= builder.hidden_field :question_id %>
+ <div class="select_field">
+ <%= builder.label :data, question.prompt %>
+ <%= builder.collection_select :data, question.choices, :value, :value, :include_blank => true %>
+ </div>
+ <% end -%>
+<% end -%>
10 app/views/census/_user_answers.html.erb
@@ -0,0 +1,10 @@
+<div class="census_answers">
+ <% DataGroup.all.each do |group| -%>
+ <h3><%=h group.name %></h3>
+ <ul>
+ <% group.questions.each do |question| -%>
+ <li><b><%=h question.prompt %></b> <%=h user.all_answers_for(question).map(&:data).join(', ') %></li>
+ <% end -%>
+ </ul>
+ <% end -%>
+</div>
10 app/views/census/_user_questions.html.erb
@@ -0,0 +1,10 @@
+<div class="census_questions">
+ <% DataGroup.all.each do |group| -%>
+ <fieldset>
+ <legend><%=h group.name %></legend>
+ <% group.questions.each do |question| -%>
+ <%= render 'census/question_fields', :question => question, :user => user %>
+ <% end -%>
+ </fieldset>
+ <% end -%>
+</div>
4 app/views/census/data_groups/_choice_fields.html.erb
@@ -0,0 +1,4 @@
+<div class="fields">
+ <%= form.text_field :value %>
+ <%= link_to_remove_fields "remove", form %>
+</div>
11 app/views/census/data_groups/_form.html.erb
@@ -0,0 +1,11 @@
+<%= form.error_messages %>
+<div class="text_field">
+ <%= form.label :name, "Group Name" %>
+ <%= form.text_field :name %>
+</div>
+<% form.fields_for :questions do |builder| %>
+ <%= render "question_fields", :form => builder %>
+<% end %>
+<div>
+ <%= link_to_add_fields "Add Question", form, :questions %>
+</div>
22 app/views/census/data_groups/_question_fields.html.erb
@@ -0,0 +1,22 @@
+<fieldset class="fields">
+ <legend>Question <%= form.object.position %></legend>
+ <div class="question">
+ <%= form.label :prompt, "Label" %>
+ <%= form.text_field :prompt %>
+ <%= link_to_remove_fields "remove", form %>
+ <br/>
+ <%= form.label :data_type %>
+ <%= form.select :data_type, Question.data_types %>
+ </div>
+ <fieldset>
+ <legend>Answer Choices (leave empty to allow freeform text entry)</legend>
+ <% form.fields_for :choices do |builder| %>
+ <%= render 'choice_fields', :form => builder %>
+ <% end %>
+ <div>
+ <%= link_to_add_fields "Add Answer", form, :choices %>
+ </div>
+ <%= form.check_box :multiple %>
+ <%= form.label :multiple, "Allow multiple selections" %>
+ </fieldset>
+</fieldset>
7 app/views/census/data_groups/edit.html.erb
@@ -0,0 +1,7 @@
+<% form_for [:census, @data_group], :html => { :class => "census_data_group_form" } do |form| %>
+<fieldset>
+ <legend>Edit Demographics Data Group</legend>
+ <%= render :partial => 'form', :object => form %>
+ <%= form.submit 'Save', :disable_with => 'Please wait...' %>
+</fieldset>
+<% end %>
10 app/views/census/data_groups/index.html.erb
@@ -0,0 +1,10 @@
+<h2>Demographics Data Groups</h2>
+<ul class="census_data_group_list">
+ <% @data_groups.each do |group| %>
+ <li>
+ <%=link_to group.name, census_data_group_path(group) %>
+ </li>
+ <% end %>
+</ul>
+
+<%= link_to 'New Group', new_census_data_group_path %>
7 app/views/census/data_groups/new.html.erb
@@ -0,0 +1,7 @@
+<% form_for [:census, @data_group], :html => { :class => "census_data_group_form" } do |form| %>
+<fieldset>
+ <legend>New Demographics Data Group</legend>
+ <%= render :partial => 'form', :object => form %>
+ <%= form.submit 'Save', :disable_with => 'Please wait...' %>
+</fieldset>
+<% end %>
73 census.gemspec
@@ -0,0 +1,73 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{census}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Mark Kendall"]
+ s.date = %q{2010-04-05}
+ s.description = %q{Census is a Rails plugin that collects searchable demographics data for each of your application's users.}
+ s.email = %q{mark@envylabs.com}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "app/controllers/census/data_groups_controller.rb",
+ "app/models/answer.rb",
+ "app/models/boolean_question.rb",
+ "app/models/choice.rb",
+ "app/models/data_group.rb",
+ "app/models/number_question.rb",
+ "app/models/question.rb",
+ "app/models/string_question.rb",
+ "app/views/census/data_groups/_choice_fields.html.erb",
+ "app/views/census/data_groups/_form.html.erb",
+ "app/views/census/data_groups/_question_fields.html.erb",
+ "app/views/census/data_groups/edit.html.erb",
+ "app/views/census/data_groups/index.html.erb",
+ "app/views/census/data_groups/new.html.erb",
+ "config/routes.rb",
+ "generators/census/USAGE",
+ "generators/census/census_generator.rb",
+ "generators/census/lib/insert_commands.rb",
+ "generators/census/lib/rake_commands.rb",
+ "generators/census/templates/README",
+ "generators/census/templates/census.js",
+ "generators/census/templates/migrations/census.rb",
+ "generators/census/templates/migrations/create_users.rb",
+ "generators/census/templates/user.rb",
+ "lib/census.rb",
+ "lib/census/user.rb"
+ ]
+ s.homepage = %q{http://github.com/envylabs/census}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{Rails user demographics collection and searching}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<acts_as_list>, ["= 0.1.2"])
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
+ else
+ s.add_dependency(%q<acts_as_list>, ["= 0.1.2"])
+ s.add_dependency(%q<shoulda>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<acts_as_list>, ["= 0.1.2"])
+ s.add_dependency(%q<shoulda>, [">= 0"])
+ end
+end
+
7 config/routes.rb
@@ -0,0 +1,7 @@
+ActionController::Routing::Routes.draw do |map|
+
+ map.namespace :census do |census|
+ census.resources :data_groups, :except => [:show]
+ end
+
+end
1  generators/census/USAGE
@@ -0,0 +1 @@
+script/generate census
36 generators/census/census_generator.rb
@@ -0,0 +1,36 @@
+require File.expand_path(File.dirname(__FILE__) + "/lib/insert_commands.rb")
+require File.expand_path(File.dirname(__FILE__) + "/lib/rake_commands.rb")
+
+class CensusGenerator < Rails::Generator::Base
+
+ def manifest
+ record do |m|
+ m.directory File.join("public", "javascripts")
+ m.file "census.js", "public/javascripts/census.js"
+
+ user_model = "app/models/user.rb"
+ if File.exists?(user_model)
+ m.insert_into user_model, "include Census::User"
+ else
+ m.directory File.join("app", "models")
+ m.file "user.rb", user_model
+ end
+
+ m.directory File.join("test", "factories")
+ m.file "factories.rb", "test/factories/census.rb"
+
+ if ActiveRecord::Base.connection.table_exists?(:users)
+ m.migration_template "migrations/without_users.rb",
+ 'db/migrate',
+ :migration_file_name => "create_census_tables"
+ else
+ m.migration_template "migrations/with_users.rb",
+ 'db/migrate',
+ :migration_file_name => "create_census_tables"
+ end
+
+ m.readme "README"
+ end
+ end
+
+end
33 generators/census/lib/insert_commands.rb
@@ -0,0 +1,33 @@
+# Mostly pinched from http://github.com/ryanb/nifty-generators/tree/master
+
+Rails::Generator::Commands::Base.class_eval do
+ def file_contains?(relative_destination, line)
+ File.read(destination_path(relative_destination)).include?(line)
+ end
+end
+
+Rails::Generator::Commands::Create.class_eval do
+ def insert_into(file, line)
+ logger.insert "#{line} into #{file}"
+ unless options[:pretend] || file_contains?(file, line)
+ gsub_file file, /^(class|module|.*Routes).*$/ do |match|
+ "#{match}\n #{line}"
+ end
+ end
+ end
+end
+
+Rails::Generator::Commands::Destroy.class_eval do
+ def insert_into(file, line)
+ logger.remove "#{line} from #{file}"
+ unless options[:pretend]
+ gsub_file file, "\n #{line}", ''
+ end
+ end
+end
+
+Rails::Generator::Commands::List.class_eval do
+ def insert_into(file, line)
+ logger.insert "#{line} into #{file}"
+ end
+end
22 generators/census/lib/rake_commands.rb
@@ -0,0 +1,22 @@
+Rails::Generator::Commands::Create.class_eval do
+ def rake_db_migrate
+ logger.rake "db:migrate"
+ unless system("rake db:migrate")
+ logger.rake "db:migrate failed. Rolling back"
+ command(:destroy).invoke!
+ end
+ end
+end
+
+Rails::Generator::Commands::Destroy.class_eval do
+ def rake_db_migrate
+ logger.rake "db:rollback"
+ system "rake db:rollback"
+ end
+end
+
+Rails::Generator::Commands::List.class_eval do
+ def rake_db_migrate
+ logger.rake "db:migrate"
+ end
+end
28 generators/census/templates/README
@@ -0,0 +1,28 @@
+
+*******************************************************************************
+
+Next:
+
+1. In order for the question admin views to work, you'll need to include the
+ census.js file in your application layout:
+
+ <%= javascript_link_tag 'census' %>
+
+2. Add a link to the question admin page somewhere in your app:
+
+ <%= link_to 'Set up questions', census_data_groups_path %>
+
+3. Include the partial that displays answers in your user show page:
+
+ <%= render 'census/user_answers', :user => @user %>
+
+4. Include the partial that allows the user to enter answers in your user
+ edit page:
+
+ <%= render 'census/user_questions', :user => @user %>
+
+5. Migrate your database:
+
+ rake db:migrate
+
+*******************************************************************************
10 generators/census/templates/census.js
@@ -0,0 +1,10 @@
+function remove_fields(link) {
+ $(link).prev("input[type=hidden]").val("1");
+ $(link).closest(".fields").hide();
+}
+
+function add_fields(link, association, content) {
+ var new_id = new Date().getTime();
+ var regexp = new RegExp("new_" + association, "g")
+ $(link).parent().before(content.replace(regexp, new_id));
+}
45 generators/census/templates/factories.rb
@@ -0,0 +1,45 @@
+Factory.define :answer do |answer|
+ answer.association :question
+ answer.association :user
+ answer.data "Factory Answer"
+end
+
+Factory.define :choice do |choice|
+ choice.association :question
+ choice.value 'Factory Choice'
+end
+
+Factory.define :data_group do |group|
+ group.name "Factory Data Group"
+end
+
+Factory.define :question do |question|
+ question.association :data_group
+ question.prompt "Enter your response"
+ question.multiple false
+ question.other false
+end
+
+Factory.define :string_question do |question|
+ question.association :data_group
+ question.prompt "Enter a string"
+ question.multiple false
+ question.other false
+end
+
+Factory.define :number_question do |question|
+ question.association :data_group
+ question.prompt "Enter a number"
+ question.multiple false
+ question.other false
+end
+
+Factory.define :boolean_question do |question|
+ question.association :data_group
+ question.prompt "Choose true/false"
+ question.multiple false
+ question.other false
+end
+
+Factory.define :user do |user|
+end
60 generators/census/templates/migrations/with_users.rb
@@ -0,0 +1,60 @@
+class CreateCensusTables < ActiveRecord::Migration
+ def self.up
+ create_table :data_groups do |t|
+ t.string :name
+ t.integer :position
+ t.timestamps
+ end
+
+ create_table :questions do |t|
+ t.integer :data_group_id
+ t.string :type
+ t.string :prompt
+ t.boolean :multiple
+ t.boolean :other
+ t.integer :position
+ t.timestamps
+ end
+
+ add_index :questions, :data_group_id
+
+ create_table :choices do |t|
+ t.integer :question_id
+ t.string :value
+ t.integer :position
+ t.timestamps
+ end
+
+ add_index :choices, :question_id
+
+ create_table(:users) do |t|
+ t.timestamps
+ end
+
+ create_table :answers do |t|
+ t.integer :question_id
+ t.integer :user_id
+ t.string :data
+ end
+
+ add_index :answers, :question_id
+ add_index :answers, :user_id
+
+ end
+
+ def self.down
+ remove_index :answers, :question_id
+ remove_index :answers, :user_id
+ drop_table :answers
+
+ drop_table :users
+
+ remove_index :choices, :question_id
+ drop_table :choices
+
+ remove_index :questions, :data_group_id
+ drop_table :questions
+
+ drop_table :data_groups
+ end
+end
54 generators/census/templates/migrations/without_users.rb
@@ -0,0 +1,54 @@
+class CreateCensusTables < ActiveRecord::Migration
+ def self.up
+ create_table :data_groups do |t|
+ t.string :name
+ t.integer :position
+ t.timestamps
+ end
+
+ create_table :questions do |t|
+ t.integer :data_group_id
+ t.string :type
+ t.string :prompt
+ t.boolean :multiple
+ t.boolean :other
+ t.integer :position
+ t.timestamps
+ end
+
+ add_index :questions, :data_group_id
+
+ create_table :choices do |t|
+ t.integer :question_id
+ t.string :value
+ t.integer :position
+ t.timestamps
+ end
+
+ add_index :choices, :question_id
+
+ create_table :answers do |t|
+ t.integer :question_id
+ t.integer :user_id
+ t.string :data
+ end
+
+ add_index :answers, :question_id
+ add_index :answers, :user_id
+
+ end
+
+ def self.down
+ remove_index :answers, :question_id
+ remove_index :answers, :user_id
+ drop_table :answers
+
+ remove_index :choices, :question_id
+ drop_table :choices
+
+ remove_index :questions, :data_group_id
+ drop_table :questions
+
+ drop_table :data_groups
+ end
+end
3  generators/census/templates/user.rb
@@ -0,0 +1,3 @@
+class User < ActiveRecord::Base
+ include Census::User
+end
1  lib/census.rb
@@ -0,0 +1 @@
+require 'census/user'
85 lib/census/user.rb
@@ -0,0 +1,85 @@
+module Census
+ module User
+
+ # Hook for all Census::User modules.
+ #
+ # If you need to override parts of Census::User,
+ # extend and include à la carte.
+ #
+ # @example
+ # extend ClassMethods
+ # include InstanceMethods
+ # include Callbacks
+ #
+ # @see ClassMethods
+ # @see InstanceMethods
+ # @see Callbacks
+ def self.included(model)
+ model.extend(ClassMethods)
+
+ model.send(:include, InstanceMethods)
+ model.send(:include, Associations)
+ model.send(:include, Callbacks)
+ end
+
+ module Associations
+ # Hook for defining associations.
+ def self.included(model)
+ model.class_eval do
+ has_many :answers, :dependent => :destroy
+ accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:data].blank? }
+ end
+ end
+ end
+
+ module Callbacks
+ # Hook for callbacks.
+ #
+ # empty answers are removed after_save.
+ def self.included(model)
+ model.class_eval do
+ after_save :remove_empty_answers
+ end
+ end
+ end
+
+ module InstanceMethods
+ #
+ # Returns this user's first answer for the given question, or a new empty
+ # answer if the user has not answered the question.
+ #
+ def first_answer_for(question)
+ answers.select {|a| a.question == question}.first || answers.build(:question => question, :data => '')
+ end
+
+ #
+ # Returns an array of this user's answers for the given question. The returned
+ # array will be empty if the user has not answered this question.
+ #
+ def all_answers_for(question)
+ answers.select {|a| a.question == question}
+ end
+
+ #
+ # Returns this user's answer for a specific choice under a multiple-choice
+ # question, or a new empty answer if the user did not select the given choice.
+ #
+ def answer_for_choice(choice)
+ answers.select {|a| a.question == choice.question && a.data == choice.value}.first || answers.build(:question => choice.question, :data => '')
+ end
+
+ private
+
+ #
+ # After save callback, used to remove blank answers
+ #
+ def remove_empty_answers
+ answers.each {|answer| answer.destroy if answer.data.blank? }
+ end
+ end
+
+ module ClassMethods
+ end
+
+ end
+end
7 rails/init.rb
@@ -0,0 +1,7 @@
+require 'acts_as_list'
+require 'inverse_of'
+require 'census'
+
+config.to_prepare do
+ ApplicationController.helper(CensusHelper)
+end
35 shoulda_macros/census.rb
@@ -0,0 +1,35 @@
+module Census
+ module Shoulda
+
+ def should_accept_nested_attributes_for(*attr_names)
+ klass = self.name.gsub(/Test$/, '').constantize
+
+ context "#{klass}" do
+ attr_names.each do |association_name|
+ should "accept nested attrs for #{association_name}" do
+ meth = "#{association_name}_attributes="
+ assert ([meth,meth.to_sym].any?{ |m| klass.instance_methods.include?(m) }),
+ "#{klass} does not accept nested attributes for #{association_name}"
+ end
+ end
+ end
+ end
+
+ def should_act_as_list
+ klass = self.name.gsub(/Test$/, '').constantize
+
+ context "To support acts_as_list" do
+ should_have_db_column('position', :type => :integer)
+ end
+
+ should "include ActsAsList methods" do
+ assert klass.include?(ActsAsList::InstanceMethods)
+ end
+
+ should_have_instance_methods :acts_as_list_class, :position_column, :scope_condition
+ end
+
+ end
+end
+
+Test::Unit::TestCase.extend(Census::Shoulda)
165 test/controllers/data_groups_controller_test.rb
@@ -0,0 +1,165 @@
+require 'test_helper'
+
+class DataGroupsControllerTest < ActionController::TestCase
+
+ tests Census::DataGroupsController
+
+ should_route :get, '/census/data_groups',
+ :controller => 'census/data_groups', :action => 'index'
+
+ should_route :get, '/census/data_groups/new',
+ :controller => 'census/data_groups', :action => 'new'
+
+ should_route :post, '/census/data_groups',
+ :controller => 'census/data_groups', :action => 'create'
+
+ should_route :get, '/census/data_groups/1/edit',
+ :controller => 'census/data_groups', :action => 'edit', :id => '1'
+
+ should_route :put, '/census/data_groups/1',
+ :controller => 'census/data_groups', :action => 'update', :id => '1'
+
+ should_route :delete, '/census/data_groups/1',
+ :controller => 'census/data_groups', :action => 'destroy', :id => '1'
+
+ context 'The Census::DataGroupsController' do
+
+ context 'using GET to index' do
+
+ setup do
+ @group = Factory(:data_group)
+ get :index
+ end
+
+ should_respond_with :success
+ should_respond_with_content_type :html
+ should_render_template :index
+ should_assign_to :data_groups
+
+ end
+
+ context 'using GET to new' do
+
+ setup do
+ get :new
+ end
+
+ should_respond_with :success
+ should_respond_with_content_type :html
+ should_render_template :new
+ should_assign_to :data_group
+
+ should 'have a new data group record' do
+ assert assigns(:data_group).new_record?
+ end
+
+ end
+
+ context 'using POST to create' do
+
+ context 'with invalid attributes' do
+
+ setup do
+ DataGroup.any_instance.stubs(:valid?).returns(false)
+ post :create, :data_group => Factory.attributes_for(:data_group)
+ end
+
+ should_respond_with :unprocessable_entity
+ should_respond_with_content_type :html
+ should_render_template :new
+ should_assign_to :data_group
+
+ should 'not create the data group' do
+ assert assigns(:data_group).new_record?
+ end
+
+ end
+
+ context 'with valid attributes' do
+
+ setup do
+ post :create, :data_group => Factory.attributes_for(:data_group)
+ end
+
+ should_respond_with :redirect
+ should_assign_to :data_group
+ should_redirect_to('census admin page') { census_data_groups_url }
+
+ should 'create the data group' do
+ assert !assigns(:data_group).new_record?
+ end
+
+ end
+
+ end
+
+ context 'using GET to edit' do
+
+ setup do
+ @group = Factory(:data_group)
+ get :edit, :id => @group.to_param
+ end
+
+ should_respond_with :success
+ should_respond_with_content_type :html
+ should_render_template :edit
+ should_assign_to(:data_group) { @group }
+
+ end
+
+ context 'using PUT to update' do
+
+ context 'with valid attributes' do
+
+ setup do
+ @group = Factory(:data_group)
+ put :update, :id => @group.to_param, :data_group => {:name => 'CHANGED'}
+ end
+
+ should_respond_with :redirect
+ should_assign_to(:data_group) { @group }
+ should_redirect_to('census admin page') { census_data_groups_url }
+
+ should 'update the data group' do
+ assert_equal('CHANGED', @group.reload.name)
+ end
+
+ end
+
+ context 'with invalid attributes' do
+
+ setup do
+ @group = Factory(:data_group)
+ DataGroup.any_instance.stubs(:valid? => false)
+ put :update, :id => @group.to_param, :data_group => {:name => 'CHANGED'}
+ end
+
+ should_respond_with :unprocessable_entity
+ should_respond_with_content_type :html
+ should_assign_to(:data_group) { @group }
+ should_render_template :edit
+
+ end
+
+ end
+
+ context 'using DELETE to destroy' do
+
+ setup do
+ @group = Factory(:data_group)
+ delete :destroy, :id => @group.to_param
+ end
+
+ should_respond_with :redirect
+ should_assign_to(:data_group) { @group }
+ should_redirect_to('census admin page') { census_data_groups_url }
+
+ should 'destroy the data group' do
+ assert_nil(DataGroup.find_by_id(@group.id))
+ end
+
+ end
+
+ end
+
+end
44 test/models/answer_test.rb
@@ -0,0 +1,44 @@
+require 'test_helper'
+
+class AnswerTest < ActiveSupport::TestCase
+
+ context "An Answer" do
+
+ setup do
+ @answer = Factory(:answer)
+ end
+
+ subject { @answer }
+
+ should_belong_to :question
+ should_belong_to :user
+
+ should_validate_presence_of :question,
+ :user
+
+ should_allow_mass_assignment_of :data
+
+ context "getting formatted data" do
+
+ should "format strings" do
+ a = Factory(:answer, :question => Factory(:string_question), :data => 'abc123')
+ assert_equal 'abc123', a.formatted_data
+ end
+
+ should "format numbers" do
+ a = Factory(:answer, :question => Factory(:number_question), :data => '5389')
+ assert_equal 5389, a.formatted_data
+ end
+
+ should "format booleans" do
+ a = Factory(:answer, :question => Factory(:boolean_question), :data => '0')
+ assert_equal false, a.formatted_data
+ a.data = '1'
+ assert_equal true, a.formatted_data
+ end
+
+ end
+
+ end
+
+end
45 test/models/boolean_question_test.rb
@@ -0,0 +1,45 @@
+require 'test_helper'
+
+class BooleanQuestionTest < ActiveSupport::TestCase
+
+ context "A BooleanQuestion" do
+
+ setup do
+ @question = Factory(:boolean_question)
+ end
+
+ subject { @question }
+
+ should "return a boolean sql transform" do
+ assert_equal "CAST(? AS CHAR)", @question.sql_transform
+ end
+
+ context "with answers" do
+
+ setup do
+ @answer1 = Factory(:answer, :question => @question)
+ @answer1.update_attribute(:data, false)
+ @answer2 = Factory(:answer, :question => @question, :data => true)
+ end
+
+ should "find answers matching true" do
+ assert @question.find_answers_matching(true).include?(@answer2)
+ end
+
+ should "not find answers not matching true" do
+ assert !@question.find_answers_matching(true).include?(@answer1)
+ end
+
+ should "find answers matching false" do
+ assert @question.find_answers_matching(false).include?(@answer1)
+ end
+
+ should "not find answers not matching false" do
+ assert !@question.find_answers_matching(false).include?(@answer2)
+ end
+
+ end
+
+ end
+
+end
24 test/models/choice_test.rb
@@ -0,0 +1,24 @@
+require 'test_helper'
+
+class ChoiceTest < ActiveSupport::TestCase
+
+ context "A Choice" do
+
+ setup do
+ @choice = Factory(:choice)
+ end
+
+ subject { @choice }
+
+ should_belong_to :question
+
+ should_act_as_list
+
+ should_validate_presence_of :value,
+ :question
+
+ should_allow_mass_assignment_of :value
+
+ end
+
+end
12 test/models/data_group_test.rb
@@ -0,0 +1,12 @@
+require 'test_helper'
+
+class DataGroupTest < ActiveSupport::TestCase
+
+ should_have_many :questions, :dependent => :destroy
+ should_accept_nested_attributes_for :questions
+
+ should_validate_presence_of :name
+
+ should_act_as_list
+
+end
52 test/models/number_question_test.rb
@@ -0,0 +1,52 @@
+require 'test_helper'
+
+class NumberQuestionTest < ActiveSupport::TestCase
+
+ context "A NumberQuestion" do
+
+ setup do
+ @question = Factory(:number_question)
+ end
+
+ subject { @question }
+
+ should "return an integer sql transform" do
+ assert_equal "CAST(? AS SIGNED INTEGER)", @question.sql_transform
+ end
+
+ context "with answers" do
+
+ setup do
+ @answer1 = Factory(:answer, :question => @question, :data => '123')
+ @answer2 = Factory(:answer, :question => @question, :data => '125')
+ @answer3 = Factory(:answer, :question => @question, :data => '127')
+ end
+
+ should "find answers matching a given string" do
+ assert @question.find_answers_matching('123').include?(@answer1)
+ end
+
+ should "not find answers not matching the given string" do
+ assert !@question.find_answers_matching('123').include?(@answer2)
+ end
+
+ should "find answers matching a given number" do
+ assert @question.find_answers_matching(123).include?(@answer1)
+ end
+
+ should "not find answers not matching the given number" do
+ assert !@question.find_answers_matching(123).include?(@answer2)
+ end
+
+ should "find answers in a given range" do
+ result = @question.find_answers_matching(123..126)
+ assert result.include?(@answer1)
+ assert result.include?(@answer2)
+ assert !result.include?(@answer3)
+ end
+
+ end
+
+ end
+
+end
92 test/models/question_test.rb
@@ -0,0 +1,92 @@
+require 'test_helper'
+
+class QuestionTest < ActiveSupport::TestCase
+
+ context "A Question" do
+
+ setup do
+ @question = Factory(:question)
+ end
+
+ subject { @question }
+
+ should_belong_to :data_group
+ should_have_many :choices, :dependent => :destroy
+ should_have_many :answers, :dependent => :destroy
+
+ should_act_as_list
+
+ should_validate_presence_of :prompt,
+ :data_group
+
+ should_allow_mass_assignment_of :prompt,
+ :multiple,
+ :other
+
+ should_accept_nested_attributes_for :choices
+
+ should "return a default sql transform" do
+ assert '"?"', @question.sql_transform
+ end
+
+ context "with choices" do
+
+ setup do
+ @choice1 = Factory(:choice, :value => 'Choice 1', :question => @question)
+ @choice2 = Factory(:choice, :value => 'Choice 2', :question => @question)
+ @choice3 = Factory(:choice, :value => 'Choice 3', :question => @question)
+ end
+
+ should "allow answers that match the choices" do
+ assert Factory.build(:answer, :question => @question, :data => 'Choice 1').valid?
+ assert Factory.build(:answer, :question => @question, :data => 'Choice 3').valid?
+ end
+
+ should "not allow answers that don't match the choices" do
+ assert !Factory.build(:answer, :question => @question, :data => 'Blah').valid?
+ end
+
+ context "that allows user-supplied 'other' answer" do
+
+ setup do
+ @question.update_attribute(:other, true)
+ end
+
+ should "allow answers that don't match the choices" do
+ assert Factory.build(:answer, :question => @question, :data => 'Blah').valid?
+ end
+
+ end
+
+ end
+
+ context "that can't have multiple answers" do
+
+ setup do
+ @question.update_attribute(:multiple, false)
+ @user = Factory(:user)
+ end
+
+ should "not be able to create multiple answers" do
+ assert @question.answers.create(:data => 'Answer 1', :user => @user)
+ assert !Factory.build(:answer, :question => @question, :data => 'Answer 2', :user => @user).valid?
+ end
+
+ end
+
+ context "that can have multiple answers" do
+
+ setup do
+ @question.update_attribute(:multiple, true)
+ end
+
+ should "be able to create multiple answers" do
+ assert @question.answers.create(:data => 'Answer 1')
+ assert @question.answers.create(:data => 'Answer 2')
+ end
+
+ end
+
+ end
+
+end
44 test/models/string_question_test.rb
@@ -0,0 +1,44 @@
+require 'test_helper'
+
+class StringQuestionTest < ActiveSupport::TestCase
+
+ context "A StringQuestion" do
+
+ setup do
+ @question = Factory(:string_question)
+ end
+
+ subject { @question }
+
+ should "return a default sql transform" do
+ assert_equal "?", @question.sql_transform
+ end
+
+ context "with answers" do
+
+ setup do
+ @answer1 = Factory(:answer, :question => @question, :data => 'findme')
+ @answer2 = Factory(:answer, :question => @question, :data => 'dont_findme')
+ @answer3 = Factory(:answer, :question => @question, :data => 'findme_too')
+ end
+
+ should "find answers matching a given string" do
+ assert @question.find_answers_matching('findme').include?(@answer1)
+ end
+
+ should "not find answers not matching the given string" do
+ assert !@question.find_answers_matching('findme').include?(@answer2)
+ end
+
+ should "find answers matching an array of strings" do
+ result = @question.find_answers_matching(['findme', 'findme_too'])
+ assert result.include?(@answer1)
+ assert result.include?(@answer3)
+ assert !result.include?(@answer2)
+ end
+
+ end
+
+ end
+
+end
9 test/models/user_test.rb
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+class UserTest < ActiveSupport::TestCase
+
+ should_have_many :answers, :dependent => :destroy
+ should_accept_nested_attributes_for :answers
+ should_allow_mass_assignment_of :answers_attributes
+
+end
19 test/test_helper.rb
@@ -0,0 +1,19 @@
+ENV["RAILS_ENV"] = "test"
+RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + "/rails_root")
+require File.expand_path(File.dirname(__FILE__) + "/rails_root/config/environment")
+require 'test_help'
+
+$: << File.expand_path(File.dirname(__FILE__) + '/..')
+require 'census'
+
+begin
+ require 'redgreen'
+rescue LoadError
+end
+
+require File.join(File.dirname(__FILE__), '..', 'shoulda_macros', 'census')
+
+class ActiveSupport::TestCase
+ self.use_transactional_fixtures = true
+ self.use_instantiated_fixtures = false
+end
Please sign in to comment.
Something went wrong with that request. Please try again.