allinone 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +2 -0
- data/allinone.gemspec +24 -0
- data/lib/allinone.rb +16 -0
- data/lib/allinone/core.rb +27 -0
- data/lib/allinone/site_selector.rb +36 -0
- data/lib/allinone/version.rb +3 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8e958af02e3b9ebd732fe56d160ea6dad911f868
|
4
|
+
data.tar.gz: c373d4e6ed392a2fd0dbb795659b00f894b7b69d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 85d5c3a8b22d3021b2982b13006eb5f9966d6ff874594a99e33ce4d4c42469875a16814b86b78dc7c17d16dac3eaaf3ff1e5db48243c20be653a20aeab176242
|
7
|
+
data.tar.gz: 26f85e7f826308167e9c5f624a08e28a14696aa3dc82867245b6a2ec37ba87d4dfc3418307a54755060b22a843d93bfe8344b0f90fe45ef6dbe0a21d4ca506e1
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Unni
|
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,60 @@
|
|
1
|
+
# Allinone
|
2
|
+
|
3
|
+
A gem that adds multitenancy to a Rails app. Install the gem and your app is already capable of handling multiple domains, with a `current_site` method to help you scope data for a particular site. See Usage section for more details.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'allinone'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install allinone
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
There is sample application available [here](https://github.com/Liberlabs/sample_multitenant_app).
|
22
|
+
|
23
|
+
* Add a migration to create sites and domains tables in your app as shown [here](https://github.com/Liberlabs/sample_multitenant_app/blob/master/db/migrate/20150115080301_create_sites_and_domains.rb).
|
24
|
+
|
25
|
+
* Add Site and Domain models in your app as shown [here](https://github.com/Liberlabs/sample_multitenant_app/blob/master/app/models/site.rb) and [here](https://github.com/Liberlabs/sample_multitenant_app/blob/master/app/models/domain.rb).
|
26
|
+
|
27
|
+
* Run the migrations
|
28
|
+
|
29
|
+
```rake db:migrate```
|
30
|
+
|
31
|
+
* Add these 2 lines into your ApplicationController, towards the top.
|
32
|
+
|
33
|
+
```
|
34
|
+
include Allinone::Core
|
35
|
+
act_as_allinone
|
36
|
+
```
|
37
|
+
|
38
|
+
* From the rails console, create a site and at least one domain.
|
39
|
+
|
40
|
+
```
|
41
|
+
s = Site.create name: 'My Site'
|
42
|
+
s.domains.create name: 'localhost'
|
43
|
+
```
|
44
|
+
|
45
|
+
You can create more sites and domains if you like. You can also create multiple domains for the same site. Make sure you point the domains you create to localhost using entries in /etc/hosts (on development machines) or using DNS on production servers.
|
46
|
+
|
47
|
+
* Start the rails server and access the site.
|
48
|
+
|
49
|
+
```
|
50
|
+
rails s
|
51
|
+
In the browser, go to http://localhost:3000
|
52
|
+
```
|
53
|
+
|
54
|
+
`current_site` method will be accessible from your controllers and views. It will return the site object corresponding to the domain used to access the app. You can use current_site.id to scope the data for a particular site wherever you have to store/access data.
|
55
|
+
|
56
|
+
## Invalid Domain Handing
|
57
|
+
|
58
|
+
If an invalid domain (a domain that is not present in the domains table) is used to access the site, a 'Site not found' page will be displayed by default.
|
59
|
+
|
60
|
+
This behaviour can be customized by overriding `site_not_found_handler` method in the application controller as shown [here] (https://github.com/Liberlabs/sample_multitenant_app/blob/master/app/controllers/application_controller.rb#L10).
|
data/Rakefile
ADDED
data/allinone.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'allinone/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "allinone"
|
8
|
+
spec.version = Allinone::VERSION
|
9
|
+
spec.authors = ["Unni"]
|
10
|
+
spec.email = ["unni.tallman@gmail.com"]
|
11
|
+
spec.summary = %q{Makes a Rails app multitenant}
|
12
|
+
spec.description = %q{Makes it possible to serve multiple sites from the same app}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_dependency "request_store"
|
24
|
+
end
|
data/lib/allinone.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "allinone/version"
|
2
|
+
require "allinone/core"
|
3
|
+
require "allinone/site_selector"
|
4
|
+
require "request_store"
|
5
|
+
|
6
|
+
module Allinone
|
7
|
+
module Core
|
8
|
+
module ClassMethods
|
9
|
+
def act_as_allinone
|
10
|
+
prepend_before_filter :set_current_site
|
11
|
+
helper_method :current_site
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Allinone
|
2
|
+
module Core
|
3
|
+
def self.included(base)
|
4
|
+
base.extend Allinone::Core::ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
def current_site
|
8
|
+
RequestStore.store[:current_site]
|
9
|
+
end
|
10
|
+
|
11
|
+
def set_current_site
|
12
|
+
if Rails.env.test?
|
13
|
+
current_site = "" #set a default, passed from the app controller
|
14
|
+
else
|
15
|
+
current_site = Allinone::SiteSelector.new(request.host).fetch
|
16
|
+
end
|
17
|
+
|
18
|
+
site_not_found_handler unless current_site
|
19
|
+
|
20
|
+
RequestStore.store[:current_site] = current_site
|
21
|
+
end
|
22
|
+
|
23
|
+
def site_not_found_handler
|
24
|
+
render :text => 'Site not Found'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Allinone
|
2
|
+
class SiteSelector
|
3
|
+
|
4
|
+
def initialize(request_host)
|
5
|
+
@request_host = request_host.downcase
|
6
|
+
@@domains_map_last_build_at ||= Time.at(0).to_datetime.utc
|
7
|
+
setup_domains_map
|
8
|
+
end
|
9
|
+
|
10
|
+
def fetch
|
11
|
+
site_id = @@domains_map[@request_host]
|
12
|
+
Site.where(id: site_id).first
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def setup_domains_map
|
17
|
+
if domains_table_last_updated_at > @@domains_map_last_build_at
|
18
|
+
build_domains_map
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def build_domains_map
|
23
|
+
@@domains_map = domains_map
|
24
|
+
@@domains_map_last_build_at = 1.second.ago.to_datetime.utc
|
25
|
+
end
|
26
|
+
|
27
|
+
def domains_map
|
28
|
+
Hash[*Domain.select([:name,:site_id]).all.collect{|p| [p.name.downcase, p.site_id]}.flatten]
|
29
|
+
end
|
30
|
+
|
31
|
+
def domains_table_last_updated_at
|
32
|
+
Domain.order('updated_at').last.updated_at
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: allinone
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Unni
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: request_store
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Makes it possible to serve multiple sites from the same app
|
56
|
+
email:
|
57
|
+
- unni.tallman@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- allinone.gemspec
|
68
|
+
- lib/allinone.rb
|
69
|
+
- lib/allinone/core.rb
|
70
|
+
- lib/allinone/site_selector.rb
|
71
|
+
- lib/allinone/version.rb
|
72
|
+
homepage: ''
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.2.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Makes a Rails app multitenant
|
96
|
+
test_files: []
|