Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit. Includes DCData models and tests. (Even though the te…

…sts need some help based on the datasets)
  • Loading branch information...
commit 61700a1b8818af3794be3373c0463858ef8577b3 0 parents
@markmcspadden markmcspadden authored
38 README.textile
@@ -0,0 +1,38 @@
+h1. donors_choose
+
+This library sprung from the 2011 Hacking Education contest for DonorsChoose.org. (http://www.donorschoose.org/hacking-education)
+
+The DCData module match up the normalized data provided by DonorsChoose.org to classes that inherit from ActiveRecord::Base.
+
+*This gem assumes you have loaded the DonorsChoose.org datasets into a PostgreSQL database.*
+
+h2. Usage
+
+<code>
+ project = DCData::Project.first
+ project.donations
+
+ school = DCData::School.last
+ school.teachers
+</code>
+
+
+h3. Rails
+If you'd like to use it in Rails, just add the following to your Gemfile:
+
+<code>
+ gem 'donors_choose'
+</code>
+
+h3. Stand alone
+If you'd like to use it stand alone, do the following:
+
+Copy database.yml.example to database.yml and change the credentials to match your setup
+
+<code>
+ terminal$ irb
+ irb(main):001:0> require 'd_c_data'
+ irb(main):001:0> DCData.connect
+ irb(main):001:0> DCData::Project.first
+</code>
+
11 Rakefile
@@ -0,0 +1,11 @@
+require 'rake'
+require 'rake/testtask'
+
+task :default => [:test_units]
+
+desc "Run basic tests"
+Rake::TestTask.new("test_units") { |t|
+ t.pattern = 'test/*/*_test.rb'
+ t.verbose = true
+ t.warning = true
+}
38 d_c_data.rb
@@ -0,0 +1,38 @@
+require 'rubygems'
+require 'active_record'
+
+# Setup logging with ActiveRecord
+require 'logger'
+def setup_log(destination=STDOUT)
+ puts "A log of the sql queries will be outputted to: #{destination}"
+
+ if destination != STDOUT
+ File.delete(destination) if File.exist?(destination)
+ end
+
+ log = Logger.new destination
+ log.level = Logger::DEBUG
+ ActiveRecord::Base.logger = log
+end
+
+module DCData
+
+ # Use this if you're going standalone (aka sans Rails)
+ # Probably want to move database.yml.example to database.yml and change the credentials
+ def self.connect
+ puts "**** SETTING UP Postgresql ****"
+
+ setup_log File.expand_path(File.dirname(__FILE__) + "/postgresql.log")
+
+ require 'pg'
+ postgres_config = YAML.load(File.read('database.yml'))
+
+ # Connect with db
+ ActiveRecord::Base.establish_connection(postgres_config)
+ end
+
+end
+
+Dir.glob("#{File.dirname __FILE__}/d_c_data/*.rb").each do |f|
+ require f
+end
8 d_c_data/account.rb
@@ -0,0 +1,8 @@
+module DCData
+ class Account < ActiveRecord::Base
+ set_table_name :normalized_account
+ set_primary_key :_acctid
+
+ belongs_to :city, :foreign_key => :cityid
+ end
+end
7 d_c_data/city.rb
@@ -0,0 +1,7 @@
+module DCData
+ class City < ActiveRecord::Base
+ set_table_name :normalized_city
+
+ has_many :accounts, :foreign_key => :cityid
+ end
+end
6 d_c_data/donation.rb
@@ -0,0 +1,6 @@
+class Donation < ActiveRecord::Base
+ set_table_name :normalized_donation
+ set_primary_key :_donationid
+
+ belongs_to :project, :foreign_key => :_projectid
+end
8 d_c_data/essay.rb
@@ -0,0 +1,8 @@
+module DCData
+ class Essay < ActiveRecord::Base
+ set_table_name :normalized_essay
+ set_primary_key :_projectid
+
+ belongs_to :project, :foreign_key => :_projectid
+ end
+end
6 d_c_data/giftcard.rb
@@ -0,0 +1,6 @@
+module DCData
+ class Giftcard < ActiveRecord::Base
+ set_table_name :normalized_giftcard
+ set_primary_key :_giftcardid
+ end
+end
14 d_c_data/project.rb
@@ -0,0 +1,14 @@
+module DCData
+ class Project < ActiveRecord::Base
+ set_table_name :normalized_project
+ set_primary_key :_projectid
+
+ belongs_to :school, :foreign_key => :_schoolid
+ belongs_to :teacher, :foreign_key => :_teacher_acctid
+
+ has_one :essay, :foreign_key => :_projectid
+
+ has_many :donations, :foreign_key => :_projectid
+ has_many :resources, :foreign_key => :_projectid
+ end
+end
8 d_c_data/resource.rb
@@ -0,0 +1,8 @@
+module DCData
+ class Resource < ActiveRecord::Base
+ set_table_name :normalized_resource
+ set_primary_key :_resourceid
+
+ belongs_to :project, :foreign_key => :_projectid
+ end
+end
9 d_c_data/school.rb
@@ -0,0 +1,9 @@
+module DCData
+ class School < ActiveRecord::Base
+ set_table_name :normalized_school
+ set_primary_key :_schoolid
+
+ has_many :projects, :foreign_key => :_schoolid
+ has_many :teachers, :through => :projects
+ end
+end
9 d_c_data/teacher.rb
@@ -0,0 +1,9 @@
+module DCData
+ class Teacher < ActiveRecord::Base
+ set_table_name :normalized_teacher
+ set_primary_key :_teacher_acctid
+
+ has_many :projects, :foreign_key => :_teacher_acctid
+ has_many :schools, :through => :projects
+ end
+end
7 database.yml.example
@@ -0,0 +1,7 @@
+adapter: postgresql
+host: localhost
+username: postgres
+password: password
+port: 5432
+database: donors_choose_data
+schema_search_path: public
17 test/d_c_data/account_test.rb
@@ -0,0 +1,17 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+class AccountTest < Test::Unit::TestCase
+
+ def setup
+ @account = DCData::Account.first
+ end
+
+ def test_account_setup
+ assert_equal "6e1d31eea9f0495d5bcfd0ab9b0b4ac3", @account._acctid
+ end
+
+ def test_city_setup
+ assert_equal 15003, @account.city.id
+ end
+
+end
17 test/d_c_data/city_test.rb
@@ -0,0 +1,17 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+class CityTest < Test::Unit::TestCase
+
+ def setup
+ @city = DCData::City.first
+ end
+
+ def test_city_setup
+ assert_equal 1, @city.id
+ end
+
+ def test_accounts_setup
+ assert_equal "c4127fe826abef12174ca4d0e7288f88", @city.accounts.first._acctid
+ end
+
+end
16 test/d_c_data/donation_test.rb
@@ -0,0 +1,16 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+class DonationTest < Test::Unit::TestCase
+
+ def setup
+ @donation = DCData::Donation.first
+ end
+
+ def test_donation_setup
+ assert_equal "798b59bfda2a7dd69a1a02f6bb007969", @donation._donationid
+ end
+
+ def test_project_setup
+ assert_equal "364c61a92927942f7241ecdc1dcc1c04", @donation.project._projectid
+ end
+end
17 test/d_c_data/essay_test.rb
@@ -0,0 +1,17 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+class EssayTest < Test::Unit::TestCase
+
+ def setup
+ @essay = DCData::Essay.first
+ end
+
+ def test_essay_setup
+ assert_equal "2a68c0296a73dcb868b5d35145935462", @essay._projectid
+ end
+
+ def test_project_setup
+ assert_equal "2a68c0296a73dcb868b5d35145935462", @essay.project._projectid
+ end
+
+end
13 test/d_c_data/giftcard_test.rb
@@ -0,0 +1,13 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+class GiftcardTest < Test::Unit::TestCase
+
+ def setup
+ @giftcard = DCData::Giftcard.first
+ end
+
+ def test_giftcard_setup
+ assert_equal "7c2fa2b7b826c6a75a4ec1519f937537", @giftcard._giftcardid
+ end
+
+end
33 test/d_c_data/project_test.rb
@@ -0,0 +1,33 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+class ProjectTest < Test::Unit::TestCase
+
+ def setup
+ @project = DCData::Project.first
+ end
+
+ def test_project_setup
+ assert_equal "b1ee1388beb00573ef0ab81e2b7243c4", @project._projectid
+ end
+
+ def test_donation_setup
+ assert_equal "8a88748e91884b5f03bbaa5f4fcfd036", @project.donations.first._donationid
+ end
+
+ def test_essay_setup
+ assert_equal "b1ee1388beb00573ef0ab81e2b7243c4", @project.essay._projectid
+ end
+
+ def test_teacher_setup
+ assert_equal "590154bc5343aae1f958b043139b3d62", @project.teacher._teacher_acctid
+ end
+
+ def test_resources_setup
+ assert_equal "590154bc5343aae1f958b043139b3d62", @project.resources.first._resourceid
+ end
+
+ def test_school_setup
+ assert_equal "df2afbda9d169825b454772337b797c8", @project.school._schoolid
+ end
+
+end
17 test/d_c_data/resource_test.rb
@@ -0,0 +1,17 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+class ResourceTest < Test::Unit::TestCase
+
+ def setup
+ @resource = DCData::Resource.first
+ end
+
+ def test_resource_setup
+ assert_equal "d44b79386ab2353c9409736c3994e83f", @resource._resourceid
+ end
+
+ def test_project_setup
+ assert_equal "2a68c0296a73dcb868b5d35145935462", @resource.project._projectid
+ end
+
+end
16 test/d_c_data/school_test.rb
@@ -0,0 +1,16 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+class SchoolTest < Test::Unit::TestCase
+
+ def setup
+ @school = DCData::School.first
+ end
+
+ def test_school_setup
+ assert_equal "4effb11a762d152b107678ab4744063b", @school._schoolid
+ end
+
+ def test_project_setup
+ assert_equal "6eb774f01d75b29a3c1971040d9d1b5a", @school.projects.first._projectid
+ end
+end
21 test/d_c_data/teacher_test.rb
@@ -0,0 +1,21 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+class TeacherTest < Test::Unit::TestCase
+
+ def setup
+ @teacher = DCData::Teacher.first
+ end
+
+ def test_teacher_setup
+ assert_equal "d04fe28078bb087996351d7a6d0c20d1", @teacher._teacher_acctid
+ end
+
+ def test_projects_setup
+ assert_equal "f9065c14b96ae492fb62924acb115521", @teacher.projects.first._projectid
+ end
+
+ def test_schools_setup
+ assert_equal "39ddbff0638271cd2eb7793af1c91237", @teacher.schools.first._schoolid
+ end
+
+end
5 test/test_helper.rb
@@ -0,0 +1,5 @@
+require 'test/unit'
+
+require File.expand_path(File.dirname(__FILE__) + "/../d_c_data")
+
+DCData.connect
Please sign in to comment.
Something went wrong with that request. Please try again.