Permalink
Browse files

adding tests, changelog and updating documentation for first usable r…

…elease
  • Loading branch information...
1 parent 8848069 commit 4fdfa74a41e86e4c1ee517f35afc1eb5294e083d @briandoll briandoll committed Nov 21, 2009
Showing with 94 additions and 17 deletions.
  1. +14 −5 README.rdoc
  2. +2 −3 Rakefile
  3. +9 −0 changelog.rdoc
  4. +0 −2 lib/st-elsewhere.rb
  5. +62 −5 test/st-elsewhere_test.rb
  6. +7 −2 test/test_helper.rb
View
19 README.rdoc
@@ -10,11 +10,11 @@ For a variety of reasons, you might find yourself supporting multiple databases
=== The Problem
-While there may be great benefits to connecting to multiple databases in your app, there are also costs. One example is that <b><tt>has_many :children, :through => parent_children</tt></b> does not work.
+While there may be great benefits to connecting to multiple databases in your app, there are also costs. One example is that <b><tt>has_many :children, :through => parent_children</tt></b> may not work.
-You'll encounter one of two errors, depending on your setup:
-* If the schemas are different, you'll see something like: <tt>ActiveRecord::StatementInvalid: Mysql::Error: Table 'appname_transactional.parent_children' doesn't exist</tt>
-* If the schemas are the same but data only exists in one schema or the other, you'll just get empty relationships.
+If your database connections are available on the same host {you can prefix your ActiveRecord table names with the database name}[http://gist.github.com/239853].
+
+If your database connections are on two different hosts, no JOINs can save you and you'll need to implement your relationships in code.
=== The Solution
@@ -52,4 +52,13 @@ http://emphaticsolutions.com/images/has_many_elsewhere.png
<tt>has_many_elsewhere</tt> is certainly much less efficient than a comparable has_many relationship. <tt>has_many :through</tt> relationships use SQL JOINs which while efficient, do not work across multiple database connections. St. Elsewhere implements much of the same resulting API methods in code, using less efficient SQL.
=== Install from gemcutter
- gem install st-elsewhere
+
+ gem install st-elsewhere
+
+=== Roadmap
+
+Currently st-elsewhere is implemented as a basic ruby module that implements some of the basic functionality of has_many :through relationships in ActiveRecord. A much more robust implementation would be to create an ActiveRecord association proxy, like HasManyThroughAssociation, that emulates the same API and could be integrated into the standard has_many class method. I will likely be waiting for Rails 3 to be released (and thus the new base ORM implementation) before attempting the association proxy route.
+
+=== Thanks
+
+Thanks to {James Reynolds}[http://drtoast.com/] for the great name and thanks to {Tanner Donovan}[http://github.com/ttdonovan] for patches and being the first production customer.
View
5 Rakefile
@@ -10,8 +10,7 @@ begin
gem.email = "brian@emphaticsolutions.com"
gem.homepage = "http://github.com/briandoll/st-elsewhere"
gem.authors = ["Brian Doll"]
- # gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ gem.add_development_dependency "rr", ">= 0"
end
Jeweler::GemcutterTasks.new
rescue LoadError
@@ -21,7 +20,7 @@ end
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
- test.pattern = 'test/**/test_*.rb'
+ test.pattern = 'test/*test.rb'
test.verbose = true
end
View
9 changelog.rdoc
@@ -0,0 +1,9 @@
+0.1.5 "Ed Flanders"
+ - This is the first acceptable production release of st-elsewhere.
+ - Implements:
+ - has_many_elsewhere :association, :through => :through_association
+ - Provides:
+ - ARKlass#associations
+ - ARKlass#association_ids
+ - ARKlass#associations=
+ - ARKlass#association_ids=
View
2 lib/st-elsewhere.rb
@@ -5,8 +5,6 @@ module StElsewhere
#
# The following methods for retrieval and query of collections of associated objects will be added:
#
- # [collection<<(object, ...)]
- # TODO: Adds one or more objects to the collection by setting their foreign keys to the collection's primary key.
# [collection=objects]
# Replaces the collections content by deleting and adding objects as appropriate.
# [collection_singular_ids]
View
67 test/st-elsewhere_test.rb
@@ -1,8 +1,65 @@
+# These tests are insanely incomplete. Testing a module like this without
+# depending on an encompasing Rails application is quite difficult. These
+# tests use mocks on the objects under test, which feels almost useless.
+# I may consider publishing an accompanyhing Rails application with a test
+# suite that tests this module in a usable state.
+
require 'test_helper'
+require 'st-elsewhere'
-class StElsewhereTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
+class Doctor < Struct.new :id, :name
+end
+
+class HospitalDoctor < Struct.new :id, :doctor_id, :hospital_id
+ attr_accessor :doctor, :hospital
end
+
+class Hospital < Struct.new :id, :name
+ extend StElsewhere
+ has_many_elsewhere :doctors, :through => :hospital_doctors
+end
+
+class StElsewhereTest < Test::Unit::TestCase
+
+ def setup
+ @hospital = Hospital.new
+ @hospital.id = 1
+ @hospital.name = "St. Elsewhere"
+
+ @doctor = Doctor.new
+ @doctor.id = 1
+ @doctor.name = "Dr. Foo"
+
+ @doctor2 = Doctor.new
+ @doctor2.id = 2
+ @doctor2.name = "Dr. Bar"
+
+ @hospital_doctor = HospitalDoctor.new
+ @hospital_doctor.id = 1
+ @hospital_doctor.doctor_id = @doctor.id
+ @hospital_doctor.hospital_id = @hospital.id
+
+ @hospital_doctor2 = HospitalDoctor.new
+ @hospital_doctor2.id = 2
+ @hospital_doctor2.doctor_id = @doctor2.id
+ @hospital_doctor2.hospital_id = @hospital.id
+ end
+
+ def test_basic_obj_setup
+ assert "St. Elsewhere".eql?(@hospital.name)
+ assert @hospital.respond_to? :doctors
+ assert @hospital.respond_to? :doctors=
+ assert @hospital.respond_to? :doctor_ids
+ assert @hospital.respond_to? :doctor_ids=
+ end
+
+ def test_basic_functionality
+ mock(@hospital_doctor).doctor {@doctor}
+ mock(@hospital_doctor2).doctor {@doctor2}
+ mock(HospitalDoctor).find([1,2]) {[@hospital_doctor, @hospital_doctor2]}
+ mock(@hospital).hospital_doctor_ids {[@hospital_doctor.id, @hospital_doctor2.id]}
+
+ assert @hospital.doctors.eql?([@doctor, @doctor2])
+ end
+
+end
View
9 test/test_helper.rb
@@ -1,3 +1,8 @@
require 'rubygems'
-require 'active_support'
-require 'active_support/test_case'
+require 'test/unit'
+require 'active_record'
+require 'rr'
+
+class Test::Unit::TestCase
+ include RR::Adapters::TestUnit
+end

0 comments on commit 4fdfa74

Please sign in to comment.