site_framework 1.0.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +94 -1
- data/app/models/site_framework/domain.rb +13 -6
- data/db/migrate/20140118200201_create_site_framework_domains.rb +6 -6
- data/lib/site_framework/action_dispatch.rb +3 -0
- data/lib/site_framework/active_record/concerns.rb +0 -1
- data/lib/site_framework/middleware.rb +0 -24
- data/lib/site_framework/routing/site_constraint.rb +81 -0
- data/lib/site_framework/routing/sites.rb +9 -0
- data/lib/site_framework/version.rb +1 -1
- data/lib/site_framework.rb +9 -3
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94b6186c75da0ee9ffc116cd0dd921b11f489124
|
4
|
+
data.tar.gz: c33db9d98b3797824f147aaee44e52e0455d7bb2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53407175606216896d048ae7e205c77849bf854296e52e72653dccd3eaf3a22953988dd1f935f7cb5309ad6dd7825105cf73117ca38bb1e430a3748a1c37d865
|
7
|
+
data.tar.gz: b77ad68f0ddcd15fe4542a4fb9ff60345faaa9ab88528711f6d534d2b9f82f25130fa0d7171bcdfa3e3faf04212981c42d1872ef66cc0c3ed8700614e2f72afe
|
data/README.md
CHANGED
@@ -1,3 +1,96 @@
|
|
1
1
|
# SiteFramework - [![Gem Version](https://badge.fury.io/rb/site_framework.png)](http://badge.fury.io/rb/site_framework)
|
2
2
|
|
3
|
-
A site framework for Ruby on Rails web framework inspired by Django site fremework.
|
3
|
+
A site framework for Ruby on Rails web framework inspired by Django site fremework.
|
4
|
+
The idea of this gem to transparently make Rails apps to work with different domains.
|
5
|
+
|
6
|
+
**Warning**: This gem is still on development. I'll be happy to have your feedback.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add `site_framework` to your `Gemfile`:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'site_framework'
|
14
|
+
```
|
15
|
+
|
16
|
+
and after installing your project dependencies using `bundle install` command. Install
|
17
|
+
**SiteFramework** migrations like:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
rake site_framework:install:migrations
|
21
|
+
```
|
22
|
+
|
23
|
+
That's it.
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
**SiteFramework** provides to solution to multi-site support.
|
28
|
+
|
29
|
+
In both solution you have to add a migration for your tables and
|
30
|
+
make them domain aware (ActiveRecord Only). e.g in your migration:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# Make posts table domain aware
|
34
|
+
domain_aware(:posts)
|
35
|
+
```
|
36
|
+
|
37
|
+
If you're using **Mongoid** just add a reference to **SiteFramework::Domain** in your model.
|
38
|
+
|
39
|
+
When a request arrives to the Rails application `SiteFramework` will add three different
|
40
|
+
methods to `Rails.application`.
|
41
|
+
|
42
|
+
* **domain**: An instance of `SiteFramework::Domain` model which refer to current domain of
|
43
|
+
the request
|
44
|
+
* **domain_name**: Current domain as string.
|
45
|
+
* **Site**: An instance of `SiteFramework::Site` model which refer to current site.
|
46
|
+
|
47
|
+
|
48
|
+
### A) Rack middleware:
|
49
|
+
Simply add `SiteFramework::Middleware` to your middleware stack.
|
50
|
+
|
51
|
+
### B) Constrants
|
52
|
+
Just use `sites` DSL in your `routes.rb`. e.g:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
Rails.application.routes.draw do
|
56
|
+
|
57
|
+
get 'home/index'
|
58
|
+
|
59
|
+
# All the routes defined in this section will be domain aware.
|
60
|
+
sites do
|
61
|
+
root 'home#index'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
**Personally I prefer this (B) option since it's more Railish.**
|
67
|
+
|
68
|
+
### Model Concern
|
69
|
+
**SiteFramework** provides an **ActiveSupport** concern which transparently
|
70
|
+
makes your models aware of the current **Site** and **Domain**. By includeing
|
71
|
+
`SiteFramework::DomainAware` into your model, default scope of your model will
|
72
|
+
change to return only records which belongs to current **Site**.
|
73
|
+
|
74
|
+
This way you can use external gems with your multi-site application easily.
|
75
|
+
All you have to do is to open there models and include the given concern.
|
76
|
+
|
77
|
+
Piece of cake. right?
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
1. Fork it
|
82
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
83
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
84
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
85
|
+
5. Create new Pull Request
|
86
|
+
|
87
|
+
## Credit
|
88
|
+
![Yellowen](http://www.yellowen.com/images/logo.png)
|
89
|
+
|
90
|
+
**SiteFramework** is maintained and funded by Yellowen. Whenever a code snippet is borrowed or inspired by existing code,
|
91
|
+
we try to credit the original developer/designer in our source code. Let us know if you think we have missed to do this.
|
92
|
+
|
93
|
+
|
94
|
+
# License
|
95
|
+
|
96
|
+
**SiteFramework** is Copyright © 2014-2015 Yellowen. It is free software, and may be redistributed under the terms specified in the LICENSE file.
|
@@ -4,13 +4,15 @@ module SiteFramework
|
|
4
4
|
# belongs to another **Domain**
|
5
5
|
class Domain < (defined?(ActiveRecord) ? ActiveRecord::Base : Object)
|
6
6
|
|
7
|
+
PATTERN = /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?\z/ix
|
8
|
+
|
7
9
|
if defined? Mongoid
|
8
10
|
include Mongoid::Document
|
9
11
|
include Mongoid::Timestamps
|
10
12
|
|
11
|
-
field :name,
|
12
|
-
field :parent, :
|
13
|
-
field :alias, :
|
13
|
+
field :name, type: String
|
14
|
+
field :parent, type: String, default: nil
|
15
|
+
field :alias, type: Boolean, default: false
|
14
16
|
|
15
17
|
embedded_in :site
|
16
18
|
|
@@ -25,12 +27,17 @@ module SiteFramework
|
|
25
27
|
belongs_to :site
|
26
28
|
|
27
29
|
# Self relation
|
28
|
-
belongs_to :parent, :
|
30
|
+
belongs_to :parent, class_name: self.class
|
29
31
|
validates_associated :site
|
30
32
|
end
|
31
33
|
|
32
|
-
validates
|
33
|
-
:format => { :with => /\A(?:[a-z0-9\-]+\.)+[a-z]{2,4}\z/i }
|
34
|
+
validates(:name, presence: true, format: { with: PATTERN })
|
34
35
|
validates_uniqueness_of :name
|
36
|
+
|
37
|
+
before_save :normalize_name
|
38
|
+
|
39
|
+
def normalize_name
|
40
|
+
self.name = name.downcase
|
41
|
+
end
|
35
42
|
end
|
36
43
|
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
class CreateSiteFrameworkDomains < ActiveRecord::Migration
|
2
2
|
def change
|
3
3
|
create_table :site_framework_domains do |t|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
t.string :name
|
5
|
+
t.integer :site_id
|
6
|
+
t.integer :parent_id, default: nil
|
7
|
+
t.boolean :alias, default: false
|
8
8
|
|
9
|
-
|
9
|
+
t.timestamps
|
10
10
|
end
|
11
11
|
|
12
|
-
add_index :site_framework_domains, :name, :
|
12
|
+
add_index :site_framework_domains, :name, unique: true
|
13
13
|
end
|
14
14
|
end
|
@@ -29,30 +29,6 @@ module SiteFramework
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
Rails.application.send :define_singleton_method, 'domain' do
|
33
|
-
domain = nil
|
34
|
-
if Rails.application.instance_variable_defined? '@domain'
|
35
|
-
domain = Rails.application.instance_variable_get '@domain'
|
36
|
-
if respond_to? :logger
|
37
|
-
logger.info "`domain` is defined, value #{domain}"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
if domain.nil?
|
42
|
-
# Fetch domain by calling **fetch_domain** method on
|
43
|
-
# **Rails.application**
|
44
|
-
domain_obj = fetch_domain
|
45
|
-
if respond_to? :logger
|
46
|
-
logger.debug '`domain` is nil'
|
47
|
-
logger.warn "Can't find domain object of `#{Rails.application.domain_name}`"
|
48
|
-
end
|
49
|
-
Rails.application.instance_variable_set '@domain', domain_obj
|
50
|
-
domain = domain_obj
|
51
|
-
end
|
52
|
-
|
53
|
-
domain
|
54
|
-
end
|
55
|
-
|
56
32
|
Rails.application.send :define_singleton_method, 'site' do
|
57
33
|
site = nil
|
58
34
|
unless Rails.application.domain.nil?
|
@@ -0,0 +1,81 @@
|
|
1
|
+
class SiteFramework::Routing::SiteConstraint
|
2
|
+
|
3
|
+
attr_reader :logger
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@logger = Rails.logger
|
7
|
+
end
|
8
|
+
|
9
|
+
def domains
|
10
|
+
SiteFramework::Domain.select('DISTINCT name').map(&:name).uniq
|
11
|
+
end
|
12
|
+
|
13
|
+
def matches?(request)
|
14
|
+
if domains.include? request.host
|
15
|
+
define_domain_name request.host
|
16
|
+
define_fetch_domain_method request.host
|
17
|
+
define_domain
|
18
|
+
define_site
|
19
|
+
true
|
20
|
+
else
|
21
|
+
|
22
|
+
logger.info("Domain name '#{request.host}' does not match with any exist domains")
|
23
|
+
false
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def define_domain_name(domain)
|
31
|
+
Rails.application.send :define_singleton_method, 'domain_name' do
|
32
|
+
domain
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def define_domain
|
37
|
+
# TODO: Find a better way to do this.
|
38
|
+
Rails.application.send :define_singleton_method, 'domain' do
|
39
|
+
domain_ = nil
|
40
|
+
if Rails.application.instance_variable_defined? '@domain'
|
41
|
+
domain_ = Rails.application.instance_variable_get '@domain'
|
42
|
+
if respond_to? :logger
|
43
|
+
logger.info "`domain` is defined, value #{domain}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
if domain_.nil?
|
48
|
+
# Fetch domain by calling **fetch_domain** method on
|
49
|
+
# **Rails.application**
|
50
|
+
domain_obj = fetch_domain
|
51
|
+
if respond_to? :logger
|
52
|
+
logger.debug '`domain` is nil'
|
53
|
+
logger.warn "Can't find domain object of `#{Rails.application.domain_name}`"
|
54
|
+
end
|
55
|
+
Rails.application.instance_variable_set '@domain', domain_obj
|
56
|
+
domain_ = domain_obj
|
57
|
+
end
|
58
|
+
|
59
|
+
domain_
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
def define_site
|
65
|
+
Rails.application.send :define_singleton_method, 'site' do
|
66
|
+
site = nil
|
67
|
+
site = Rails.application.domain.site unless Rails.application.domain.nil?
|
68
|
+
site
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def define_fetch_domain_method(domain)
|
73
|
+
Rails.application.send :define_singleton_method, 'fetch_domain' do
|
74
|
+
if defined? ActiveRecord
|
75
|
+
Domain.find_by(nam: domain)
|
76
|
+
elsif defined? Mongoid
|
77
|
+
Site.where('domains.name' => domain).domains.first
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/lib/site_framework.rb
CHANGED
@@ -1,10 +1,18 @@
|
|
1
1
|
# Main module of `site_framework` gem
|
2
2
|
module SiteFramework
|
3
|
+
autoload :Middleware, 'site_framework/middleware'
|
4
|
+
|
5
|
+
module Routing
|
6
|
+
end
|
3
7
|
end
|
4
8
|
|
5
|
-
require
|
9
|
+
require 'site_framework/engine'
|
6
10
|
require 'site_framework/railtie'
|
7
11
|
require 'site_framework/orm'
|
12
|
+
require 'site_framework/routing/site_constraint'
|
13
|
+
require 'site_framework/routing/sites'
|
14
|
+
require 'site_framework/action_dispatch'
|
15
|
+
|
8
16
|
case SiteFramework::ORM.current_orm
|
9
17
|
when 'active_record'
|
10
18
|
require 'site_framework/active_record/migration'
|
@@ -12,5 +20,3 @@ when 'active_record'
|
|
12
20
|
when 'mongoid'
|
13
21
|
require 'site_framework/mongoid/concerns'
|
14
22
|
end
|
15
|
-
|
16
|
-
require 'site_framework/middleware'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: site_framework
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sameer Rahmani
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- db/migrate/20140118200201_create_site_framework_domains.rb
|
71
71
|
- db/migrate/20140303141448_create_site_framework_sites.rb
|
72
72
|
- lib/site_framework.rb
|
73
|
+
- lib/site_framework/action_dispatch.rb
|
73
74
|
- lib/site_framework/active_record/concerns.rb
|
74
75
|
- lib/site_framework/active_record/migration.rb
|
75
76
|
- lib/site_framework/engine.rb
|
@@ -77,6 +78,8 @@ files:
|
|
77
78
|
- lib/site_framework/mongoid/concerns.rb
|
78
79
|
- lib/site_framework/orm.rb
|
79
80
|
- lib/site_framework/railtie.rb
|
81
|
+
- lib/site_framework/routing/site_constraint.rb
|
82
|
+
- lib/site_framework/routing/sites.rb
|
80
83
|
- lib/site_framework/version.rb
|
81
84
|
- lib/tasks/site_framework_tasks.rake
|
82
85
|
- spec/dummy/README.rdoc
|