bradgessler-domain_slice 0.0.0 → 0.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.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :minor: 0
2
+ :minor: 1
3
3
  :patch: 0
4
4
  :major: 0
@@ -1,7 +1,63 @@
1
+ require 'rack/utils'
2
+
1
3
  module DomainSlice
2
4
  module Middleware
3
- module Static
4
- # Grabs the static assets out of the correctomundo path
5
+ class Static
6
+ FILE_METHODS = %w(GET HEAD).freeze unless defined?(FILE_METHODS)
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ @file_servers = [
11
+ ::Rack::File.new(domain_slice_public_path),
12
+ ::Rack::File.new(rails_public_path)
13
+ ]
14
+ end
15
+
16
+ def call(env)
17
+ path = env['PATH_INFO'].chomp('/')
18
+ method = env['REQUEST_METHOD']
19
+
20
+ if FILE_METHODS.include?(method)
21
+ if rack_file = file_exist?(path)
22
+ return rack_file.call(env)
23
+ else
24
+ cached_path = directory_exist?(path) ? "#{path}/index" : path
25
+ cached_path += ::ActionController::Base.page_cache_extension
26
+
27
+ if rack_file = file_exist?(cached_path)
28
+ env['PATH_INFO'] = cached_path
29
+ return rack_file.call(env)
30
+ end
31
+ end
32
+ end
33
+
34
+ @app.call(env)
35
+ end
36
+
37
+ private
38
+ def rails_public_path
39
+ File.join(RAILS_ROOT, 'public')
40
+ end
41
+
42
+ def domain_slice_public_path
43
+ File.join(RAILS_ROOT, 'domains', DomainSlice.domain, 'public')
44
+ end
45
+
46
+ # TODO [BG Mar 09] This might have to be optimized instead of a stupid check
47
+ # one, check the other arrangement
48
+ def file_exist?(path)
49
+ @file_servers.find do |file_server|
50
+ full_path = File.join(file_server.root, ::Rack::Utils.unescape(path))
51
+ File.file?(full_path) && File.readable?(full_path)
52
+ end
53
+ end
54
+
55
+ def directory_exist?(path)
56
+ @file_servers.find do |file_server|
57
+ full_path = File.join(file_server.root, ::Rack::Utils.unescape(path))
58
+ File.directory?(full_path) && File.readable?(full_path)
59
+ end
60
+ end
5
61
  end
6
62
  end
7
63
  end
@@ -1,6 +1,5 @@
1
1
  require 'rubygems'
2
2
  require 'domain_slice'
3
- require 'rack'
4
3
  require File.join(File.dirname(__FILE__), %w[middleware static])
5
4
 
6
5
  module DomainSlice
@@ -1,6 +1,47 @@
1
1
  module DomainSlice
2
2
  module Rails
3
3
  module Initializer
4
+ def self.included(base)
5
+ # Just skip this whole thing if a domain package is not specified and
6
+ # load rails as you normally would without domain packages
7
+ base.send(:extend, ClassMethods)
8
+ base.send(:include, InstanceMethods)
9
+
10
+ base.instance_eval do # Class methods
11
+ alias :run_without_domain_package :run
12
+ alias :run :run_with_domain_package
13
+ end
14
+
15
+ base.class_eval do # Instance methods
16
+ alias :load_environment_without_domain_package :load_environment
17
+ alias :load_environment :load_environment_with_domain_package
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+ def load_environment_with_domain_package(*args)
23
+ load_environment_without_domain_package(*args)
24
+ # This is copy, pasted, and modified to load domain package configs from
25
+ # http://github.com/rails/rails/blob/39ff550fa88da9a22d8c21ca872f5e4d0d83f8d4/railties/lib/initializer.rb#L365-369
26
+ config = self.send(:configuration) # Gotta tee up config for the bindings below in the evals
27
+ silence_warnings do
28
+ # If the env files don't exist, who cares? Just use the base configs
29
+ eval(IO.read(DomainSlice::Rails.root_environment_path), binding, DomainSlice::Rails.root_environment_path) if File.exists? DomainSlice::Rails.root_environment_path
30
+ eval(IO.read(DomainSlice::Rails.environment_path), binding, DomainSlice::Rails.environment_path) if File.exists? DomainSlice::Rails.environment_path
31
+ end
32
+ end
33
+ end
34
+
35
+ module ClassMethods
36
+ # TODO [BG Mar 09] Watch Rails::Initializer on github to monitor changes and pontential breakage
37
+ # of function arity in future versions of rails.
38
+ def run_with_domain_package(command = :process, configuration=::Rails::Configuration.new, &block)
39
+ run_without_domain_package(command, DomainSlice::Rails.configure(configuration), &block)
40
+ DomainSlice::Rails.init!
41
+ end
42
+ end
4
43
  end
5
44
  end
6
- end
45
+ end
46
+
47
+ Rails::Initializer.send(:include, DomainSlice::Rails::Initializer) if DomainSlice.exists?
@@ -1,15 +1,44 @@
1
- require 'domain_slice'
1
+ require File.join(File.dirname(__FILE__), %w[.. domain_slice])
2
2
  require File.join(File.dirname(__FILE__), %w[rails initializer])
3
3
 
4
4
  module DomainSlice
5
5
  module Rails
6
6
  class << self
7
- def init
8
- # Strap all of the domain-slice crap into rails
7
+ def init!
8
+ ::Rails.public_path = File.join(root, 'public')
9
+ ::ActionController::Routing::Routes.add_configuration_file File.join(config_path, 'routes.rb')
10
+ ::ActionController::Base.view_paths.unshift File.join(root, %w[app views])
9
11
  end
10
12
 
11
13
  def root
12
-
14
+ File.join(RAILS_ROOT, 'domains', DomainSlice.domain)
15
+ end
16
+
17
+ def environment_path
18
+ File.join(config_path, 'environments', "#{RAILS_ENV}.rb")
19
+ end
20
+
21
+ def root_environment_path
22
+ File.join(config_path, 'environment.rb')
23
+ end
24
+
25
+ def config_path
26
+ File.join(root, 'config')
27
+ end
28
+
29
+ def configure(configuration = ::Rails::Configuration.new)
30
+ # Create our new configuration block here
31
+ configuration.database_configuration_file = database_configuration_file
32
+ return configuration
33
+ end
34
+
35
+ protected
36
+ def routes_configuration_file
37
+ File.join(config_path, 'routes.rb')
38
+ end
39
+
40
+ def database_configuration_file
41
+ File.join(config_path, 'database.yml')
13
42
  end
14
43
  end
15
44
  end
data/lib/domain_slice.rb CHANGED
@@ -1,2 +1,18 @@
1
+ DOMAIN_SLICE = ENV['DOMAIN_SLICE'] unless defined?(DOMAIN_SLICE)
2
+
1
3
  module DomainSlice
2
- end
4
+ class << self
5
+ attr_accessor :domain
6
+
7
+ def exists?
8
+ !@domain.nil? and @domain != ''
9
+ end
10
+
11
+ # Binds the DomainSlice to the environmental variable DOMAIN_SLICE
12
+ def init!
13
+ @domain = DOMAIN_SLICE
14
+ end
15
+ end
16
+ end
17
+
18
+ DomainSlice.init!
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,24 @@
1
1
  require 'rubygems'
2
2
  require 'spec'
3
3
 
4
+ # Loads various ruby files and application stacks for testing purposes
4
5
  module DomainSlice
5
6
  module TestHelpers
6
7
  class << self
7
- def require_domain_slice
8
+ def require_domain_slice!
8
9
  require File.join(File.dirname(__FILE__), %w[.. lib domain_slice])
9
10
  end
11
+
12
+ def require_middleware!
13
+ require 'rack'
14
+ require File.join(File.dirname(__FILE__), %w[.. lib domain_slice middleware])
15
+ end
16
+
17
+ def require_rails!
18
+ # Load whatever is needed to test domain slice integration with rails
19
+ # require 'rails'
20
+ require File.join(File.dirname(__FILE__), %w[.. lib domain_slice rails])
21
+ end
10
22
  end
11
23
  end
12
24
  end
@@ -1,4 +1,23 @@
1
1
  require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
2
 
3
- describe DomainSlice, "loading" do
3
+ DomainSlice::TestHelpers.require_domain_slice!
4
+
5
+ describe DomainSlice do
6
+ it "should have init!" do
7
+ DomainSlice.init!
8
+ end
9
+
10
+ it "should initialize with DOMAIN_SLICE value" do
11
+ DOMAIN_SLICE = 'foo.com'
12
+ DomainSlice.init!
13
+ DomainSlice.domain.should eql(DOMAIN_SLICE)
14
+ end
15
+
16
+ it "should have name" do
17
+ DomainSlice.should respond_to(:name)
18
+ end
19
+
20
+ it "should have domain" do
21
+ DomainSlice.should respond_to(:domain)
22
+ end
4
23
  end
@@ -0,0 +1,7 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ DomainSlice::TestHelpers::require_middleware!
4
+
5
+ describe DomainSlice::Middleware::Static do
6
+
7
+ end
@@ -1 +1,15 @@
1
- require File.join(File.dirname(__FILE__), %w[.. spec_helper])
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ DomainSlice::TestHelpers::require_rails!
4
+
5
+ describe Initiailizer do
6
+ it "should initialize without DOMAIN_SLICE"
7
+
8
+ it "should initialize with DOMAIN_SLICE"
9
+
10
+ it %{should override RAILS_ROOT/config/environments.rb
11
+ with RAILS_ROOT/domains/DOMAIN_SLICE/config/environments.rb}
12
+
13
+ it %{should override RAILS_ROOT/config/environments/RAILS_ENV.rb
14
+ with RAILS_ROOT/domains/DOMAIN_SLICE/config/environments/RAILS_ENV.rb}
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bradgessler-domain_slice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Gessler
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-25 00:00:00 -07:00
12
+ date: 2009-03-27 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -31,9 +31,11 @@ files:
31
31
  - lib/domain_slice/rails/initializer.rb
32
32
  - lib/domain_slice/rails.rb
33
33
  - lib/domain_slice.rb
34
+ - spec/fixtures
34
35
  - spec/spec_helper.rb
35
36
  - spec/specs
36
37
  - spec/specs/domain_slice_spec.rb
38
+ - spec/specs/middleware_spec.rb
37
39
  - spec/specs/rails_spec.rb
38
40
  has_rdoc: true
39
41
  homepage: http://github.com/bradgessler/domain-slice