site_framework 2.0.0 → 3.1.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 +19 -2
- data/app/models/site_framework/domain.rb +1 -1
- data/lib/site_framework/current_state.rb +7 -0
- data/lib/site_framework/engine.rb +7 -0
- data/lib/site_framework/helpers.rb +35 -0
- data/lib/site_framework/routing/default_constraint.rb +29 -0
- data/lib/site_framework/routing/site_constraint.rb +24 -58
- data/lib/site_framework/routing/sites.rb +11 -3
- data/lib/site_framework/routing.rb +3 -0
- data/lib/site_framework/version.rb +1 -1
- data/lib/site_framework.rb +5 -2
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc343768092e51720768dc4b7a0d898bd9f70d56
|
4
|
+
data.tar.gz: 01aa8c586ca705123082dd960fa6532dc6eb467c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae5fa48b1d33a01beffe3566a4ba77383ffaea071f09da5b0508fc37f17a499022b6960098c8aa0e76913086532a6744f23d9a6f616d1322a0139741bf8217b0
|
7
|
+
data.tar.gz: 806d3320c628af9a50e672138ca0a378c9eda43db5858156b81edba151d3c969a5b886477a8bfab1229287ce594601156f5f17c0b314bae18c5e9d46670fe415
|
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
# SiteFramework
|
1
|
+
# SiteFramework
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/site_framework.png)](http://badge.fury.io/rb/site_framework)
|
3
|
+
[![Downloads](https://img.shields.io/gem/dt/site_framework.svg)](http://rubygems/gems/site_framework)
|
2
4
|
|
3
5
|
A site framework for Ruby on Rails web framework inspired by Django site fremework.
|
4
6
|
The idea of this gem to transparently make Rails apps to work with different domains.
|
@@ -54,12 +56,27 @@ Just use `sites` DSL in your `routes.rb`. e.g:
|
|
54
56
|
```ruby
|
55
57
|
Rails.application.routes.draw do
|
56
58
|
|
59
|
+
# Share routes
|
57
60
|
get 'home/index'
|
58
61
|
|
59
62
|
# All the routes defined in this section will be domain aware.
|
60
|
-
sites do
|
63
|
+
sites(self) do
|
61
64
|
root 'home#index'
|
62
65
|
end
|
66
|
+
|
67
|
+
default_site(self) do
|
68
|
+
# routs for default site
|
69
|
+
end
|
70
|
+
end
|
71
|
+
```
|
72
|
+
Note: You can provide default domains for **SiteFramework** via an
|
73
|
+
initializer like this:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
SiteFramework.setup do |config|
|
77
|
+
|
78
|
+
config.default_domains = ['localhost', 'example.com']
|
79
|
+
|
63
80
|
end
|
64
81
|
```
|
65
82
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'site_framework/current_state'
|
2
|
+
|
3
|
+
module SiteFramework
|
4
|
+
|
5
|
+
# Return the current site, domain, domain_name
|
6
|
+
# if it wasn't the default site
|
7
|
+
module DummyHelper
|
8
|
+
|
9
|
+
def current_site
|
10
|
+
SiteFramework::CurrentState.instance.site
|
11
|
+
end
|
12
|
+
|
13
|
+
def current_domain
|
14
|
+
SiteFramework::CurrentState.instance.domain
|
15
|
+
end
|
16
|
+
|
17
|
+
def current_domain_name
|
18
|
+
SiteFramework::CurrentState.instance.domain_name
|
19
|
+
end
|
20
|
+
|
21
|
+
def default_site?
|
22
|
+
return true if SiteFramework::CurrentState.instance.site.nil?
|
23
|
+
false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module Helpers
|
28
|
+
extend ActiveSupport::Concern
|
29
|
+
include SiteFramework::DummyHelper
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
include SiteFramework::DummyHelper
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class SiteFramework::Routing::DefaultConstraint
|
2
|
+
|
3
|
+
attr_reader :logger
|
4
|
+
|
5
|
+
def initialize(mapper, defaults = nil)
|
6
|
+
@logger = Rails.logger
|
7
|
+
@mapper = mapper
|
8
|
+
@default_domains = defaults || SiteFramework::Engine.default_domains
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
def matches?(request)
|
13
|
+
@domain_name = request.host
|
14
|
+
|
15
|
+
if @default_domains.include? @domain_name
|
16
|
+
logger.debug("Loading default site configuration")
|
17
|
+
|
18
|
+
SiteFramework::CurrentState.instance.domain_name = nil
|
19
|
+
SiteFramework::CurrentState.instance.domain = nil
|
20
|
+
SiteFramework::CurrentState.instance.site = nil
|
21
|
+
|
22
|
+
true
|
23
|
+
else
|
24
|
+
logger.info("Domain name '#{request.host}' does not match with any exist domains")
|
25
|
+
false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -2,80 +2,46 @@ class SiteFramework::Routing::SiteConstraint
|
|
2
2
|
|
3
3
|
attr_reader :logger
|
4
4
|
|
5
|
-
def initialize
|
5
|
+
def initialize(mapper)
|
6
6
|
@logger = Rails.logger
|
7
|
+
@mapper = mapper
|
7
8
|
end
|
8
9
|
|
9
|
-
def
|
10
|
-
|
10
|
+
def domain(name)
|
11
|
+
@domain ||= if defined? ActiveRecord
|
12
|
+
SiteFramework::Domain.find_by(name: name)
|
13
|
+
elsif defined? Mongoid
|
14
|
+
SiteFramework::Site.where('domains.name' => name).domains.first
|
15
|
+
else
|
16
|
+
nil
|
17
|
+
end
|
11
18
|
end
|
12
19
|
|
13
20
|
def matches?(request)
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
21
|
+
@domain_name = request.host
|
22
|
+
|
23
|
+
if domain_obj = domain(request.host)
|
24
|
+
logger.debug("''#{@domain_name}' matched.")
|
25
|
+
setup(domain_obj)
|
26
|
+
initialize_site_default_state
|
27
|
+
|
19
28
|
true
|
20
29
|
else
|
21
|
-
|
22
30
|
logger.info("Domain name '#{request.host}' does not match with any exist domains")
|
23
31
|
false
|
24
32
|
end
|
25
|
-
|
26
33
|
end
|
27
34
|
|
28
35
|
private
|
29
36
|
|
30
|
-
def
|
31
|
-
|
32
|
-
domain
|
33
|
-
|
37
|
+
def setup(domain)
|
38
|
+
SiteFramework::CurrentState.instance.domain_name = domain.name
|
39
|
+
SiteFramework::CurrentState.instance.domain = domain
|
40
|
+
SiteFramework::CurrentState.instance.site = domain.site
|
41
|
+
@site = domain.site
|
34
42
|
end
|
35
43
|
|
36
|
-
def
|
37
|
-
|
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
|
44
|
+
def initialize_site_default_state
|
45
|
+
@site.try(:before_dispatch, @mapper)
|
80
46
|
end
|
81
47
|
end
|
@@ -1,8 +1,16 @@
|
|
1
1
|
module SiteFramework::Routing
|
2
2
|
module Sites
|
3
|
-
def sites(
|
4
|
-
constraints(SiteFramework::Routing::SiteConstraint.new) do
|
5
|
-
|
3
|
+
def sites(mapper)
|
4
|
+
constraints(SiteFramework::Routing::SiteConstraint.new(mapper)) do
|
5
|
+
yield
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def default_site(mapper, defaults)
|
10
|
+
constraint = SiteFramework::Routing::DefaultConstraint.new(mapper,
|
11
|
+
defaults)
|
12
|
+
constraints(constraint) do
|
13
|
+
yield
|
6
14
|
end
|
7
15
|
end
|
8
16
|
end
|
data/lib/site_framework.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
+
require 'site_framework/current_state'
|
2
|
+
require 'site_framework/helpers'
|
3
|
+
|
1
4
|
# Main module of `site_framework` gem
|
2
5
|
module SiteFramework
|
3
6
|
autoload :Middleware, 'site_framework/middleware'
|
7
|
+
include SiteFramework::Helpers
|
4
8
|
|
5
9
|
module Routing
|
6
10
|
end
|
@@ -9,8 +13,7 @@ end
|
|
9
13
|
require 'site_framework/engine'
|
10
14
|
require 'site_framework/railtie'
|
11
15
|
require 'site_framework/orm'
|
12
|
-
require 'site_framework/routing
|
13
|
-
require 'site_framework/routing/sites'
|
16
|
+
require 'site_framework/routing'
|
14
17
|
require 'site_framework/action_dispatch'
|
15
18
|
|
16
19
|
case SiteFramework::ORM.current_orm
|
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: 3.1.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: 2015-
|
11
|
+
date: 2015-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -73,11 +73,15 @@ files:
|
|
73
73
|
- lib/site_framework/action_dispatch.rb
|
74
74
|
- lib/site_framework/active_record/concerns.rb
|
75
75
|
- lib/site_framework/active_record/migration.rb
|
76
|
+
- lib/site_framework/current_state.rb
|
76
77
|
- lib/site_framework/engine.rb
|
78
|
+
- lib/site_framework/helpers.rb
|
77
79
|
- lib/site_framework/middleware.rb
|
78
80
|
- lib/site_framework/mongoid/concerns.rb
|
79
81
|
- lib/site_framework/orm.rb
|
80
82
|
- lib/site_framework/railtie.rb
|
83
|
+
- lib/site_framework/routing.rb
|
84
|
+
- lib/site_framework/routing/default_constraint.rb
|
81
85
|
- lib/site_framework/routing/site_constraint.rb
|
82
86
|
- lib/site_framework/routing/sites.rb
|
83
87
|
- lib/site_framework/version.rb
|