rails_multisite 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rails_multisite might be problematic. Click here for more details.

checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e45ef95202698ebf603b6442abf02eba8f6a25e2
4
+ data.tar.gz: c47de25e43b5a8c01d8a3643bcff11592075dc4c
5
+ SHA512:
6
+ metadata.gz: 87c49b6fdd43d33155168227cf94949d8e114a6b440bf0c53e3233e19c1cd78f42e7057c17d2752920225b050b2effc4b7cf29e2715f452283392bcbf1aca0cd
7
+ data.tar.gz: 4ef167a5687c24657d69a1744a1d9c18d07593f7fc1546bca98bce24996056fa2b92abe58c843b4070b6e6af95464ea0a2cb0574ed6cff17eeda914632d236e6
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Sam Saffron
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # RailsMultisite
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rails_multisite'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rails_multisite
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,168 @@
1
+ module RailsMultisite
2
+ class ConnectionManagement
3
+ CONFIG_FILE = 'config/multisite.yml'
4
+
5
+ def self.rails4?
6
+ !!(Rails.version =~ /^4/)
7
+ end
8
+
9
+ def self.establish_connection(opts)
10
+ if opts[:db] == "default" && (!defined?(@@default_spec) || !@@default_spec)
11
+ # don't do anything .. handled implicitly
12
+ else
13
+ spec = connection_spec(opts) || @@default_spec
14
+ handler = nil
15
+ if spec != @@default_spec
16
+ handler = @@connection_handlers[spec]
17
+ unless handler
18
+ handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
19
+ if rails4?
20
+ handler.establish_connection(ActiveRecord::Base, spec)
21
+ end
22
+ @@connection_handlers[spec] = handler
23
+ end
24
+ else
25
+ handler = @@default_connection_handler
26
+ if rails4? && !@@established_default
27
+ handler.establish_connection(ActiveRecord::Base, spec)
28
+ @@established_default = true
29
+ end
30
+ end
31
+
32
+ ActiveRecord::Base.connection_handler = handler
33
+
34
+ unless rails4?
35
+ handler.establish_connection("ActiveRecord::Base", spec)
36
+ end
37
+ end
38
+ end
39
+
40
+ def self.each_connection
41
+ old = current_db
42
+ connected = ActiveRecord::Base.connection_pool.connected?
43
+ all_dbs.each do |db|
44
+ establish_connection(:db => db)
45
+ yield db
46
+ ActiveRecord::Base.connection_handler.clear_active_connections!
47
+ end
48
+ establish_connection(:db => old)
49
+ ActiveRecord::Base.connection_handler.clear_active_connections! unless connected
50
+ end
51
+
52
+ def self.all_dbs
53
+ ["default"] +
54
+ if defined?(@@db_spec_cache) && @@db_spec_cache
55
+ @@db_spec_cache.keys.to_a
56
+ else
57
+ []
58
+ end
59
+ end
60
+
61
+ def self.current_db
62
+ db = ActiveRecord::Base.connection_pool.spec.config[:db_key] || "default"
63
+ end
64
+
65
+ def self.config_filename=(config_filename)
66
+ @@config_filename = config_filename
67
+ end
68
+
69
+ def self.config_filename
70
+ @@config_filename ||= File.absolute_path(Rails.root.to_s + "/" + RailsMultisite::ConnectionManagement::CONFIG_FILE)
71
+ end
72
+
73
+ def self.current_hostname
74
+ config = ActiveRecord::Base.connection_pool.spec.config
75
+ config[:host_names].nil? ? config[:host] : config[:host_names].first
76
+ end
77
+
78
+
79
+ def self.clear_settings!
80
+ @@db_spec_cache = nil
81
+ @@host_spec_cache = nil
82
+ @@default_spec = nil
83
+ end
84
+
85
+ def self.load_settings!
86
+ spec_klass = rails4? ? ActiveRecord::ConnectionAdapters::ConnectionSpecification : ActiveRecord::Base::ConnectionSpecification
87
+ configs = YAML::load(File.open(self.config_filename))
88
+ configs.each do |k,v|
89
+ raise ArgumentError.new("Please do not name any db default!") if k == "default"
90
+ v[:db_key] = k
91
+ end
92
+
93
+ @@db_spec_cache = Hash[*configs.map do |k, data|
94
+ [k, spec_klass::Resolver.new(k, configs).spec]
95
+ end.flatten]
96
+
97
+ @@host_spec_cache = {}
98
+ configs.each do |k,v|
99
+ next unless v["host_names"]
100
+ v["host_names"].each do |host|
101
+ @@host_spec_cache[host] = @@db_spec_cache[k]
102
+ end
103
+ end
104
+
105
+ @@default_spec = spec_klass::Resolver.new(Rails.env, ActiveRecord::Base.configurations).spec
106
+ ActiveRecord::Base.configurations[Rails.env]["host_names"].each do |host|
107
+ @@host_spec_cache[host] = @@default_spec
108
+ end
109
+
110
+ @@default_connection_handler = ActiveRecord::Base.connection_handler
111
+
112
+ # inject our connection_handler pool
113
+ # WARNING MONKEY PATCH
114
+ #
115
+ # see: https://github.com/rails/rails/issues/8344#issuecomment-10800848
116
+ if ActiveRecord::VERSION::MAJOR == 3
117
+ ActiveRecord::Base.send :include, NewConnectionHandler
118
+ ActiveRecord::Base.connection_handler = @@default_connection_handler
119
+ end
120
+
121
+ @@connection_handlers = {}
122
+ @@established_default = false
123
+ end
124
+
125
+ module NewConnectionHandler
126
+ def self.included(klass)
127
+ klass.class_eval do
128
+ define_singleton_method :connection_handler do
129
+ Thread.current[:connection_handler] || @connection_handler
130
+ end
131
+ define_singleton_method :connection_handler= do |handler|
132
+ @connection_handler ||= handler
133
+ Thread.current[:connection_handler] = handler
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+
140
+ def initialize(app, config = nil)
141
+ @app = app
142
+ end
143
+
144
+ def call(env)
145
+ request = Rack::Request.new(env)
146
+ begin
147
+
148
+ #TODO: add a callback so users can simply go to a domain to register it, or something
149
+ return [404, {}, ["not found"]] unless @@host_spec_cache[request.host]
150
+
151
+ ActiveRecord::Base.connection_handler.clear_active_connections!
152
+ self.class.establish_connection(:host => request['__ws'] || request.host)
153
+ @app.call(env)
154
+ ensure
155
+ ActiveRecord::Base.connection_handler.clear_active_connections!
156
+ end
157
+ end
158
+
159
+ def self.connection_spec(opts)
160
+ if opts[:host]
161
+ @@host_spec_cache[opts[:host]]
162
+ else
163
+ @@db_spec_cache[opts[:db]]
164
+ end
165
+ end
166
+
167
+ end
168
+ end
@@ -0,0 +1,21 @@
1
+ module RailsMultisite
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ Dir[File.join(File.dirname(__FILE__),'../tasks/*.rake')].each { |f| load f }
5
+ end
6
+
7
+ initializer "RailsMultisite.init" do |app|
8
+ if File.exists?(ConnectionManagement.config_filename)
9
+ ConnectionManagement.load_settings!
10
+ app.middleware.insert_after(ActiveRecord::ConnectionAdapters::ConnectionManagement, RailsMultisite::ConnectionManagement)
11
+ app.middleware.delete(ActiveRecord::ConnectionAdapters::ConnectionManagement)
12
+ if ENV['RAILS_DB']
13
+ ConnectionManagement.establish_connection(:db => ENV['RAILS_DB'])
14
+ end
15
+ end
16
+ end
17
+
18
+
19
+ end
20
+ end
21
+
@@ -0,0 +1,3 @@
1
+ module RailsMultisite
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require "rails_multisite/version"
2
+ require "rails_multisite/railtie"
3
+ require "rails_multisite/connection_management"
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_multisite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sam Saffron
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Multi tenancy support for Rails
14
+ email:
15
+ - sam.saffron@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/rails_multisite.rb
23
+ - lib/rails_multisite/connection_management.rb
24
+ - lib/rails_multisite/railtie.rb
25
+ - lib/rails_multisite/version.rb
26
+ homepage: ''
27
+ licenses: []
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.2.0
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Multi tenancy support for Rails
49
+ test_files: []
50
+ has_rdoc: