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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dc7c3abea6d256318fb880272c6865135880642f
4
- data.tar.gz: fb016cffae231550d57e5343ea9b0658ede0ca5f
3
+ metadata.gz: 94b6186c75da0ee9ffc116cd0dd921b11f489124
4
+ data.tar.gz: c33db9d98b3797824f147aaee44e52e0455d7bb2
5
5
  SHA512:
6
- metadata.gz: b4dcfa4473e9ed0a939880e9429b83dd9cd3d7a45ed1777dae38e0ab258ac7625cedde86d2881a29f5725e629ddff1da7a6e292d303a004702d0e9da862dbbc0
7
- data.tar.gz: 3d687c3440dc7422b201ca1f146401117cfe1ac53d4bee83e718a6b3b9754a713e84ed939ee8b38c796370db9c3236419e3e51e548d1c119889d4920b1fd5527
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. The idea of this gem to transparently make Rails facilities to work with different domains.
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, :type => String
12
- field :parent, :type => String
13
- field :alias, :type => Boolean
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, :class_name => self.class
30
+ belongs_to :parent, class_name: self.class
29
31
  validates_associated :site
30
32
  end
31
33
 
32
- validates :name, :presence => true,
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
- t.string :name
5
- t.integer :site_id
6
- t.integer :parent_id
7
- t.boolean :alias
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
- t.timestamps
9
+ t.timestamps
10
10
  end
11
11
 
12
- add_index :site_framework_domains, :name, :unique => true
12
+ add_index :site_framework_domains, :name, unique: true
13
13
  end
14
14
  end
@@ -0,0 +1,3 @@
1
+ class ActionDispatch::Routing::Mapper
2
+ include SiteFramework::Routing::Sites
3
+ end
@@ -6,6 +6,5 @@ module SiteFramework
6
6
  belongs_to :domain
7
7
  default_scope -> { where(:domain => Rails.application.domain) }
8
8
  end
9
-
10
9
  end
11
10
  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
@@ -0,0 +1,9 @@
1
+ module SiteFramework::Routing
2
+ module Sites
3
+ def sites(&block)
4
+ constraints(SiteFramework::Routing::SiteConstraint.new) do
5
+ block.call
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module SiteFramework
2
- VERSION = "1.0.2"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -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 "site_framework/engine"
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: 1.0.2
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: 2014-06-16 00:00:00.000000000 Z
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