site_framework 2.0.0 → 3.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94b6186c75da0ee9ffc116cd0dd921b11f489124
4
- data.tar.gz: c33db9d98b3797824f147aaee44e52e0455d7bb2
3
+ metadata.gz: cc343768092e51720768dc4b7a0d898bd9f70d56
4
+ data.tar.gz: 01aa8c586ca705123082dd960fa6532dc6eb467c
5
5
  SHA512:
6
- metadata.gz: 53407175606216896d048ae7e205c77849bf854296e52e72653dccd3eaf3a22953988dd1f935f7cb5309ad6dd7825105cf73117ca38bb1e430a3748a1c37d865
7
- data.tar.gz: b77ad68f0ddcd15fe4542a4fb9ff60345faaa9ab88528711f6d534d2b9f82f25130fa0d7171bcdfa3e3faf04212981c42d1872ef66cc0c3ed8700614e2f72afe
6
+ metadata.gz: ae5fa48b1d33a01beffe3566a4ba77383ffaea071f09da5b0508fc37f17a499022b6960098c8aa0e76913086532a6744f23d9a6f616d1322a0139741bf8217b0
7
+ data.tar.gz: 806d3320c628af9a50e672138ca0a378c9eda43db5858156b81edba151d3c969a5b886477a8bfab1229287ce594601156f5f17c0b314bae18c5e9d46670fe415
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # SiteFramework - [![Gem Version](https://badge.fury.io/rb/site_framework.png)](http://badge.fury.io/rb/site_framework)
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
 
@@ -27,7 +27,7 @@ module SiteFramework
27
27
  belongs_to :site
28
28
 
29
29
  # Self relation
30
- belongs_to :parent, class_name: self.class
30
+ belongs_to :parent, class_name: self.class.to_s
31
31
  validates_associated :site
32
32
  end
33
33
 
@@ -0,0 +1,7 @@
1
+ # This singleton class act as an temp storage for the
2
+ # current site and domain
3
+ class SiteFramework::CurrentState
4
+ include Singleton
5
+
6
+ attr_accessor :site, :domain, :domain_name
7
+ end
@@ -9,5 +9,12 @@ module SiteFramework
9
9
  g.test_framework :rspec
10
10
  end
11
11
 
12
+ @@default_domains = ['localhost']
13
+
14
+ mattr_accessor :default_domains
15
+
16
+ def self.setup
17
+ yield self
18
+ end
12
19
  end
13
20
  end
@@ -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 domains
10
- SiteFramework::Domain.select('DISTINCT name').map(&:name).uniq
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
- if domains.include? request.host
15
- define_domain_name request.host
16
- define_fetch_domain_method request.host
17
- define_domain
18
- define_site
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 define_domain_name(domain)
31
- Rails.application.send :define_singleton_method, 'domain_name' do
32
- domain
33
- end
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 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
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(&block)
4
- constraints(SiteFramework::Routing::SiteConstraint.new) do
5
- block.call
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
@@ -0,0 +1,3 @@
1
+ require 'site_framework/routing/site_constraint'
2
+ require 'site_framework/routing/default_constraint'
3
+ require 'site_framework/routing/sites'
@@ -1,3 +1,3 @@
1
1
  module SiteFramework
2
- VERSION = "2.0.0"
2
+ VERSION = '3.1.0'
3
3
  end
@@ -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/site_constraint'
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: 2.0.0
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-05-18 00:00:00.000000000 Z
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