Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move Rails::Static into ActionDispatch

  • Loading branch information...
commit ef58194129a1dc0a96286c271b71455d37712b42 1 parent dbb3211
@josh josh authored
View
3  actionpack/lib/action_dispatch.rb
@@ -34,10 +34,11 @@ module ActionDispatch
autoload :Utils, 'action_dispatch/http/utils'
autoload :Callbacks, 'action_dispatch/middleware/callbacks'
+ autoload :MiddlewareStack, 'action_dispatch/middleware/stack'
autoload :ParamsParser, 'action_dispatch/middleware/params_parser'
autoload :Rescue, 'action_dispatch/middleware/rescue'
autoload :ShowExceptions, 'action_dispatch/middleware/show_exceptions'
- autoload :MiddlewareStack, 'action_dispatch/middleware/stack'
+ autoload :Static, 'action_dispatch/middleware/static'
autoload :Assertions, 'action_dispatch/testing/assertions'
autoload :Integration, 'action_dispatch/testing/integration'
View
44 actionpack/lib/action_dispatch/middleware/static.rb
@@ -0,0 +1,44 @@
+require 'rack/utils'
+
+module ActionDispatch
+ class Static
+ FILE_METHODS = %w(GET HEAD).freeze
+
+ def initialize(app, root)
+ @app = app
+ @file_server = ::Rack::File.new(root)
+ end
+
+ def call(env)
+ path = env['PATH_INFO'].chomp('/')
+ method = env['REQUEST_METHOD']
+
+ if FILE_METHODS.include?(method)
+ if file_exist?(path)
+ return @file_server.call(env)
+ else
+ cached_path = directory_exist?(path) ? "#{path}/index" : path
+ cached_path += ::ActionController::Base.page_cache_extension
+
+ if file_exist?(cached_path)
+ env['PATH_INFO'] = cached_path
+ return @file_server.call(env)
+ end
+ end
+ end
+
+ @app.call(env)
+ end
+
+ private
+ def file_exist?(path)
+ full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
+ File.file?(full_path) && File.readable?(full_path)
+ end
+
+ def directory_exist?(path)
+ full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
+ File.directory?(full_path) && File.readable?(full_path)
+ end
+ end
+end
View
15 railties/test/rack_static_test.rb → actionpack/test/dispatch/static_test.rb
@@ -1,21 +1,10 @@
require 'abstract_unit'
-require 'action_controller'
-require 'rails/rack'
-
-class RackStaticTest < ActiveSupport::TestCase
- def setup
- FileUtils.cp_r "#{RAILS_ROOT}/fixtures/public", "#{RAILS_ROOT}/public"
- end
-
- def teardown
- FileUtils.rm_rf "#{RAILS_ROOT}/public"
- end
-
+class StaticTest < ActiveSupport::TestCase
DummyApp = lambda { |env|
[200, {"Content-Type" => "text/plain"}, ["Hello, World!"]]
}
- App = Rails::Rack::Static.new(DummyApp, "#{RAILS_ROOT}/public")
+ App = ActionDispatch::Static.new(DummyApp, "#{FIXTURE_LOAD_PATH}/public")
test "serves dynamic content" do
assert_equal "Hello, World!", get("/nofile")
View
0  railties/test/fixtures/public/foo/bar.html → actionpack/test/fixtures/public/foo/bar.html
File renamed without changes
View
0  railties/test/fixtures/public/foo/index.html → ...npack/test/fixtures/public/foo/index.html
File renamed without changes
View
0  railties/test/fixtures/public/index.html → actionpack/test/fixtures/public/index.html
File renamed without changes
View
11 railties/guides/source/rails_on_rack.textile
@@ -40,11 +40,8 @@ Here's how +script/server+ creates an instance of +Rack::Builder+
app = Rack::Builder.new {
use Rails::Rack::LogTailer unless options[:detach]
use Rails::Rack::Debugger if options[:debugger]
-
- map "/" do
- use Rails::Rack::Static
- run ActionController::Dispatcher.new
- end
+ use ActionDispatch::Static
+ run ActionController::Dispatcher.new
}.to_app
</ruby>
@@ -52,7 +49,7 @@ Middlewares used in the code above are primarily useful only in the development
|_.Middleware|_.Purpose|
|+Rails::Rack::LogTailer+|Appends log file output to console|
-|+Rails::Rack::Static+|Serves static files inside +RAILS_ROOT/public+ directory|
+|+ActionDispatch::Static+|Serves static files inside +RAILS_ROOT/public+ directory|
|+Rails::Rack::Debugger+|Starts Debugger|
h4. +rackup+
@@ -64,7 +61,7 @@ To use +rackup+ instead of Rails' +script/server+, you can put the following ins
require "config/environment"
use Rails::Rack::LogTailer
-use Rails::Rack::Static
+use ActionDispatch::Static
run ActionController::Dispatcher.new
</ruby>
View
2  railties/lib/rails/initializer.rb
@@ -272,7 +272,7 @@ def self.run(initializer = nil, config = nil)
# Include middleware to serve up static assets
Initializer.default.add :initialize_static_server do
if configuration.frameworks.include?(:action_controller) && configuration.serve_static_assets
- configuration.middleware.insert(0, Rails::Rack::Static, Rails.public_path)
+ configuration.middleware.insert(0, ActionDispatch::Static, Rails.public_path)
end
end
View
47 railties/lib/rails/rack/static.rb
@@ -1,46 +1,5 @@
-require 'rack/utils'
+require 'action_dispatch'
-module Rails
- module Rack
- class Static
- FILE_METHODS = %w(GET HEAD).freeze
-
- def initialize(app, root)
- @app = app
- @file_server = ::Rack::File.new(root)
- end
-
- def call(env)
- path = env['PATH_INFO'].chomp('/')
- method = env['REQUEST_METHOD']
-
- if FILE_METHODS.include?(method)
- if file_exist?(path)
- return @file_server.call(env)
- else
- cached_path = directory_exist?(path) ? "#{path}/index" : path
- cached_path += ::ActionController::Base.page_cache_extension
-
- if file_exist?(cached_path)
- env['PATH_INFO'] = cached_path
- return @file_server.call(env)
- end
- end
- end
-
- @app.call(env)
- end
-
- private
- def file_exist?(path)
- full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
- File.file?(full_path) && File.readable?(full_path)
- end
-
- def directory_exist?(path)
- full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
- File.directory?(full_path) && File.readable?(full_path)
- end
- end
- end
+module Rails::Rack
+ Static = Deprecation::DeprecatedConstantProxy.new('Rails::Rack::Static', ActionDispatch::Static)
end
Please sign in to comment.
Something went wrong with that request. Please try again.