connect_with 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +1 -0
- data/.gitignore +4 -0
- data/MIT-License +22 -0
- data/Manifest +12 -0
- data/Rakefile +28 -0
- data/VERSION +1 -0
- data/lib/connect_with.rb +6 -0
- data/lib/connect_with/orms/active_record.rb +17 -0
- data/lib/connect_with/railtie.rb +12 -0
- data/spec/connect_with_spec.rb +44 -0
- data/spec/fixtures/database.yml +20 -0
- data/spec/fixtures/models.rb +2 -0
- data/spec/spec_helper.rb +14 -0
- metadata +68 -0
data/.autotest
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Autotest.add_discovery { "rspec2" }
|
data/.gitignore
ADDED
data/MIT-License
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009 Chris Herring
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
MIT-License
|
2
|
+
Manifest
|
3
|
+
README.markdown
|
4
|
+
Rakefile
|
5
|
+
config/database.example.yml
|
6
|
+
connect_with.gemspec
|
7
|
+
lib/connect_with.rb
|
8
|
+
spec/connect_with_spec.rb
|
9
|
+
spec/fixtures/models.rb
|
10
|
+
spec/fixtures/structure.sql
|
11
|
+
spec/spec_helper.rb
|
12
|
+
spec/test_helper.rb
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "connect)with"
|
8
|
+
gem.summary = %Q{Handle multiple Database conenctions with ActiveRecord Models}
|
9
|
+
gem.description = %Q{Handle multiple Database conenctions with ActiveRecord Models}
|
10
|
+
gem.email = "chris.herring.iphone@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/cherring/connection_ninja"
|
12
|
+
gem.authors = ["Chris Herring"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/rdoctask'
|
21
|
+
Rake::RDocTask.new do |rdoc|
|
22
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
23
|
+
|
24
|
+
rdoc.rdoc_dir = 'rdoc'
|
25
|
+
rdoc.title = "connect with #{version}"
|
26
|
+
rdoc.rdoc_files.include('README*')
|
27
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
28
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.4.1
|
data/lib/connect_with.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'connect_with'
|
3
|
+
|
4
|
+
module ConnectWith
|
5
|
+
module Orms
|
6
|
+
module ActiveRecord
|
7
|
+
def connect_with(config_group, options = {})
|
8
|
+
establish_connection configurations[config_group.to_s][::Rails.env]
|
9
|
+
if options[:primary_key_prefix] == :table
|
10
|
+
set_primary_key { "#{self.table_name.singularize}_id" }
|
11
|
+
end
|
12
|
+
rescue
|
13
|
+
raise ::ActiveRecord::AdapterNotFound, "Could not find the #{::Rails.env} configuration for group \"#{config_group.to_s}\""
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'connect_with'
|
2
|
+
|
3
|
+
module ConnectWith
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer "connect_with.active_record" do |app|
|
6
|
+
if defined? ::ActiveRecord
|
7
|
+
require 'connect_with/orms/active_record'
|
8
|
+
ActiveRecord::Base.send(:extend, ConnectWith::Orms::ActiveRecord)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveRecord::Base, "methods" do
|
4
|
+
before do
|
5
|
+
ActiveRecord::Base.send(:extend, ConnectWith::Orms::ActiveRecord)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should have configurtions" do
|
9
|
+
ActiveRecord::Base.configurations.should_not == {}
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should have connect with method" do
|
13
|
+
ActiveRecord::Base.should respond_to(:connect_with)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe ConnectWith::Orms::ActiveRecord, "exception" do
|
18
|
+
it "should raise an error if connection group not in database.yml" do
|
19
|
+
lambda{ActiveRecord::Base.connect_with(:fial)}.should raise_error(::ActiveRecord::AdapterNotFound)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe Customer do
|
24
|
+
before do
|
25
|
+
ActiveRecord::Base.send(:extend, ConnectWith::Orms::ActiveRecord)
|
26
|
+
@connection = Customer.establish_connection
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should be connected to the alternate database" do
|
30
|
+
Customer.connection.current_database.should == "ninja_one"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe Order do
|
35
|
+
before do
|
36
|
+
ActiveRecord::Base.send(:extend, ConnectWith::Orms::ActiveRecord)
|
37
|
+
@connection = Order.send(:connect_with, :other)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should be connected to the default database" do
|
41
|
+
Order.connection.current_database.should == "ninja_two"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
development: &development
|
2
|
+
database: ninja_one
|
3
|
+
host: localhost
|
4
|
+
adapter: postgresql
|
5
|
+
username: rails
|
6
|
+
password: rails
|
7
|
+
|
8
|
+
test:
|
9
|
+
<<: *development
|
10
|
+
|
11
|
+
other:
|
12
|
+
development: &development
|
13
|
+
database: ninja_two
|
14
|
+
host: localhost
|
15
|
+
adapter: postgresql
|
16
|
+
username: rails
|
17
|
+
password: rails
|
18
|
+
|
19
|
+
test:
|
20
|
+
<<: *development
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rspec'
|
4
|
+
|
5
|
+
require 'rails'
|
6
|
+
require 'active_record'
|
7
|
+
require 'connect_with/orms/active_record'
|
8
|
+
|
9
|
+
require './spec/fixtures/models'
|
10
|
+
|
11
|
+
FileUtils.mkdir_p "#{Dir.pwd}/tmp"
|
12
|
+
ActiveRecord::Base.logger = Logger.new(StringIO.new)
|
13
|
+
ActiveRecord::Base.configurations = YAML.load_file(File.join("spec", "fixtures", "database.yml"))
|
14
|
+
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: connect_with
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Herring
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2010-09-01 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &8242320 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.0.0.beta19
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *8242320
|
25
|
+
description: Handle multiple Database conenctions with ActiveRecord Models
|
26
|
+
email: chris.herring.iphone@gmail.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- .autotest
|
32
|
+
- .gitignore
|
33
|
+
- MIT-License
|
34
|
+
- Manifest
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- lib/connect_with.rb
|
38
|
+
- lib/connect_with/orms/active_record.rb
|
39
|
+
- lib/connect_with/railtie.rb
|
40
|
+
- spec/connect_with_spec.rb
|
41
|
+
- spec/fixtures/database.yml
|
42
|
+
- spec/fixtures/models.rb
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
homepage:
|
45
|
+
licenses: []
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.10
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Handle multiple Database conenctions with ActiveRecord Models
|
68
|
+
test_files: []
|