Permalink
Browse files

generic elevator specs plus doco on using it

  • Loading branch information...
1 parent e3b65b6 commit 2f8e27b523c3f7de4f77571867c5a10e9d8d8241 @bradrobertson bradrobertson committed Jun 1, 2012
Showing with 39 additions and 3 deletions.
  1. +26 −2 README.md
  2. +3 −1 spec/dummy/config/application.rb
  3. +10 −0 spec/integration/middleware/generic_elevator_spec.rb
View
@@ -60,8 +60,9 @@ database, call switch with no arguments.
You can have Apartment route to the appropriate database by adding some Rack middleware.
Apartment can support many different "Elevators" that can take care of this routing to your data.
-In house, we use the subdomain elevator, which analyzes the subdomain of the request and switches
-to a database schema of the same name. It can be used like so:
+
+**Switch on subdomain**
+In house, we use the subdomain elevator, which analyzes the subdomain of the request and switches to a database schema of the same name. It can be used like so:
# application.rb
module My Application
@@ -71,6 +72,29 @@ to a database schema of the same name. It can be used like so:
end
end
+**Switch on domain**
+To switch based on full domain (excluding subdomains *ie 'www'* and top level domains *ie '.com'* ) use the following:
+
+ # application.rb
+ module My Application
+ class Application < Rails::Application
+
+ config.middleware.use 'Apartment::Elevators::Domain'
+ end
+ end
+
+**Custom Elevator**
+A Generic Elevator exists that allows you to pass a `Proc` (or anything that responds to `call`) to the middleware. This Object will be passed in an `ActionDispatch::Request` object when called for you to do your magic. Apartment will use the return value of this proc to switch to the appropriate database. Use like so:
+
+ # application.rb
+ module My Application
+ class Application < Rails::Application
+ # Obviously not a contrived example
+ config.middleware.use 'Apartment::Elevators::Generic', Proc.new { |request| request.host.reverse }
+ end
+ end
+
+
## Config
The following config options should be set up in a Rails initializer such as:
@@ -17,7 +17,9 @@ class Application < Rails::Application
config.middleware.use 'Apartment::Elevators::Subdomain'
config.middleware.use 'Apartment::Elevators::Domain'
-
+ # Our test for this middleware is using a query_string couldn't think of a better way to differentiate it from the other middleware
+ config.middleware.use 'Apartment::Elevators::Generic', Proc.new { |request| request.query_string.split('=').last if request.query_string.present? }
+
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/lib)
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+describe Apartment::Elevators::Generic, :elevator => true do
+
+ # NOTE, see spec/dummy/config/application.rb to see the Proc that defines the behaviour here
+ let(:domain1) { "http://#{database1}.com?db=#{database1}" }
+ let(:domain2) { "http://#{database2}.com?db=#{database2}" }
+
+ it_should_behave_like "an apartment elevator"
+end

0 comments on commit 2f8e27b

Please sign in to comment.