27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

536 CHAPTER 18 Rack-based applications<br />

In this request, the ActionDispatch::Static middleware first checks for the presence<br />

of public/projects.html, which would be there if you had cached the page.<br />

Because it’s not there, the request goes through the rest of the middleware stack<br />

being passed along. When it gets to ActionDispatch::Best::StandardsSupport, this<br />

middleware sets the X-UA-Compatible header and passes along the request to the<br />

application, which then serves the request like normal.<br />

Let’s dive into exactly how ActionDispatch::Static works.<br />

18.4.2 Investigating ActionDispatch::Static<br />

The ActionDispatch::Static class is responsible for serving static requests in the<br />

development environment for your application. The code for this piece of middleware<br />

can be found in the actionpack gem, which you can view by opening the path provided<br />

by the command bundle show actionpack in your editor. The file that defines the<br />

ActionDispatch::Static middleware can be found at lib/action_dispatch/<br />

middleware/static.rb. The first 10 lines in this file are shown inthe following listing.<br />

Listing 18.10 Listing 18.10 lib/action_dispatch/middleware/static.rb<br />

require 'rack/utils'<br />

module ActionDispatch<br />

class Static<br />

FILE_METHODS = %w(GET HEAD).freeze<br />

def initialize(app, root)<br />

@app = app<br />

@file_server = ::Rack::File.new(root)<br />

end<br />

The first line requires the rack/utilsfile, which contains the Rack::Utils module this<br />

file references a little later on. The next three lines define the ActionDispatch<br />

::Static class and a constant called FILE_METHODS. Finally, this middleware defines<br />

the initialize method, which takes two arguments, an app and a root. This method<br />

then assigns the app argument to the @app variable so that it can be accessed in methods<br />

called after this point. The root variable is passed to a call to ::Rack::File.new 7<br />

which is then assigned to the @file_server variable. The Rack::File class is actually<br />

a Rack application too, as you’ll see later.<br />

So why does this middleware begin in this particular way, and what is app? Well, app<br />

is the next piece of middleware in the stack that you can pass requests to if you wish.<br />

You’ll see this app variable used later. The instance of your middleware is then cached<br />

by the Rails server so it can call the call method on it when a request comes into the<br />

stack. The call method in this middleware begins with this:<br />

7 Not-well-known Ruby fact: the double colon (::) of ::Rack::File represents a top-level class, meaning that<br />

Ruby will get the Rack::File class defined at the top level of your application, rather than one (potentially)<br />

within the current class or module.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!